- #ifndef __INTERFACEAPIIMP_H__
- #define __INTERFACEAPIIMP_H__
- //#include <string>
- #include "conn_base.h"
- //#include "SessionBase.h"
- #include "event_looper.h"
- #include "interface_api.h"
- #include <vector>
- #include <list>
- #include <map>
- #include <stdarg.h>
- #include <log4cxx/logger.h>
- #include <log4cxx/logstring.h>
- #include <log4cxx/propertyconfigurator.h>
- #include <log4cxx/helpers/exception.h>
- using namespace cdy;
- using namespace std;
- using namespace log4cxx;
- class SessionImp;
- typedef enum
- {
- SESSION_TS = 0,
-
- SESSION_APP_SERVER = 1,
- SESSION_APP_CLIENT = 2
- } SessionType;
- class SessionImp : public IConnectionSink
- {
- public:
- //SessionImp(ITsSessionUser *sessionUser,TsSession *tssession);
- virtual void SendMessage(TsMessageBase &msg){}
- virtual void SendMessage(AppMessageBase &msg){}
- void SendMessage(string &str);
-
- virtual void Close() = 0;
- // virtual void SetSessionType() = 0;
- virtual void SetConnection(Connection *connection) = 0;
- protected:
- void ShowReadBuf();
- void ProcessFrame(const char *buf, int length);
- virtual bool OnFrame(string &frame) = 0;
-
- Connection *m_connection;
-
- SessionType m_sessionType;
-
- vector<char> m_rdBuffer; //initial size set 1024.
-
- long m_idleTime;
- TimerID m_timeout;
- friend class SessionManagerImp;
- };
- class TsSessionImp : public SessionImp
- {
- public:
- TsSessionImp(ITsSessionUser *sessionUser,TsSession *tssession);
-
- void SetSessionUser(ITsSessionUser *sessionUser);
-
- virtual void SendMessage(TsMessageBase &msg);
- virtual void Close();
- // virtual void SetSessionType(){m_sessionType = SESSION_TS;}
-
- virtual void SetConnection(Connection *connection){m_connection = connection;}
- protected:
- virtual void OnData(const char *buf, int length, Connection *conn);
- // called when user can write data to network.
- virtual void OnWrite(Connection *conn);
- // called when the connection is broken, if user call Connection::Close(), //&&&
- // will not get this callback.
- virtual void OnDisconnect(int reason, Connection *conn); //
- virtual bool OnFrame(string &frame);
- private:
- ITsSessionUser *m_sessionUser;
- class TsSession* m_tssession;
- };
- class AppServerSessionImp : public SessionImp
- {
- public:
- AppServerSessionImp(Connection *connection,IAppEventListener *event_listener)
- : m_eventListener(event_listener)
- {
- m_connection = connection;
- m_sessionType = SESSION_APP_SERVER;
- }
-
- void SetEventListener(IAppEventListener *eventListener);
-
- virtual void SendMessage(AppMessageBase &msg);
- virtual void Close();
- //virtual void SetSessionType(){m_sessionType = SESSION_APP_LISTEN;}
- virtual void SetConnection(Connection *connection){m_connection = connection;}
- protected:
- virtual void OnData(const char *buf, int length, Connection *conn);
- // called when user can write data to network.
- virtual void OnWrite(Connection *conn);
- // called when the connection is broken, if user call Connection::Close(), //&&&
- // will not get this callback.
- virtual void OnDisconnect(int reason, Connection *conn); //
- virtual bool OnFrame(string &frame);
- private:
- IAppEventListener *m_eventListener;
- };
- class AppClientSessionImp : public SessionImp
- {
- public:
- AppClientSessionImp(Connection *connection)
- {
- if(connection != NULL)
- m_connection = connection;
- m_sessionType = SESSION_APP_CLIENT;
- }
- virtual void SendMessage(AppMessageBase &msg);
- virtual void Close();
- //virtual void SetSessionType(){m_sessionType = SESSION_APP_CONNECT;}
- virtual void SetConnection(Connection *connection){m_connection = connection;}
- protected:
- virtual void OnData(const char *buf, int length, Connection *conn);
- // called when user can write data to network.
- virtual void OnWrite(Connection *conn);
- // called when the connection is broken, if user call Connection::Close(), //&&&
- // will not get this callback.
- virtual void OnDisconnect(int reason, Connection *conn); //
- virtual bool OnFrame(string &frame);
- private:
- };
- struct TsSessionInfo{
- TsSession *session;
- bool timeOut;
- };
- class SessionManagerImp : public IConnectorAcceptorSink, public ITimerUserSink
- {
- //SessionManager &sessionmanager;
- SessionManagerImp(/*SessionManager &wrapper*/);
-
- public:
-
- ~SessionManagerImp();
- //-------- for test ----------------
- void ShowTsSessionList();
- void ShowConnectors();
- void ShowAppServerSessionList();
- void ShowAppClientSessionList();
- //-------------------------------
- static SessionManagerImp& GetInstance();
- bool StartListenForAppServer(IAppEventListener &listener);
- bool StartListenForTS(ITsSessionUser &sessionUser);
- void SendAppMessage(AppMessageBase &appMessage);
- void OnNewSession(SessionImp &session);
- void OnNewSession(TsSession &session);
- void OnSessionClose(SessionImp &session);
- void OnSessionClose(TsSession &session);
- IAppEventListener* GetAppEventListener() const;
- ITsSessionUser* GetSessionUser() const;
- void NewTimer(unsigned int dwInterval,TimerHandler pTimerHander );
-
- void KillTimer();
- protected:
- virtual void OnConnection(Connection *conn, IConnectorAcceptor *ca);
- virtual void OnTimer(TimerID tid);
- private:
-
- IAppEventListener *m_appEventListener;
- ITsSessionUser *m_sessionUser;
- Acceptor *m_appListener;
- Acceptor *m_tsListener;
-
- //list<SessionImp*> m_sessions;
- struct ConnectorInfo{
- //AppMessageBase *msg;
- string msg;
- unsigned int re_connect_times;
- };
- map<IConnectorAcceptor*,struct ConnectorInfo *> m_connectors;
- typedef list<struct TsSessionInfo*>TsSessionList;
- TsSessionList m_tsSessionList;
- typedef list<SessionImp*> SessionImpList;
-
- SessionImpList m_appServerSessionList;
- SessionImpList m_appClientSessionList;
-
- //timer
- TimerID m_idleConnectionChecker;
- TimerID m_connectionTimeout;
- TimerID m_timerForApp;
- TimerHandler m_timerHandler;
- friend class TsSessionImp;
- };
- typedef enum{
- DISCON_RECV_RESPONSE = 0,
- DISCON_APP_CLOSE = 1
- }DisconnectReason;
- #if 0
- #define BuildMsgVa(m_message,strFormat) \
- { \
- int ret; \
- va_list ap; \
- va_start(ap, strFormat); \
- ret = vsprintf(m_message, strFormat, ap); \
- if(ret < 0){ \
- return 0; \
- } \
- }
- class LoggerUser{
- LoggerPtr m_logger;
- char m_message[300];
- LoggerUser()
- {
- memset(m_message,0,300);
- PropertyConfigurator::configure("log4cxx.cfg");
- m_logger = Logger::getLogger("");
- }
- public:
- static LoggerUser& GetInstance()
- {
- static LoggerUser instance_;
- return instance_;
- }
- bool Trace(const char * strFormat,...)
- {/*
- BuildMsgVa(m_message,strFormat);
- LOG4CXX_TRACE(m_logger, m_message);
- */
- return 1;
- }
- bool Warn(char *strFormat,...)
- {/*
- BuildMsgVa(m_message,strFormat);
- LOG4CXX_WARN(m_logger, m_message);
- */
- return 1;
- }
- bool Debug(char *strFormat,...)
- {/*
- BuildMsgVa(m_message,strFormat);
- LOG4CXX_DEBUG(m_logger, m_message);
- */
- return 1;
- }
- bool Fatal(char *strFormat,...)
- {/*
- BuildMsgVa(m_message,strFormat);
- LOG4CXX_FATAL(m_logger, m_message);
- */
- return 1;
- }
- bool Error(char *strFormat,...)
- {/*
- BuildMsgVa(m_message,strFormat);
- LOG4CXX_ERROR(m_logger, m_message);
- */
- return 1;
- }
- };
- #endif
- #endif