- Timestamp:
- 11/24/11 14:45:29 (8 years ago)
- Location:
- trunk/legacy/jvmlink/src/main/c++
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/legacy/jvmlink/src/main/c++/JVMLinkClient.cpp
r3834 r7812 31 31 */ 32 32 33 #ifdef _WIN32 33 34 #include "stdafx.h" 35 #include <windows.h> 36 #else 37 #include <sys/types.h> 38 #include <sys/wait.h> 39 #include <sys/socket.h> 40 #include <unistd.h> 41 #include <netdb.h> 42 #endif 43 34 44 #include "JVMLinkClient.h" 35 #include <Windows.h> 45 46 #include <iostream> 47 #include <string> 48 #include <string.h> 49 #include <sstream> 50 #include <stdlib.h> 36 51 37 52 #define DEFAULT_PORT 20345 … … 49 64 // -- Public API methods -- 50 65 51 void JVMLinkClient::startJava(int arg_port, CString classpath) { 52 port = arg_port == NULL ? DEFAULT_PORT : arg_port; 53 CString command; 66 void JVMLinkClient::startJava(int arg_port, std::string classpath) { 67 port = arg_port == 0 ? DEFAULT_PORT : arg_port; 68 std::stringstream tmpportstr; 69 tmpportstr << port; 70 const std::string portstr = tmpportstr.str(); 71 72 #ifdef _WIN32 54 73 // NB: Toggle comments to control debugging output for the server. 55 co mmand.Format("-cp %s loci.jvmlink.JVMLinkServer %d", classpath, port);56 //co mmand.Format("-cp %s loci.jvmlink.JVMLinkServer -debug %d", classpath, port);74 const std::string command = "-cp " + classpath + " loci.jvmlink.JVMLinkServer " + portstr; 75 //const std::string command = "-cp " + classpath + " loci.jvmlink.JVMLinkServer -debug " + portstr; 57 76 debug("java " << command); 58 77 ShellExecute(NULL, "open", "javaw.exe" , command, "", SW_SHOW); 59 78 //ShellExecute(NULL, "open", "java.exe" , command, "", SW_SHOW); 79 #else 80 pid_t vProcID = vfork(); 81 if (vProcID == 0) { 82 // executed by child process 83 execlp("java", "java", "-cp", classpath.c_str(), "loci.jvmlink.JVMLinkServer", portstr.c_str(), "-debug", (char*)NULL); 84 //execlp("java", "java", "-cp", classpath.c_str(), "loci.jvmlink.JVMLinkServer", portstr.c_str(), (char*)NULL); 85 86 std::cerr << "Error: Child failed to execute process." << std::endl; 87 _exit(1); 88 } else if (vProcID < 0) { 89 // failed to fork 90 debug("Error: Failed to fork, will exit now."); 91 exit(1); 92 } else { 93 // Code only executed by parent process 94 // TODO: check if the forked child is alive and working 95 } 96 #endif 60 97 } 61 98 … … 66 103 67 104 JVMLinkClient::ConnectionCode JVMLinkClient::establishConnection() { 105 const std::string servername = "127.0.0.1"; 106 107 #ifdef _WIN32 68 108 WSADATA wsaData; 69 109 struct hostent *hp; 70 110 unsigned int addr; 71 111 struct sockaddr_in server; 72 CString servername = "127.0.0.1";73 112 74 113 int wsaret=WSAStartup(0x101,&wsaData); … … 100 139 closesocket(conn); 101 140 debug("No server response on port " << port); 102 return RESPONSE_ERR; 103 } 141 return RESPONSE_ERR; 142 } 143 #else 144 debug("starting to create socket"); 145 struct addrinfo hints; 146 struct addrinfo *res; 147 148 memset(&hints, 0, sizeof hints); 149 hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever 150 hints.ai_socktype = SOCK_STREAM; 151 hints.ai_flags = AI_PASSIVE; // fill in my IP for me 152 153 // do the lookup 154 std::stringstream tmpportstr; 155 tmpportstr << port; 156 const std::string portstr = tmpportstr.str(); 157 getaddrinfo(servername.c_str(), portstr.c_str(), &hints, &res); 158 159 // TODO: should do error-checking on getaddrinfo(), and walk 160 // the "res" linked list looking for valid entries instead of just 161 // assuming the first one is good 162 conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 163 if (conn < 0) return SOCKET_ERR; 164 165 debug("finished to create socket"); 166 167 if (connect(conn, res->ai_addr, res->ai_addrlen) < 0) { 168 close(conn); 169 debug("No server response on port " << port); 170 return RESPONSE_ERR; 171 } 172 #endif 173 104 174 debug("Connected to server: " << servername); 105 175 return CONNECTION_SUCCESS; … … 108 178 int JVMLinkClient::closeConnection() { 109 179 debug("Closing connection"); 180 #ifdef _WIN32 110 181 shutdown(conn, SD_SEND); 111 182 closesocket(conn); … … 113 184 WSACleanup(); 114 185 debug("De-initialized WinSock"); 186 #else 187 close(conn); 188 conn = 0; 189 #endif 115 190 return CONNECTION_SUCCESS; 116 191 } 117 192 118 JVMLinkObject* JVMLinkClient::getVar( CString name) {193 JVMLinkObject* JVMLinkClient::getVar(std::string name) { 119 194 debug("getVar: requesting " << name); 120 195 JVMLinkObject* obj = new JVMLinkObject(name); … … 126 201 obj->length = readInt(); 127 202 if (obj->insideType == STRING_TYPE) { 128 CString* s = new CString[obj->length];203 std::string* s = new std::string[obj->length]; 129 204 for (int i=0; i<obj->length; i++) s[i] = *readString(); 130 205 obj->data = s; … … 136 211 debug("getVar: got array: length=" << obj->length << ", type=" << obj->insideType); 137 212 } 138 else if (obj->type == STRING_TYPE) { 213 else if (obj->type == STRING_TYPE) { 139 214 obj->data = readString(); 140 215 obj->size = 0; … … 156 231 } 157 232 158 void JVMLinkClient::exec( CString command) {233 void JVMLinkClient::exec(std::string command) { 159 234 debug("exec: " << command); 160 235 sendInt(EXEC_CMD); … … 170 245 sendInt(obj->length); 171 246 if (obj->insideType == STRING_TYPE) { 172 CString* s = (CString*) obj->data;247 std::string* s = (std::string*) obj->data; 173 248 for (int i=0; i<obj->length; i++) { 174 249 sendMessage(s[i]); … … 185 260 } 186 261 else { 187 if (obj->type == STRING_TYPE) sendMessage(*( CString*) obj->data);262 if (obj->type == STRING_TYPE) sendMessage(*(std::string*) obj->data); 188 263 else send(conn, (char*) obj->data, obj->size, 0); 189 264 } 190 265 } 191 266 192 void JVMLinkClient::setVar( CString argname, int obj) {267 void JVMLinkClient::setVar(std::string argname, int obj) { 193 268 debug("setVar: " << argname << " = " << obj << " (int)"); 194 269 JVMLinkObject* jvmObj = new JVMLinkObject(argname, INT_TYPE, &obj); … … 197 272 } 198 273 199 void JVMLinkClient::setVar( CString argname, int* obj, int length) {274 void JVMLinkClient::setVar(std::string argname, int* obj, int length) { 200 275 debug("setVar: " << argname << " (int array)"); 201 276 JVMLinkObject* jvmObj = new JVMLinkObject(argname, INT_TYPE, length, obj); … … 204 279 } 205 280 206 void JVMLinkClient::setVar( CString argname, CString* obj) {281 void JVMLinkClient::setVar(std::string argname, std::string* obj) { 207 282 debug("setVar: " << argname << " = " << obj << " (string)"); 208 283 JVMLinkObject* jvmObj = new JVMLinkObject(argname, STRING_TYPE, obj); … … 211 286 } 212 287 213 void JVMLinkClient::setVar( CString argname, CString* obj, int length) {288 void JVMLinkClient::setVar(std::string argname, std::string* obj, int length) { 214 289 debug("setVar: " << argname << " (string array)"); 215 290 JVMLinkObject* jvmObj = new JVMLinkObject(argname, STRING_TYPE, length, obj); … … 218 293 } 219 294 220 void JVMLinkClient::setVar( CString argname, char obj) {295 void JVMLinkClient::setVar(std::string argname, char obj) { 221 296 debug("setVar: " << argname << " = " << obj << " (char)"); 222 297 JVMLinkObject* jvmObj = new JVMLinkObject(argname, CHAR_TYPE, &obj); … … 225 300 } 226 301 227 void JVMLinkClient::setVar( CString argname, char* obj, int length) {302 void JVMLinkClient::setVar(std::string argname, char* obj, int length) { 228 303 debug("setVar: " << argname << " (char array)"); 229 304 JVMLinkObject* jvmObj = new JVMLinkObject(argname, CHAR_TYPE, length, obj); … … 232 307 } 233 308 234 void JVMLinkClient::setVar( CString argname, Byte obj) {309 void JVMLinkClient::setVar(std::string argname, Byte obj) { 235 310 debug("setVar: " << argname << " = " << obj.data << " (byte)"); 236 311 JVMLinkObject* jvmObj = new JVMLinkObject(argname, BYTE_TYPE, &obj); … … 239 314 } 240 315 241 void JVMLinkClient::setVar( CString argname, Byte* obj, int length) {316 void JVMLinkClient::setVar(std::string argname, Byte* obj, int length) { 242 317 debug("setVar: " << argname << " (byte array)"); 243 318 JVMLinkObject* jvmObj = new JVMLinkObject(argname, BYTE_TYPE, length, obj); … … 246 321 } 247 322 248 void JVMLinkClient::setVar( CString argname, float obj) {323 void JVMLinkClient::setVar(std::string argname, float obj) { 249 324 debug("setVar: " << argname << " = " << obj << " (float)"); 250 325 JVMLinkObject* jvmObj = new JVMLinkObject(argname, FLOAT_TYPE, &obj); … … 253 328 } 254 329 255 void JVMLinkClient::setVar( CString argname, float* obj, int length) {330 void JVMLinkClient::setVar(std::string argname, float* obj, int length) { 256 331 debug("setVar: " << argname << " (float array)"); 257 332 JVMLinkObject* jvmObj = new JVMLinkObject(argname, FLOAT_TYPE, length, obj); … … 260 335 } 261 336 262 void JVMLinkClient::setVar( CString argname, bool obj) {337 void JVMLinkClient::setVar(std::string argname, bool obj) { 263 338 debug("setVar: " << argname << " = " << obj << " (bool)"); 264 339 JVMLinkObject* jvmObj = new JVMLinkObject(argname, BOOL_TYPE, &obj); … … 267 342 } 268 343 269 void JVMLinkClient::setVar( CString argname, bool* obj, int length) {344 void JVMLinkClient::setVar(std::string argname, bool* obj, int length) { 270 345 debug("setVar: " << argname << " (bool array)"); 271 346 JVMLinkObject* jvmObj = new JVMLinkObject(argname, BOOL_TYPE, length, obj); … … 274 349 } 275 350 276 void JVMLinkClient::setVar( CString argname, double obj) {351 void JVMLinkClient::setVar(std::string argname, double obj) { 277 352 debug("setVar: " << argname << " = " << obj << " (double)"); 278 353 JVMLinkObject* jvmObj = new JVMLinkObject(argname, DOUBLE_TYPE, &obj); … … 281 356 } 282 357 283 void JVMLinkClient::setVar( CString argname, double* obj, int length) {358 void JVMLinkClient::setVar(std::string argname, double* obj, int length) { 284 359 debug("setVar: " << argname << " (double array)"); 285 360 JVMLinkObject* jvmObj = new JVMLinkObject(argname, DOUBLE_TYPE, length, obj); … … 288 363 } 289 364 290 void JVMLinkClient::setVar( CString argname, long long obj) {365 void JVMLinkClient::setVar(std::string argname, long long obj) { 291 366 debug("setVar: " << argname << " = " << obj << " (long)"); 292 367 JVMLinkObject* jvmObj = new JVMLinkObject(argname, LONG_TYPE, &obj); … … 295 370 } 296 371 297 void JVMLinkClient::setVar( CString argname, long long* obj, int length) {372 void JVMLinkClient::setVar(std::string argname, long long* obj, int length) { 298 373 debug("setVar: " << argname << " (long array)"); 299 374 JVMLinkObject* jvmObj = new JVMLinkObject(argname, LONG_TYPE, length, obj); … … 302 377 } 303 378 304 void JVMLinkClient::setVar( CString argname, short obj) {379 void JVMLinkClient::setVar(std::string argname, short obj) { 305 380 debug("setVar: " << argname << " = " << obj << " (short)"); 306 381 JVMLinkObject* jvmObj = new JVMLinkObject(argname, SHORT_TYPE, &obj); … … 309 384 } 310 385 311 void JVMLinkClient::setVar( CString argname, short* obj, int length) {386 void JVMLinkClient::setVar(std::string argname, short* obj, int length) { 312 387 debug("setVar: " << argname << " (short array)"); 313 388 JVMLinkObject* jvmObj = new JVMLinkObject(argname, SHORT_TYPE, length, obj); … … 316 391 } 317 392 318 void JVMLinkClient::setVarNull( CString argname) {393 void JVMLinkClient::setVarNull(std::string argname) { 319 394 debug("setVarNull: " << argname); 320 395 JVMLinkObject* jvmObj = new JVMLinkObject(argname, NULL_TYPE, NULL); 321 396 setVar(jvmObj); 322 delete jvmObj; 397 delete jvmObj; 323 398 } 324 399 325 400 // -- Private methods -- 326 401 327 void JVMLinkClient::sendMessage( CString message) {402 void JVMLinkClient::sendMessage(std::string message) { 328 403 int sent = 0; 329 c har* buf = (char*) (LPCTSTR) message;330 int total = message. GetLength();404 const char* buf = message.c_str(); 405 int total = message.length(); 331 406 sendInt(total); 332 407 while (sent < total) sent += send(conn, buf + sent, total - sent, 0); … … 349 424 } 350 425 351 CString* JVMLinkClient::readString() {426 std::string* JVMLinkClient::readString() { 352 427 int read = 0; 353 428 int total = readInt(); … … 355 430 while (read < total) read += recv(conn, buf + read, total - read, 0); 356 431 buf[total] = '\0'; 357 return new CString(buf);358 } 432 return new std::string(buf); 433 } -
trunk/legacy/jvmlink/src/main/c++/JVMLinkClient.h
r3834 r7812 34 34 #include "JVMLinkObject.h" 35 35 36 #include <string> 37 36 38 // NB: Toggle comments to control debugging output for the client. 37 39 #define debug(msg) ((void)0) … … 42 44 private: 43 45 int port; 46 #ifdef _WIN32 44 47 SOCKET conn; 48 #else 49 int conn; 50 #endif 45 51 46 void sendMessage( CString);52 void sendMessage(std::string); 47 53 void* readMessage(int); 48 54 void sendInt(int); 49 55 int readInt(); 50 CString* readString();56 std::string* readString(); 51 57 52 58 public: … … 60 66 61 67 JVMLinkClient(); 62 void startJava(int, CString);68 void startJava(int, std::string); 63 69 void shutJava(); 64 70 ConnectionCode establishConnection(); 65 71 int closeConnection(); 66 JVMLinkObject* getVar( CString);72 JVMLinkObject* getVar(std::string); 67 73 void setVar(JVMLinkObject*); 68 void setVar( CString, int);69 void setVar( CString, int*, int);70 void setVar( CString, CString*);71 void setVar( CString, CString*, int);72 void setVar( CString, char);73 void setVar( CString, char*, int);74 void setVar( CString, Byte);75 void setVar( CString, Byte*, int);76 void setVar( CString, float);77 void setVar( CString, float*, int);78 void setVar( CString, bool);79 void setVar( CString, bool*, int);80 void setVar( CString, double);81 void setVar( CString, double*, int);82 void setVar( CString, long long);83 void setVar( CString, long long*, int);84 void setVar( CString, short);85 void setVar( CString, short*, int);86 void setVarNull( CString);87 void exec( CString);74 void setVar(std::string, int); 75 void setVar(std::string, int*, int); 76 void setVar(std::string, std::string*); 77 void setVar(std::string, std::string*, int); 78 void setVar(std::string, char); 79 void setVar(std::string, char*, int); 80 void setVar(std::string, Byte); 81 void setVar(std::string, Byte*, int); 82 void setVar(std::string, float); 83 void setVar(std::string, float*, int); 84 void setVar(std::string, bool); 85 void setVar(std::string, bool*, int); 86 void setVar(std::string, double); 87 void setVar(std::string, double*, int); 88 void setVar(std::string, long long); 89 void setVar(std::string, long long*, int); 90 void setVar(std::string, short); 91 void setVar(std::string, short*, int); 92 void setVarNull(std::string); 93 void exec(std::string); 88 94 89 95 ~JVMLinkClient(void); -
trunk/legacy/jvmlink/src/main/c++/JVMLinkObject.cpp
r3834 r7812 34 34 #include "JVMLinkObject.h" 35 35 36 JVMLinkObject::JVMLinkObject( CString name) {36 JVMLinkObject::JVMLinkObject(std::string name) { 37 37 this->name = name; 38 38 } 39 39 40 40 // Constructor for single primitives 41 JVMLinkObject::JVMLinkObject( CString name, Type type, void* data) {41 JVMLinkObject::JVMLinkObject(std::string name, Type type, void* data) { 42 42 this->name = name; 43 43 this->size = getSize(type); … … 48 48 49 49 // Constructor for arrays 50 JVMLinkObject::JVMLinkObject( CString name, Type type, int length, void* data) {50 JVMLinkObject::JVMLinkObject(std::string name, Type type, int length, void* data) { 51 51 this->name = name; 52 52 this->size = getSize(type); … … 72 72 } 73 73 74 CString* JVMLinkObject::getDataAsString() {75 CString* retval = (CString*) data;74 std::string* JVMLinkObject::getDataAsString() { 75 std::string* retval = (std::string*) data; 76 76 return retval; 77 77 } 78 78 79 CString* JVMLinkObject::getDataAsStringArray() {80 CString* retval = (CString*) data;79 std::string* JVMLinkObject::getDataAsStringArray() { 80 std::string* retval = (std::string*) data; 81 81 return retval; 82 82 } -
trunk/legacy/jvmlink/src/main/c++/JVMLinkObject.h
r3834 r7812 33 33 #pragma once 34 34 35 #include <string> 36 #include <vector> 37 38 35 39 enum Command { 36 40 BYTE_ORDER_CMD = 0, … … 64 68 65 69 public: 66 JVMLinkObject( CString);67 JVMLinkObject( CString, Type, void*);68 JVMLinkObject( CString, Type, int, void*);70 JVMLinkObject(std::string); 71 JVMLinkObject(std::string, Type, void*); 72 JVMLinkObject(std::string, Type, int, void*); 69 73 70 74 ~JVMLinkObject(void); 71 75 72 CString name;76 std::string name; 73 77 int size, length; 74 78 Type type, insideType; … … 77 81 int getDataAsInt(); 78 82 int* getDataAsIntArray(); 79 CString* getDataAsString();80 CString* getDataAsStringArray();83 std::string* getDataAsString(); 84 std::string* getDataAsStringArray(); 81 85 char getDataAsChar(); 82 86 char* getDataAsCharArray(); -
trunk/legacy/jvmlink/src/main/c++/TestC2.cpp
r3834 r7812 31 31 */ 32 32 33 #include "stdafx.h" 33 #ifdef _WIN32 34 #include <stdafx.h> 35 #else 36 #include <unistd.h> // for usleep() 37 #endif 38 34 39 #include "JVMLinkClient.h" 40 35 41 #include <ctime> // for time() 36 42 #include <cstdlib> // for srand() and rand() 43 #include <iostream> // for cout and cerr 44 #include <stdio.h> // for getchar 37 45 38 46 bool randomBool() { … … 95 103 } 96 104 97 void printStrings( CString* values, int len) {105 void printStrings(std::string* values, int len) { 98 106 for (int i=0; i<len; i++) { 99 107 std::cout << "\t" << values[i] << std::endl; … … 102 110 103 111 // Tests the JVMLink API. 104 int _tmain(int argc, _TCHAR* argv[])112 int main(int argc, char* argv[]) 105 113 { 106 114 JVMLinkClient *p = new JVMLinkClient(); 107 115 p->startJava(20345, "jvmlink.jar"); 108 while (p->establishConnection() != JVMLinkClient::CONNECTION_SUCCESS) Sleep(250); 116 while (p->establishConnection() != JVMLinkClient::CONNECTION_SUCCESS) { 117 #ifdef _WIN32 118 Sleep(250); 119 #else 120 usleep(250 * 1000); 121 #endif 122 } 109 123 110 124 srand((int) time(0)); … … 167 181 std::cout << "TestC2: getVar: myShort == " << jvmShort->getDataAsShort() << std::endl; 168 182 169 // CString variables170 CString myString = "<<The quick brown fox jumps over the lazy dog.>>";183 // std::string variables 184 std::string myString = "<<The quick brown fox jumps over the lazy dog.>>"; 171 185 p->setVar("myString", &myString); 172 186 std::cout << "TestC2: setVar: myString -> " << myString << std::endl; … … 208 222 } 209 223 210 CString* myStrings = new CString[6];224 std::string* myStrings = new std::string[6]; 211 225 myStrings[0] = "\"There was an Old Man with a beard,"; 212 226 myStrings[1] = "Who said, 'It is just as I feared!"; … … 296 310 std::cout << " ]" << std::endl; 297 311 298 // CString arrays312 // std::string arrays 299 313 p->setVar("myStrings", myStrings, 6); 300 314 std::cout << "TestC2: setVar: myStrings -> [" << std::endl; … … 358 372 delete largeBytes; 359 373 360 fprintf(stdout, "\n\nPress enter to shut down the server and exit...\n"); 374 std::cout << std::endl << std::endl << "Press enter to shut down the server and exit..." << std::endl; 375 #ifdef _WIN32 361 376 _fgetchar(); 377 #else 378 getchar(); 379 #endif 362 380 363 381 // free Java resources -
trunk/legacy/jvmlink/src/main/c++/stdafx.h
r3685 r7812 3 3 // are changed infrequently 4 4 // 5 6 // this header is required for Windows only 7 #ifdef _WIN32 5 8 6 9 #if !defined(AFX_STDAFX_H__59F83989_2D03_11D6_AA7C_00C026A39668__INCLUDED_) … … 30 33 31 34 #endif // !defined(AFX_STDAFX_H__59F83989_2D03_11D6_AA7C_00C026A39668__INCLUDED_) 35 36 #endif // _WIN32
Note: See TracChangeset
for help on using the changeset viewer.