Protocol Handler Component
authorJustin Dickow <jjdickow@gmail.com>
Tue, 15 Jul 2014 15:44:38 +0000 (11:44 -0400)
committerJustin Dickow <jjdickow@gmail.com>
Tue, 15 Jul 2014 15:44:38 +0000 (11:44 -0400)
Signed-off-by: Justin Dickow <jjdickow@gmail.com>
src/components/protocol_handler/include/protocol_handler/protocol_handler.h
src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h
src/components/protocol_handler/include/protocol_handler/session_observer.h
src/components/protocol_handler/include/protocol_handler/time_metric_observer.h
src/components/protocol_handler/src/message_priority.cc
src/components/protocol_handler/src/protocol_handler_impl.cc
src/components/protocol_handler/src/rpc_type.cc
src/components/protocol_handler/src/service_type.cc

index 31c5e8d..bbe3c41 100644 (file)
@@ -83,6 +83,11 @@ class ProtocolHandler {
      */
     virtual void SendFramesNumber(int32_t connection_key, int32_t number_of_frames) = 0;
 
+    /*
+     * Prepare and send heartbeat message to mobile app
+     */
+    virtual void SendHeartBeat(int32_t connection_id, uint8_t session_id) = 0;
+
   protected:
     /**
      * \brief Destructor
index 92bc51d..292a7f0 100644 (file)
@@ -39,7 +39,6 @@
 #include <map>
 #include <memory>
 #include <set>
-#include "utils/logger.h"
 #include "utils/prioritized_queue.h"
 #include "utils/message_queue.h"
 #include "utils/threads/thread.h"
@@ -53,6 +52,7 @@
 #include "transport_manager/common.h"
 #include "transport_manager/transport_manager.h"
 #include "transport_manager/transport_manager_listener_empty.h"
+#include "time_metric_observer.h"
 
 /**
  *\namespace NsProtocolHandler
@@ -172,6 +172,18 @@ class ProtocolHandlerImpl
      */
     void SendFramesNumber(int32_t connection_key, int32_t number_of_frames);
 
+    /**
+     * @brief Setup observer for time metric.
+     *
+     * @param observer - pointer to observer
+     */
+    void SetTimeMetricObserver(PHMetricObserver* observer);
+
+    /*
+     * Prepare and send heartbeat message to mobile
+     */
+    void SendHeartBeat(int32_t connection_id, uint8_t session_id);
+
   protected:
 
     /**
@@ -414,18 +426,11 @@ class ProtocolHandlerImpl
     void Handle(const impl::RawFordMessageToMobile& message);
   private:
     /**
-     * \brief For logging.
-     */
-#ifdef ENABLE_LOG
-    static log4cxx::LoggerPtr logger_;
-#endif // ENABLE_LOG
-
-    /**
      *\brief Pointer on instance of class implementing IProtocolObserver
      *\brief (JSON Handler)
      */
     ProtocolObservers protocol_observers_;
-
+    PHMetricObserver* metric_observer_;
     /**
      *\brief Pointer on instance of class implementing ISessionObserver
      *\brief (Connection Handler)
index 9151b9d..1b691ca 100644 (file)
@@ -64,6 +64,7 @@ class SessionObserver {
     virtual int32_t OnSessionStartedCallback(
       const transport_manager::ConnectionUID& connection_handle,
       const uint8_t& sessionId,
+      const uint8_t& protocol_version,
       const ServiceType& service_type) = 0;
 
     /**
index c003d89..cbf8dbb 100644 (file)
 
 #ifndef SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_TIME_METRIC_OBSERVER_H_
 #define SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_TIME_METRIC_OBSERVER_H_
+#include "protocol_handler/raw_message.h"
+
+#include <stdint.h>
+#include "utils/date_time.h"
 
 namespace protocol_handler {
 
 class PHMetricObserver {
+ public:
   struct MessageMetric {
-      time_t begin;
-      time_t end;
-      std::string GetStylesString() {
-        return "PHMetricObserver";
-      }
+    RawMessagePtr raw_msg;
+    uint32_t message_id;
+    uint8_t connection_key;
+    TimevalStruct begin;
+    TimevalStruct end;
   };
-  virtual void OnMessage(MessageMetric& metric) = 0;
-};
+  /**
+   */
+  virtual void StartMessageProcess(uint32_t message_id) = 0;
 
-}
+  virtual void EndMessageProcess(utils::SharedPtr<MessageMetric> m) = 0;
+  virtual ~PHMetricObserver(){}
+};
+}  // protocol_handler
 #endif  // SRC_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_TIME_METRIC_OBSERVER_H_
