00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if ! defined ( SERVER_HH )
00028 #define SERVER_HH
00029
00030
00031 #include <sys/types.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <arpa/inet.h>
00035 #include <unistd.h>
00036
00037
00038 #include <iostream>
00039 #include <string>
00040
00041
00042 #include "libpiave/piave_base.hh"
00043 #include "libpiave/storybase.hh"
00044
00045
00046 namespace PIAVE {
00047
00069 class Server
00070 {
00071 public:
00072 enum State {
00073 state_not_initialized,
00074 state_not_connected,
00075 state_error_connecting,
00076 state_connected,
00077 state_process_commands,
00078 state_shutting_down
00079 };
00080
00081 public:
00082 Server( unsigned short port );
00083 virtual ~Server();
00087 int execute();
00088
00091 void sendMessage( const std::string & str );
00092 int receiveMessage( std::string & str );
00093
00094 protected:
00097 virtual void startProcessCommandsLoop() = 0;
00098 void setState( State state ) { _state = state; }
00099 State getState() const { return _state; };
00100
00101 private:
00103 void initListenSocket();
00105 void closeListenSocket();
00107 void closeCommSocket();
00108
00109 private:
00111 State _state;
00112
00113
00115 unsigned short m_port;
00117 int m_listenSocket;
00119 int m_commSocket;
00121 sockaddr_in m_socketAddress;
00123 std::string m_commandBuffer;
00124
00127 int m_totalBytesRead;
00128
00130 std::string m_messageBuffer;
00131
00132 static std::string whitespace;
00133 public:
00134 static Server * Inst;
00135 static void cleanup_at_exit();
00136 };
00137
00138 };
00139
00140 #endif