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 #include "NetOriginalMessageProtocol.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 NetOriginalMessageProtocol::NetOriginalMessageProtocol():NetProtocol("Original Message")
00034 {
00035 netTimeout = 60000;
00036 }
00037
00038 NetOriginalMessageProtocol::~NetOriginalMessageProtocol()
00039 {
00040
00041 }
00042
00043 Object* NetOriginalMessageProtocol::clone() const {
00044 return new NetOriginalMessageProtocol();
00045 }
00046
00047 bool NetOriginalMessageProtocol::checkBufferForCompatibility(char* buffer, int length) {
00048
00049 if (length < 8) return false;
00050
00051 JString str = buffer;
00052 if (str.startsWith("<message"))
00053 return true;
00054
00055 return false;
00056 }
00057
00058 bool NetOriginalMessageProtocol::initializeConversation(JSocket* socket) {
00059 if (socket == NULL)
00060 return false;
00061
00062 return true;
00063 }
00064
00065 bool NetOriginalMessageProtocol::sendObject(JSocket* socket, Message* msg, bool isReply) {
00066
00067
00068 int res;
00069 JString str;
00070
00071 if (!isReply) {
00072 Message* greeting = new Message(msg->getFrom(), msg->getTo(), "CONNECT_REQUEST");
00073 str = greeting->toXML();
00074 str = str.removeCRs();
00075 res = socket->writeln(str);
00076 delete(greeting);
00077 if (res < (int)str.length()) {
00078 if (DEBUGLEVEL(STATUS)) {
00079 printf("[NetOrigMsg] Failed to send CONNECT_REQUEST\n");
00080 }
00081 return false;
00082 }
00083
00084 Message* hmsg = receiveObject(socket, 1000);
00085 if (hmsg == NULL) {
00086 if (DEBUGLEVEL(STATUS)) {
00087 printf("[NetOrigMsg] Failed to receive CONNECT_ACCEPT\n");
00088 }
00089 return false;
00090 }
00091
00092 if (!hmsg->getType().equalsIgnoreCase("CONNECT_ACCEPT")) {
00093 if (DEBUGLEVEL(STATUS)) {
00094 printf("[NetOrigMsg] Received something else: %s\n", (char*) hmsg->getType());
00095 }
00096 delete(hmsg);
00097 return false;
00098 }
00099 delete(hmsg);
00100 }
00101
00102 str = msg->toXML();
00103 str = str.removeCRs();
00104 res = socket->writeln(str);
00105 if (res < (int)str.length())
00106 return false;
00107
00108 return true;
00109 }
00110
00111 Message* NetOriginalMessageProtocol::receiveObject(JSocket* socket, int timeout) {
00112
00113 Message* msg = NULL;
00114 JString str = socket->readln(timeout);
00115 if (str.length() > 0)
00116 str = str;
00117 if (!str.startsWith("<message"))
00118 return NULL;
00119 msg = new Message(str);
00120
00121 if (msg->getType().equalsIgnoreCase("CONNECT_REQUEST")) {
00122 Message* greeting = new Message(msg->getTo(), msg->getFrom(), "CONNECT_ACCEPT");
00123 delete(msg);
00124 if (DEBUGLEVEL(KITCHENSINK)) {
00125 printf("[NetOrigMsg] Sending CONNECT_ACCEPT...\n");
00126 }
00127 str = greeting->toXML();
00128 delete(greeting);
00129 str = str.removeCRs();
00130 int res = socket->writeln(str);
00131 if (res < (int)str.length()) {
00132 return NULL;
00133 }
00134
00135 str = socket->readln(timeout);
00136 if (!str.startsWith("<message")) {
00137 return NULL;
00138 }
00139 msg = new Message(str);
00140 }
00141
00142
00143
00144
00145
00146 JString hostname = socket->getRemoteIPAddress();
00147 if (hostname.equals("localhost"))
00148 hostname = socket->getLocalIPAddress();
00149
00150 if (msg->getOrigin().length() == 0)
00151 msg->setOrigin(hostname);
00152 msg->set("netprotocol", name);
00153
00154 return msg;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 }