index 7a9c30d..b336363 100644 (file)
@@ -1,14 +1,8 @@
 #include "protocol_handler/service_type.h"
 #include "protocol_handler/message_priority.h"
 
-#include "utils/logger.h"
 #include "utils/macro.h"
 
-namespace {
-log4cxx::LoggerPtr g_logger = log4cxx::LoggerPtr(
-    log4cxx::Logger::getLogger("ConnectionHandler"));
-}
-
 namespace protocol_handler {
 
 // static
index fcd47ac..1207685 100644 (file)
 
 #include <memory.h>
 
+#include "utils/logger.h"
+
 #include "connection_handler/connection_handler_impl.h"
 #include "config_profile/profile.h"
 
 namespace protocol_handler {
 
-#ifdef ENABLE_LOG
-log4cxx::LoggerPtr ProtocolHandlerImpl::logger_ = log4cxx::LoggerPtr(
-    log4cxx::Logger::getLogger("ProtocolHandler"));
-#endif // ENABLE_LOG
-
+CREATE_LOGGERPTR_GLOBAL(logger_, "ProtocolHandler")
 
 /**
  * Function return packet data as std::string.
@@ -156,7 +154,8 @@ ProtocolHandlerImpl::ProtocolHandlerImpl(
       raw_ford_messages_from_mobile_("MessagesFromMobileAppHandler", this,
                                      threads::ThreadOptions(kStackSize)),
       raw_ford_messages_to_mobile_("MessagesToMobileAppHandler", this,
-                                   threads::ThreadOptions(kStackSize)) {
+                                   threads::ThreadOptions(kStackSize)),
+      metric_observer_(NULL) {
   LOG4CXX_TRACE_ENTER(logger_);
 
   LOG4CXX_TRACE_EXIT(logger_);
@@ -302,6 +301,21 @@ RESULT_CODE ProtocolHandlerImpl::SendHeartBeatAck(ConnectionID connection_id,
   return RESULT_OK;
 }
 
+void ProtocolHandlerImpl::SendHeartBeat(int32_t connection_id,
+                                               uint8_t session_id) {
+  LOG4CXX_TRACE_ENTER(logger_);
+
+  ProtocolFramePtr ptr(new protocol_handler::ProtocolPacket(connection_id,
+      PROTOCOL_VERSION_3, COMPRESS_OFF, FRAME_TYPE_CONTROL,
+      SERVICE_TYPE_ZERO, FRAME_DATA_HEART_BEAT, session_id,
+      0, 0));
+
+  raw_ford_messages_to_mobile_.PostMessage(
+      impl::RawFordMessageToMobile(ptr, false));
+
+  LOG4CXX_TRACE_EXIT(logger_);
+}
+
 void ProtocolHandlerImpl::SendMessageToMobileApp(const RawMessagePtr& message,
                                                  bool final_message) {
   LOG4CXX_TRACE_ENTER(logger_);
@@ -363,10 +377,6 @@ void ProtocolHandlerImpl::SendMessageToMobileApp(const RawMessagePtr& message,
 
 void ProtocolHandlerImpl::OnTMMessageReceived(const RawMessagePtr tm_message) {
   LOG4CXX_TRACE_ENTER(logger_);
-  connection_handler::ConnectionHandlerImpl* connection_handler =
-      connection_handler::ConnectionHandlerImpl::instance();
-  // Connection handler should be accessed from TM thread only
-  connection_handler->KeepConnectionAlive(tm_message->connection_key());
 
   if (tm_message) {
     LOG4CXX_INFO_EXT(
@@ -394,8 +404,11 @@ void ProtocolHandlerImpl::OnTMMessageReceived(const RawMessagePtr tm_message) {
   for (std::vector<ProtocolFramePtr>::const_iterator it =
            protocol_frames.begin();
        it != protocol_frames.end(); ++it) {
-    raw_ford_messages_from_mobile_.PostMessage(
-        impl::RawFordMessageFromMobile(*it));
+    impl::RawFordMessageFromMobile msg(*it);
+    if (metric_observer_) {
+      metric_observer_->StartMessageProcess(msg->message_id());
+    }
+    raw_ford_messages_from_mobile_.PostMessage(msg);
   }
   LOG4CXX_TRACE_EXIT(logger_);
 }
@@ -607,7 +620,6 @@ RESULT_CODE ProtocolHandlerImpl::SendMultiFrameMessage(
 RESULT_CODE ProtocolHandlerImpl::HandleMessage(ConnectionID connection_id,
                                                const ProtocolFramePtr& packet) {
   LOG4CXX_TRACE_ENTER(logger_);
-
   switch (packet->frame_type()) {
     case FRAME_TYPE_CONTROL: {
       LOG4CXX_INFO(logger_, "handleMessage(1) - case FRAME_TYPE_CONTROL");
@@ -636,7 +648,13 @@ RESULT_CODE ProtocolHandlerImpl::HandleMessage(ConnectionID connection_id,
       RawMessagePtr raw_message(
           new RawMessage(connection_key, packet->protocol_version(), packet->data(),
                          packet->data_size(), packet->service_type()));
-
+      if (metric_observer_) {
+        PHMetricObserver::MessageMetric* metric = new PHMetricObserver::MessageMetric();
+        metric->message_id = packet->message_id();
+        metric->connection_key = connection_key;
+        metric->raw_msg = raw_message;
+        metric_observer_->EndMessageProcess(metric);
+      }
       NotifySubscribers(raw_message);
       break;
     }
@@ -660,7 +678,6 @@ RESULT_CODE ProtocolHandlerImpl::HandleMessage(ConnectionID connection_id,
 RESULT_CODE ProtocolHandlerImpl::HandleMultiFrameMessage(
     ConnectionID connection_id, const ProtocolFramePtr& packet) {
   LOG4CXX_TRACE_ENTER(logger_);
-
   if (!session_observer_) {
     LOG4CXX_ERROR(logger_, "No ISessionObserver set.");
 
@@ -718,10 +735,14 @@ RESULT_CODE ProtocolHandlerImpl::HandleMultiFrameMessage(
       }
 
       ProtocolPacket* completePacket = it->second.get();
-      RawMessage* rawMessage = new RawMessage(
+      RawMessagePtr rawMessage (new RawMessage(
           key, completePacket->protocol_version(), completePacket->data(),
-          completePacket->total_data_bytes(), completePacket->service_type());
-
+          completePacket->total_data_bytes(), completePacket->service_type()));
+      if (metric_observer_) {
+        PHMetricObserver::MessageMetric* metric = new PHMetricObserver::MessageMetric();
+        metric->raw_msg = rawMessage;
+        metric_observer_->EndMessageProcess(metric);
+      }
       NotifySubscribers(rawMessage);
 
       incomplete_multi_frame_messages_.erase(it);
@@ -734,6 +755,7 @@ RESULT_CODE ProtocolHandlerImpl::HandleMultiFrameMessage(
 
 RESULT_CODE ProtocolHandlerImpl::HandleControlMessage(
     ConnectionID connection_id, const ProtocolFramePtr& packet) {
+
   if (!session_observer_) {
     LOG4CXX_ERROR(logger_, "ISessionObserver is not set.");
 
@@ -751,6 +773,10 @@ RESULT_CODE ProtocolHandlerImpl::HandleControlMessage(
                    "Received heart beat for connection " << connection_id);
       return HandleControlMessageHeartBeat(connection_id, *(packet.get()));
     }
+    case FRAME_DATA_HEART_BEAT_ACK: {
+      LOG4CXX_INFO(logger_, "Received heart beat ack from mobile app"
+          " for connection " << connection_id);
+    }
     default:
       LOG4CXX_WARN(
           logger_,
@@ -810,7 +836,7 @@ RESULT_CODE ProtocolHandlerImpl::HandleControlMessageStartSession(
                    static_cast<int>(packet.protocol_version()));
 
   int32_t session_id = session_observer_->OnSessionStartedCallback(
-      connection_id, packet.session_id(),
+      connection_id, packet.session_id(), packet.protocol_version(),
       ServiceTypeFromByte(packet.service_type()));
 
   if (-1 != session_id) {
@@ -843,6 +869,11 @@ void ProtocolHandlerImpl::Handle(
     const impl::RawFordMessageFromMobile& message) {
   LOG4CXX_TRACE_ENTER(logger_);
 
+  connection_handler::ConnectionHandlerImpl* connection_handler =
+        connection_handler::ConnectionHandlerImpl::instance();
+    connection_handler->KeepConnectionAlive(message->connection_key(),
+                                            message->session_id());
+
   if (((0 != message->data()) && (0 != message->data_size())) ||
       FRAME_TYPE_CONTROL == message->frame_type() ||
       FRAME_TYPE_FIRST == message->frame_type()) {
@@ -886,7 +917,11 @@ void ProtocolHandlerImpl::SendFramesNumber(int32_t connection_key,
       session_id, 0, number_of_frames));
 
   raw_ford_messages_to_mobile_.PostMessage(
-      impl::RawFordMessageToMobile(ptr, false));
+        impl::RawFordMessageToMobile(ptr, false));
+}
+
+void ProtocolHandlerImpl::SetTimeMetricObserver(PHMetricObserver* observer) {
+  metric_observer_ = observer;
 }
 
 std::string ConvertPacketDataToString(const uint8_t* data,
index 49c503b..618dd48 100644 (file)
 
 namespace protocol_handler {
 
-namespace {
-log4cxx::LoggerPtr g_logger = log4cxx::LoggerPtr(
-    log4cxx::Logger::getLogger("ProtocolHandler"));
+CREATE_LOGGERPTR_GLOBAL(logger_, "ProtocolHandler")
 
+namespace {
 bool IsSupported(RpcType rpc_type) {
   switch (rpc_type) {
     case kRpcTypeRequest:
@@ -56,7 +55,7 @@ RpcType RpcTypeFromByte(uint8_t byte) {
   RpcType type = RpcType(byte);
   bool supported_type = IsSupported(type);
   if (!supported_type) {
-    LOG4CXX_INFO(g_logger, "Invalid service type: "<<int32_t(byte));
+    LOG4CXX_INFO(logger_, "Invalid service type: "<<int32_t(byte));
   }
 
   return supported_type ? type : kRpcTypeReserved;
index 6bc4896..65a4a15 100644 (file)
@@ -4,13 +4,11 @@
 #include "utils/logger.h"
 #include "utils/macro.h"
 
-
 namespace protocol_handler {
 
-namespace {
-log4cxx::LoggerPtr g_logger = log4cxx::LoggerPtr(
-    log4cxx::Logger::getLogger("ConnectionHandler"));
+CREATE_LOGGERPTR_GLOBAL(logger_, "ConnectionHandler")
 
+namespace {
 // Check if provided service value is one of the specified
 bool IsValid(ServiceType service_type) {
   switch (service_type) {
@@ -30,7 +28,7 @@ ServiceType ServiceTypeFromByte(uint8_t byte) {
   ServiceType type = ServiceType(byte);
   bool valid_type = IsValid(type);
   if (!valid_type) {
-    LOG4CXX_INFO(g_logger, "Invalid service type: "<<int32_t(byte))
+    LOG4CXX_INFO(logger_, "Invalid service type: "<<int32_t(byte))
   }
   return valid_type ? type : kInvalidServiceType;
 }