Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : // 7 : // sandesh_connection.h 8 : // 9 : // Sandesh connection class 10 : // 11 : 12 : #ifndef __SANDESH_CONNECTION_H__ 13 : #define __SANDESH_CONNECTION_H__ 14 : 15 : #include <boost/asio/ip/tcp.hpp> 16 : 17 : #include <base/util.h> 18 : #include <base/lifetime.h> 19 : #include "sandesh_state_machine.h" 20 : 21 : class Sandesh; 22 : class SandeshSession; 23 : class TcpSession; 24 : class TcpServer; 25 : class SandeshUVE; 26 : class SandeshHeader; 27 : class SandeshMessage; 28 : 29 : class SandeshConnection { 30 : public: 31 : typedef boost::asio::ip::tcp::endpoint Endpoint; 32 : SandeshConnection(const char *prefix, TcpServer *server, Endpoint endpoint, 33 : int task_instance, int task_id); 34 : virtual ~SandeshConnection(); 35 : 36 : // Invoked from server when a session is accepted. 37 : void AcceptSession(SandeshSession *session); 38 : virtual bool ReceiveMsg(const std::string &msg, SandeshSession *session); 39 : 40 12560 : TcpServer *server() { return server_; } 41 : 42 37 : Endpoint endpoint() { return endpoint_; } 43 : bool SendSandesh(Sandesh *snh); 44 : 45 : void set_session(SandeshSession *session); 46 : 47 : SandeshSession *session() const; 48 : SandeshStateMachine *state_machine() const; 49 : 50 : std::string StateName() const { return state_machine_->StateName(); } 51 : 52 111 : int GetTaskInstance() const { 53 : // Parallelism between connections 54 111 : return task_instance_; 55 : } 56 : 57 74 : int GetTaskId() const { 58 74 : return task_id_; 59 : } 60 : 61 37 : void Initialize() { 62 37 : state_machine_->Initialize(); 63 37 : } 64 : 65 : void Shutdown(); 66 : bool MayDelete() const; 67 : 68 : void SetAdminState(bool down); 69 : 70 0 : virtual bool ProcessResourceUpdate(bool res) { return true; } 71 : virtual bool ProcessSandeshMessage(const SandeshMessage *msg, 72 : bool resource) = 0; 73 : virtual bool ProcessSandeshCtrlMessage(const std::string &msg, 74 : const SandeshHeader &header, const std::string sandesh_name, 75 : const uint32_t header_offset) = 0; 76 : virtual void ProcessDisconnect(SandeshSession * sess) = 0; 77 : 78 : virtual void ManagedDelete() = 0; 79 : virtual LifetimeActor *deleter() = 0; 80 : virtual LifetimeManager *lifetime_manager() = 0; 81 : virtual void Destroy() = 0; 82 : 83 : protected: 84 : bool is_deleted_; 85 : 86 : private: 87 : friend class SandeshServerStateMachineTest; 88 : 89 : TcpServer *server_; 90 : Endpoint endpoint_; 91 : bool admin_down_; 92 : SandeshSession *session_; 93 : int task_instance_; 94 : int task_id_; 95 : boost::scoped_ptr<SandeshStateMachine> state_machine_; 96 : 97 : DISALLOW_COPY_AND_ASSIGN(SandeshConnection); 98 : }; 99 : 100 : class SandeshServerConnection : public SandeshConnection { 101 : public: 102 : SandeshServerConnection(TcpServer *server, Endpoint endpoint, 103 : int task_instance, int task_id); 104 : virtual ~SandeshServerConnection(); 105 : 106 : virtual bool ProcessResourceUpdate(bool res); 107 : virtual bool ProcessSandeshMessage(const SandeshMessage *msg, 108 : bool resource); 109 : virtual bool ProcessSandeshCtrlMessage(const std::string &msg, 110 : const SandeshHeader &header, const std::string sandesh_name, 111 : const uint32_t header_offset); 112 : virtual void ProcessDisconnect(SandeshSession *session); 113 : 114 : virtual void ManagedDelete(); 115 : virtual LifetimeActor *deleter(); 116 : virtual LifetimeManager *lifetime_manager(); 117 : virtual void Destroy(); 118 : 119 : private: 120 : class DeleteActor; 121 : boost::scoped_ptr<DeleteActor> deleter_; 122 : LifetimeRef<SandeshServerConnection> server_delete_ref_; 123 : 124 : DISALLOW_COPY_AND_ASSIGN(SandeshServerConnection); 125 : }; 126 : 127 : #endif // __SANDESH_CONNECTION_H__