00001
00009 #ifndef CLUSTERSYNC_H
00010 #define CLUSTERSYNC_H
00011
00012 #include "IS3DCommon.H"
00013 #include "Event.H"
00014 #include "FsaMgr.H"
00015
00016 namespace IS3D {
00017
00018
00019 #define CLUSTERSYNC_SERVER_PORT 1201
00020
00021
00022 #define CLUSTERSYNC_CLIENT_PORT 1301
00023
00024
00025 #define CLUSTERSYNC_MS_SLEEP_TIME 10
00026
00027
00028
00029
00030
00031 enum ClusterMsgType {
00032 NO_MSG = 0,
00033 Ping_MSG = 2001,
00034 Sync_MSG = 2002,
00035 Event_MSG = 2003,
00036 EventBuffer_MSG = 2004
00037 };
00038
00039 static const std::string clientGreeting = "hello, server";
00040 static const std::string serverResponse = "hello, client";
00041
00043 class PingMessage {
00044 public:
00045 PingMessage() : text("") {}
00046 PingMessage(const std::string& s) : text(s) {}
00047
00048 virtual uint32 type() const {
00049 return Ping_MSG;
00050 }
00051
00052 virtual void serialize(BinaryOutput& b) const {
00053 b.writeString(text);
00054 }
00055
00056 virtual void deserialize(BinaryInput& b) {
00057 text = b.readString();
00058 }
00059
00060 std::string text;
00061 };
00062
00063
00064 class SyncMessage {
00065 public:
00066 enum SyncMessageID {
00067 SyncNOP=0,
00068 RequestEvents=1,
00069 ReadyToSwapBuffers=2,
00070 ProceedWithSwapBuffers=3,
00071 Shutdown=4
00072 };
00073
00074 SyncMessage() : id(SyncNOP) {}
00075 SyncMessage(const int32 msg_id) : id(msg_id) {}
00076
00077 virtual uint32 type() const {
00078 return Sync_MSG;
00079 }
00080
00081 virtual void serialize(BinaryOutput& b) const {
00082 b.writeInt32(id);
00083 }
00084
00085 virtual void deserialize(BinaryInput& b) {
00086 id = b.readInt32();
00087
00088 if (id == Shutdown) {
00089 cout << "Received shutdown message from ClusterSync server." << endl;
00090 exit(0);
00091 }
00092 }
00093
00094 int32 id;
00095 };
00096
00097
00098
00099
00100 class ClusterEventMsg
00101 {
00102 public:
00103 ClusterEventMsg() { _event = NULL; }
00104 ClusterEventMsg(EventRef e) { _event = e; }
00105
00106 virtual ~ClusterEventMsg() {}
00107
00108 uint32 type() const { return Event_MSG; }
00109
00110 void serialize(BinaryOutput &b) const {
00111 alwaysAssertM( ! _event.isNull(), "Null event in serialize method");
00112 _event->serialize(b);
00113 }
00114
00115 void deserialize(BinaryInput &b) {
00116 _event = new Event("temp-name");
00117 _event->deserialize(b);
00118 }
00119
00120 EventRef getEvent() { return _event; }
00121
00122 private:
00123 EventRef _event;
00124 };
00125
00126
00128 class ClusterEventBufferMsg
00129 {
00130 public:
00131 ClusterEventBufferMsg() {};
00132 ClusterEventBufferMsg(Array<EventRef> eventBuffer) {
00133 _eventBuffer = eventBuffer;
00134 }
00135
00136 virtual ~ClusterEventBufferMsg() {};
00137
00138 uint32 type() const { return EventBuffer_MSG; }
00139
00140 void serialize(BinaryOutput &b) const {
00141 b.writeInt32(_eventBuffer.size());
00142
00143 for (int i=0;i<_eventBuffer.size();i++) {
00144 _eventBuffer[i]->serialize(b);
00145 }
00146
00147 }
00148
00149 void deserialize(BinaryInput &b) {
00150
00151 int32 numEvents = b.readInt32();
00152
00153 for (int i=0;i<numEvents;i++) {
00154 EventRef e = new Event("temp-name");
00155 e->deserialize(b);
00156 _eventBuffer.append(e);
00157 }
00158 }
00159
00160 void queueEventBuffer() {
00161 for (int i=0;i<_eventBuffer.size();i++) {
00162 FsaMgr::queueEvent(_eventBuffer[i]);
00163 }
00164 }
00165
00166 private:
00167
00168 Array<EventRef> _eventBuffer;
00169 };
00170
00171
00172
00173
00174
00175
00176
00177
00178 class ClusterSyncClient
00179 {
00180 public:
00183 ClusterSyncClient(const std::string &serverHost, int serverPort, int myPort,
00184 bool verbose=false);
00185
00187 virtual ~ClusterSyncClient();
00188
00192 void pollServerForEvents();
00193
00197 void synchronizeSwapBuffers();
00198
00199 private:
00200
00201 NetworkDevice *_networkDevice;
00202 ReliableConduitRef _conduit;
00203 NetAddress _serverAddress;
00204 double _sleepTime;
00205 double _verbose;
00206 };
00207
00208
00209 }
00210
00211 #endif
00212