|
MASA-Core
|
00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2010-2015 Edans Sandes 00004 * 00005 * This file is part of MASA-Core. 00006 * 00007 * MASA-Core is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * MASA-Core is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with MASA-Core. If not, see <http://www.gnu.org/licenses/>. 00019 * 00020 ******************************************************************************/ 00021 00022 class Peer; 00023 00024 #ifndef PEER_HPP_ 00025 #define PEER_HPP_ 00026 00027 #include <pthread.h> 00028 #include <sys/time.h> 00029 00030 #include <string> 00031 #include <map> 00032 #include <set> 00033 using namespace std; 00034 00035 #include "command/Command.hpp" 00036 #include "../common/exceptions/IOException.hpp" 00037 #include "MasaNetCallbacks.hpp" 00038 00039 /* Node Types */ 00040 #define TYPE_CLI (0) 00041 #define TYPE_PROCESSING_NODE (1) 00042 #define TYPE_GATEWAY (2) 00043 #define TYPE_UNKNOWN (-1) 00044 00045 00046 /* Ring Types */ 00047 #define RING_NONE (0) 00048 #define RING_RIGHT (+1) //Local to Remote 00049 #define RING_LEFT (-1) //Remote to Local 00050 00051 #define CONNECTION_TYPE_UNKNOWN (0) 00052 #define CONNECTION_TYPE_CTRL (1) 00053 #define CONNECTION_TYPE_DATA (2) 00054 00055 typedef Command* (*cmd_creator_f)(); 00056 00057 class Peer { 00058 public: 00059 int ringType; // TODO Proteger 00060 00061 Peer(int socket, const string& localId, bool initiator, int connectionType = CONNECTION_TYPE_UNKNOWN); 00062 Peer(string remoteId, string remoteAddress, 00063 const int remoteType, const int ringType, const int connectionType); 00064 virtual ~Peer(); 00065 00066 static void registerCommandCreator(int id, cmd_creator_f creator); 00067 Command* recvCommand(); 00068 Command* sendCommand(Command* command); 00069 00070 void addHook(int id, int serial = -1); 00071 Command* waitHook(); 00072 00073 bool isConnected(); 00074 bool handshake(); 00075 bool waitHandshake(); 00076 void finalize(); 00077 00078 void send_int32(int value); 00079 int recv_int32(); 00080 void send_int16(short value); 00081 short recv_int16(); 00082 void send_int8(char value); 00083 char recv_int8(); 00084 void send_array(const char* value, int len); 00085 void recv_array(char* value, int len); 00086 void send_vls8(const char* value); 00087 void recv_vls8(char* value, int max=-1); 00088 void send_vls8(const string& value); 00089 string recv_vls8(); 00090 00091 void recv_dummy(int len); 00092 00093 00094 void setLocalType(int localType); 00095 void setLocalAddress(const string& publicAddress); 00096 00097 const string& getLocalAddress() const; 00098 const string& getRemoteAddress() const; 00099 const string& getLocalId() const; 00100 const string& getRemoteId() const; 00101 int getLocalType() const; 00102 int getRemoteType() const; 00103 00104 string toString(); 00105 void setCallback(MasaNetCallbacks* callback); 00106 bool isInitiator() const; 00107 int getSocket() const; 00108 int getConnectionType() const; 00109 00110 private: 00111 MasaNetCallbacks* callback; 00112 00113 pthread_mutex_t mutex; 00114 pthread_cond_t responseCond; 00115 pthread_cond_t handshakeCond; 00116 //int waitingResponse; 00117 00118 //Command* response; 00119 00120 int socket; 00121 bool initiator; 00122 bool handshakeDone; 00123 bool connected; 00124 bool error; 00125 int serial; 00126 float timeout; 00127 00128 int connectionType; 00129 00130 static map<int, cmd_creator_f> cmdCreators; 00131 00132 map<int, set<pthread_t> > hookThreads; 00133 map<pthread_t, Command*> hookCommand; 00134 map<pthread_t, int> hookSerial; 00135 00136 string localId; 00137 string remoteId; 00138 string localAddress; 00139 string remoteAddress; 00140 int remoteType; 00141 int localType; 00142 00143 bool handshakeInitiator(); 00144 bool handshakeInitiated(); 00145 00146 int getNextSerial(); 00147 00148 void notifyHook(Command* cmd); 00149 00150 void handleSendError(int ret); 00151 void handleRecvError(int ret); 00152 00153 static bool hasStaticEvent; 00154 static timeval staticEvent; 00155 static float getGlobalTime(); 00156 static float getElapsedTime(timeval *end_time, timeval *start_time); 00157 00158 00159 }; 00160 00161 00162 #endif /* PEER_HPP_ */
1.7.6.1