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 "TCPLocation.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 TCPLocation::TCPLocation()
00034 {
00035 addr = "";
00036 port = 0;
00037 pCommunicator = NULL;
00038 setIPAddress("");
00039 isConnectedViaCNS = false;
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 TCPLocation::TCPLocation(const JString& xml) {
00054 pCommunicator = NULL;
00055 if (xml.length() > 0)
00056 fromXML(xml);
00057 else {
00058 addr = "";
00059 port = 0;
00060 setIPAddress("");
00061 }
00062 isConnectedViaCNS = false;
00063 }
00064
00065 TCPLocation::TCPLocation(XMLNode* node) {
00066 pCommunicator = NULL;
00067 if (!fromXML(node)) {
00068 addr = "";
00069 port = 0;
00070 setIPAddress("");
00071 }
00072 isConnectedViaCNS = false;
00073 }
00074
00075
00076 TCPLocation::TCPLocation(const JString& str, const JString& alias) {
00077 pCommunicator = NULL;
00078 if (set(str))
00079 name = alias;
00080 setIPAddress("");
00081 isConnectedViaCNS = false;
00082 }
00083
00084 TCPLocation::TCPLocation(const JString& str, int portNum) {
00085 pCommunicator = NULL;
00086 addr = str;
00087 port = portNum;
00088 name = "";
00089 setIPAddress("");
00090 isConnectedViaCNS = false;
00091 }
00092
00093 TCPLocation::TCPLocation(const JString& str, int portNum, const JString& alias) {
00094 pCommunicator = NULL;
00095 addr = str;
00096 port = portNum;
00097 name = alias;
00098 setIPAddress("");
00099 isConnectedViaCNS = false;
00100 }
00101
00102 TCPLocation::~TCPLocation()
00103 {
00104 }
00105
00106 Object* TCPLocation::clone() const {
00107 TCPLocation* t = new TCPLocation();
00108
00109 t->isConnectedViaCNS = isConnectedViaCNS;
00110 t->addr = addr;
00111 t->port = port;
00112 t->name = name;
00113 t->authentication = authentication;
00114 t->additional = additional;
00115 t->regID = regID;
00116 t->procid = procid;
00117 t->pCommunicator = pCommunicator;
00118 t->ipnumber.a = ipnumber.a;
00119 t->ipnumber.b = ipnumber.b;
00120 t->ipnumber.c = ipnumber.c;
00121 t->ipnumber.d = ipnumber.d;
00122 t->netProtocolName = netProtocolName;
00123 return (Object*) t;
00124 }
00125
00126
00127 unsigned long TCPLocation::getPayloadSize() const {
00128 return addr.getPayloadSize() + name.getPayloadSize() + authentication.getPayloadSize() + regID.getPayloadSize() +
00129 additional.getPayloadSize() + procid.getPayloadSize() + netProtocolName.getPayloadSize();
00130 }
00131
00132 bool TCPLocation::equals(const TCPLocation& loc) const {
00133 return ((loc.addr.equalsIgnoreCase(addr)) && (loc.port == port) && (loc.name.equalsIgnoreCase(name)));
00134 }
00135
00136 bool TCPLocation::isValid() const {
00137 if ( (addr.length() > 0) && ((port >= 0) || (port == NO_LISTEN)) )
00138 return true;
00139 else
00140 return false;
00141 }
00142
00143 bool TCPLocation::isNoListen() const {
00144 return (port == NO_LISTEN);
00145 }
00146
00147
00148 bool TCPLocation::set(const JString& str) {
00149
00150
00151
00152 int n = str.indexOf(":");
00153
00154 if (n > 0) {
00155 addr = str.substring(0, n);
00156 port = str.substring(n+1).toInt();
00157 return true;
00158 }
00159 else
00160 return false;
00161 }
00162
00163 JString TCPLocation::asText() const {
00164 JString str = addr;
00165 str += JString(":");
00166 str += JString(port);
00167 str += " ";
00168 str += name;
00169 return str;
00170 }
00171
00172 JString TCPLocation::getLocation() const {
00173 JString str = addr;
00174 str += JString(":");
00175 str += JString(port);
00176 return str;
00177 }
00178
00179 JString TCPLocation::print() {
00180 return asText();
00181 }
00182
00183 Collection TCPLocation::getAllAddresses() {
00184 Collection col;
00185 col.add(addr);
00186 if (additional.length() > 0)
00187 col.copyAll(additional.split(","));
00188 return col;
00189 }
00190
00191 JString TCPLocation::printList() {
00192 if (additional.length() > 0)
00193 return JString::format("%s:%d (%s)", (char*) addr, port, (char*) additional);
00194 else
00195 return JString::format("%s:%d", (char*) addr, port);
00196 }
00197
00198 JString TCPLocation::toXML() {
00199
00200 return JString::format("<tcplocation address=\"%s\" port=\"%d\" name=\"%s\" regid=\"%s\" procid=\"%s\" ipnumber=\"%s\" pointer=\"%s\" netprotocol=\"%s\" auth=\"%s\" additional=\"%s\" />",
00201 (char*) addr, port, (char*) name.xmlStringEncode(), (char*) regID, (char*) procid.xmlStringEncode(), (char*) getIPAddress(), (char*) JString(pCommunicator), (char*) netProtocolName.xmlStringEncode(), (char*) authentication.xmlStringEncode(), (char*) additional.xmlStringEncode());
00202 }
00203
00204 bool TCPLocation::fromXML(const JString& xml) {
00205
00206 bool ret;
00207
00208 XMLParser* xmlParser = new XMLParser();
00209 xmlParser->parse(xml);
00210
00211 XMLNode* xmlNode = xmlParser->getRootNode();
00212 if (xmlNode == NULL) {
00213 delete(xmlParser);
00214 return false;
00215 }
00216 if (xmlNode->getTag().equalsIgnoreCase("tcplocation"))
00217 ret = fromXML(xmlNode);
00218 else {
00219 ret = fromXML(xmlParser->getChildNode("tcplocation"));
00220 }
00221 delete(xmlParser);
00222 return ret;
00223
00224 }
00225
00226 bool TCPLocation::fromXML(XMLNode* node) {
00227
00228 if (node == NULL)
00229 return false;
00230
00231 if (!node->getTag().equalsIgnoreCase("tcplocation"))
00232 return false;
00233
00234
00235
00236 addr = node->getAttribute("address");
00237 port = node->getAttribute("port").toInt();
00238 name = node->getAttribute("name");
00239 regID = node->getAttribute("regid");
00240 procid = node->getAttribute("procid");
00241 pCommunicator = (void*) node->getAttribute("pointer").toPointer();
00242 netProtocolName = node->getAttribute("netprotocol");
00243 authentication = node->getAttribute("auth");
00244 additional = node->getAttribute("additional");
00245
00246 setIPAddress(node->getAttribute("ipnumber"));
00247
00248 return true;
00249 }
00250
00251 bool TCPLocation::setIPAddress(JString ip) {
00252
00253 ipnumber.a = 0;
00254 ipnumber.b = 0;
00255 ipnumber.c = 0;
00256 ipnumber.d = 0;
00257
00258 if (ip.length() > 0) {
00259 Collection oc = ip.split(".");
00260
00261 if (oc.getCount() == 4) {
00262 ipnumber.a = (unsigned char) oc.get(0).toInt();
00263 ipnumber.b = (unsigned char) oc.get(1).toInt();
00264 ipnumber.c = (unsigned char) oc.get(2).toInt();
00265 ipnumber.d = (unsigned char) oc.get(3).toInt();
00266 }
00267 }
00268
00269 return true;
00270 }
00271
00272 JString TCPLocation::getIPAddress() const {
00273 JString ip = JString((int) ipnumber.a);
00274 ip += JString(".") + JString((int) ipnumber.b);
00275 ip += JString(".") + JString((int) ipnumber.c);
00276 ip += JString(".") + JString((int) ipnumber.d);
00277 return ip;
00278 }
00279
00280
00281
00282
00283 }