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