- Timestamp:
- Mar 29, 2012, 11:38:42 PM (12 years ago)
- Location:
- trunk/packages/vizservers/nanovis/socket
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanovis/socket/ClientSocket.cpp
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Implementation of the ClientSocket class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 17 13 #include "SocketException.h" 18 14 19 ClientSocket::ClientSocket ( std::string host, int port)15 ClientSocket::ClientSocket(std::string host, int port) 20 16 { 21 if ( ! Socket::create() ) 22 { 23 throw SocketException ( "Could not create client socket." ); 17 if (!Socket::create()) { 18 throw SocketException("Could not create client socket."); 24 19 } 25 20 26 if ( ! Socket::connect ( host, port ) ) 27 { 28 throw SocketException ( "Could not bind to port." ); 21 if (!Socket::connect(host, port)) { 22 throw SocketException("Could not bind to port."); 23 } 24 } 25 26 const ClientSocket& ClientSocket::operator <<(const std::string& s) const 27 { 28 if (!Socket::send(s)) { 29 throw SocketException("Could not write to socket."); 29 30 } 30 31 32 return *this; 31 33 } 32 34 33 34 const ClientSocket& ClientSocket::operator << ( const std::string& s ) const 35 const ClientSocket& ClientSocket::operator >>(std::string& s) const 35 36 { 36 if ( ! Socket::send ( s ) ) 37 { 38 throw SocketException ( "Could not write to socket." ); 37 if (!Socket::recv(s)) { 38 throw SocketException("Could not read from socket."); 39 39 } 40 40 41 return *this; 42 41 return *this; 43 42 } 44 43 44 void ClientSocket::set_non_blocking(bool val) 45 { 46 Socket::set_non_blocking(val); 47 } 45 48 46 const ClientSocket& ClientSocket::operator >> ( std::string& s) const49 bool ClientSocket::send(char* s, int size) const 47 50 { 48 if ( ! Socket::recv ( s ) )49 {50 throw SocketException ( "Could not read from socket.");51 bool ret = Socket::send(s, size); 52 if (!ret) { 53 throw SocketException("Could not write to socket."); 51 54 } 52 55 53 return *this;56 return ret; 54 57 } 55 58 56 void ClientSocket::set_non_blocking(bool val){ 57 Socket::set_non_blocking(val); 58 } 59 60 61 bool ClientSocket::send (char* s, int size) const 59 int ClientSocket::recv(char *s, int size) const 62 60 { 63 bool ret = Socket::send (s, size); 64 if (!ret) 65 { 66 throw SocketException ( "Could not write to socket." ); 61 bool ret = Socket::recv (s, size); 62 if (!ret) { 63 throw SocketException("Could not read from socket."); 67 64 } 68 65 69 return ret;66 return ret; 70 67 } 71 72 73 int ClientSocket::recv ( char* s, int size) const74 {75 bool ret = Socket::recv (s, size);76 if (!ret)77 {78 throw SocketException ( "Could not read from socket." );79 }80 81 return ret;82 } -
trunk/packages/vizservers/nanovis/socket/ClientSocket.h
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Definition of the ClientSocket class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef C lientSocket_class17 #define C lientSocket_class12 #ifndef CLIENTSOCKET_H 13 #define CLIENTSOCKET_H 18 14 19 15 #include "Socket.h" … … 21 17 class ClientSocket : private Socket 22 18 { 23 public: 19 public: 20 ClientSocket(std::string host, int port); 21 virtual ~ClientSocket() 22 {} 24 23 25 ClientSocket ( std::string host, int port );26 virtual ~ClientSocket(){};24 const ClientSocket& operator <<(const std::string&) const; 25 const ClientSocket& operator >>(std::string&) const; 27 26 28 const ClientSocket& operator << ( const std::string&) const;29 const ClientSocket& operator >> ( std::string&) const;27 bool send(char* s, int size) const; 28 int recv(char* s, int size) const; 30 29 31 bool send(char* s, int size) const; 32 int recv( char* s, int size) const; 33 34 void set_non_blocking(bool val); 30 void set_non_blocking(bool val); 35 31 }; 36 32 37 38 33 #endif -
trunk/packages/vizservers/nanovis/socket/RenderClient.cpp
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * RenderClient.cpp: client connecting to the RenderServer 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #include <GL/freeglut.h>17 12 #include <stdlib.h> 18 13 #include <sstream> … … 22 17 #include <assert.h> 23 18 19 #include <GL/glut.h> 20 24 21 #include "RenderClient.h" 25 22 #include "Event.h" … … 31 28 32 29 int width, height; 33 RenderClient::RenderClient(){} 34 35 RenderClient::RenderClient(std::string& remote_host, int port_num){36 //screen_size = sizeof(float)*4*512*512; //float 37 screen_size = 3*width*height; //unsigned byte 38 screen_buffer = new char[screen_size]; 39 40 socket_num = port_num; 41 host = remote_host; 42 43 //init socket server 44 std::cout << "client running....\n";45 46 try 47 {48 // Create the socket49 client_socket = new ClientSocket(host, socket_num); 50 //client_socket->set_non_blocking(true); 51 fprintf(stderr, "client socket initialized\n"); 52 }53 catch ( SocketException& e )54 {55 std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";56 }57 }58 59 void RenderClient::send(std::string& msg){ 60 try 61 62 //printf("client: send()\n"); 63 *client_socket << msg;64 } 65 catch ( SocketException& ) {}66 }67 68 void RenderClient::receive(std::string& msg){ 69 std::string buf=""; 70 try 71 { 72 fprintf(stderr, "client: receive()\n"); 73 fflush(stdout);74 *client_socket >> buf;75 msg =buf;76 } 77 catch ( SocketException& ) {}78 }79 80 81 bool RenderClient::receive(char* s, int size) {82 try 83 {84 85 86 87 } 88 catch ( SocketException& ) { return false;}89 }90 91 92 bool RenderClient::send(char* s, int size) {93 try 94 {95 96 97 98 } 99 catch ( SocketException& ) { return false;}100 }101 30 31 RenderClient::RenderClient() 32 { 33 } 34 35 RenderClient::RenderClient(std::string& remote_host, int port_num) 36 { 37 //screen_size = sizeof(float)*4*512*512; //float 38 screen_size = 3*width*height; //unsigned byte 39 screen_buffer = new char[screen_size]; 40 41 socket_num = port_num; 42 host = remote_host; 43 44 //init socket server 45 std::cout << "client running....\n"; 46 47 try { 48 // Create the socket 49 client_socket = new ClientSocket(host, socket_num); 50 //client_socket->set_non_blocking(true); 51 fprintf(stderr, "client socket initialized\n"); 52 } catch (SocketException& e) { 53 std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; 54 } 55 } 56 57 void RenderClient::send(std::string& msg) 58 { 59 try { 60 //printf("client: send()\n"); 61 *client_socket << msg; 62 } catch (SocketException&) { 63 } 64 } 65 66 void RenderClient::receive(std::string& msg) 67 { 68 std::string buf=""; 69 try { 70 fprintf(stderr, "client: receive()\n"); 71 fflush(stdout); 72 *client_socket >> buf; 73 msg = buf; 74 } catch (SocketException&){ 75 } 76 } 77 78 bool RenderClient::receive(char* s, int size) 79 { 80 try { 81 //printf("client: receive()\n"); 82 bool ret = client_socket->recv(s, size); 83 return true; 84 } catch (SocketException&) { 85 return false; 86 } 87 } 88 89 bool RenderClient::send(char* s, int size) 90 { 91 try { 92 //printf("client: receive()\n"); 93 bool ret = client_socket->send(s, size); 94 return true; 95 } catch (SocketException&) { 96 return false; 97 } 98 } 102 99 103 100 RenderClient* client; … … 120 117 void resize(int _width, int _height) 121 118 { 122 delete[] client->screen_buffer;123 client->screen_size = 3*_width*_height;124 client->screen_buffer = new char[client->screen_size];119 delete[] client->screen_buffer; 120 client->screen_size = 3*_width*_height; 121 client->screen_buffer = new char[client->screen_size]; 125 122 126 123 width = _width; … … 141 138 } 142 139 143 144 void draw_buttons(){145 glPushMatrix();146 147 glViewport(0, 0, width, height);148 glMatrixMode(GL_PROJECTION);149 glLoadIdentity();150 glOrtho(0, width, 0, height, -0.09, 1);151 glMatrixMode(GL_MODELVIEW);152 glLoadIdentity();153 154 glDisable(GL_BLEND);155 glEnable(GL_BLEND);156 157 //loaddata button158 glColor4f(0.5, 0.5, 0.5, 0.5);159 glBegin(GL_QUADS);140 void draw_buttons() 141 { 142 glPushMatrix(); 143 144 glViewport(0, 0, width, height); 145 glMatrixMode(GL_PROJECTION); 146 glLoadIdentity(); 147 glOrtho(0, width, 0, height, -0.09, 1); 148 glMatrixMode(GL_MODELVIEW); 149 glLoadIdentity(); 150 151 glDisable(GL_BLEND); 152 glEnable(GL_BLEND); 153 154 //loaddata button 155 glColor4f(0.5, 0.5, 0.5, 0.5); 156 glBegin(GL_QUADS); 160 157 glVertex2d(10, 10); 161 158 glVertex2d(60, 10); 162 159 glVertex2d(60, 40); 163 160 glVertex2d(10, 40); 164 glEnd(); 165 166 167 glDisable(GL_BLEND); 168 glPopMatrix(); 169 } 170 171 161 glEnd(); 162 163 glDisable(GL_BLEND); 164 glPopMatrix(); 165 } 172 166 173 167 void display() 174 168 { 175 169 176 177 if(loaded){ 178 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 179 //glDrawPixels(width, height, GL_RGBA, GL_FLOAT, client->screen_buffer); 180 //bzero(client->screen_buffer, client->screen_size); 181 glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, client->screen_buffer); 182 glFlush(); 183 glutSwapBuffers(); 184 return; 185 } 186 170 if (loaded) { 171 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 172 //glDrawPixels(width, height, GL_RGBA, GL_FLOAT, client->screen_buffer); 173 //bzero(client->screen_buffer, client->screen_size); 174 glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, client->screen_buffer); 175 glFlush(); 176 glutSwapBuffers(); 177 return; 178 } 187 179 188 180 render_done = false; … … 221 213 } 222 214 223 224 215 void key(unsigned char key, int x, int y) 225 216 { … … 229 220 std::string msg2; 230 221 231 switch (key) 232 { 233 case 27 : 234 case 'q': 235 exit(0); 236 break; 237 238 case 'l': 239 cerr << "client: load" << endl; 240 //msgstream << "command=0" << endl; 241 msgstream << "load" << endl; 242 msg = msgstream.str(); 243 client->send(msg); 244 loaded = true; 245 break; 246 247 case 'o': 248 msgstream << "command=" << "1" << endl; 249 msg = msgstream.str(); 250 client->send(msg); 251 break; 252 253 case '+': 254 msgstream << "command=" << "4" << endl; 255 msg = msgstream.str(); 256 client->send(msg); 257 break; 258 259 case '-': 260 msgstream << "command=" << "5" << endl; 261 msg = msgstream.str(); 262 client->send(msg); 263 break; 264 265 case 'x': 266 //msgstream << "and=" << "5" << endl; 267 msgstream << "cut x " << "off" << endl; 268 msg = msgstream.str(); 269 cerr << "client: " << msg << endl; 270 client->send(msg); 271 break; 272 273 case 'X': 274 msgstream << "cut x " << " on" << endl; 275 msg = msgstream.str(); 276 cerr << "client: " << msg << endl; 277 client->send(msg); 278 break; 279 280 case 'y': 281 msgstream << "cut y " << "off" << endl; 282 msg = msgstream.str(); 283 cerr << "client: " << msg << endl; 284 client->send(msg); 285 break; 286 287 case 'Y': 288 msgstream << "cut y " << "on" << endl; 289 msg = msgstream.str(); 290 cerr << "client: " << msg << endl; 291 client->send(msg); 292 break; 293 294 case 'z': 295 msgstream << "cut z " << "off" << endl; 296 msg = msgstream.str(); 297 cerr << "client: " << msg << endl; 298 client->send(msg); 299 break; 300 301 case 'Z': 302 msgstream << "cut z " << "on" << endl; 303 msg = msgstream.str(); 304 cerr << "client: " << msg << endl; 305 client->send(msg); 306 break; 307 308 default: 309 return; 310 } 311 222 switch (key) { 223 case 27 : 224 case 'q': 225 exit(0); 226 break; 227 228 case 'l': 229 cerr << "client: load" << endl; 230 //msgstream << "command=0" << endl; 231 msgstream << "load" << endl; 232 msg = msgstream.str(); 233 client->send(msg); 234 loaded = true; 235 break; 236 237 case 'o': 238 msgstream << "command=" << "1" << endl; 239 msg = msgstream.str(); 240 client->send(msg); 241 break; 242 243 case '+': 244 msgstream << "command=" << "4" << endl; 245 msg = msgstream.str(); 246 client->send(msg); 247 break; 248 249 case '-': 250 msgstream << "command=" << "5" << endl; 251 msg = msgstream.str(); 252 client->send(msg); 253 break; 254 255 case 'x': 256 //msgstream << "and=" << "5" << endl; 257 msgstream << "cut x " << "off" << endl; 258 msg = msgstream.str(); 259 cerr << "client: " << msg << endl; 260 client->send(msg); 261 break; 262 263 case 'X': 264 msgstream << "cut x " << " on" << endl; 265 msg = msgstream.str(); 266 cerr << "client: " << msg << endl; 267 client->send(msg); 268 break; 269 270 case 'y': 271 msgstream << "cut y " << "off" << endl; 272 msg = msgstream.str(); 273 cerr << "client: " << msg << endl; 274 client->send(msg); 275 break; 276 277 case 'Y': 278 msgstream << "cut y " << "on" << endl; 279 msg = msgstream.str(); 280 cerr << "client: " << msg << endl; 281 client->send(msg); 282 break; 283 284 case 'z': 285 msgstream << "cut z " << "off" << endl; 286 msg = msgstream.str(); 287 cerr << "client: " << msg << endl; 288 client->send(msg); 289 break; 290 291 case 'Z': 292 msgstream << "cut z " << "on" << endl; 293 msg = msgstream.str(); 294 cerr << "client: " << msg << endl; 295 client->send(msg); 296 break; 297 298 default: 299 return; 300 } 312 301 313 302 /* 314 for(int j=0; j<512; j=j+1){303 for(int j=0; j<512; j=j+1){ 315 304 cerr << "client read: " << j << endl; 316 305 client->receive(client->screen_buffer + j*4*512, 4*512); //unsigned byte 317 }306 } 318 307 */ 319 308 320 309 client->receive(client->screen_buffer, client->screen_size); //unsigned byte 321 310 322 311 //cin >> msg; 323 312 //strncpy(client->screen_buffer, msg.c_str(), 512*512*4); 324 313 325 314 display(); 326 315 //glutPostRedisplay(); … … 329 318 } 330 319 331 332 void update_rot(int delta_x, int delta_y){ 333 live_rot_x += delta_x; 334 live_rot_y += delta_y; 335 336 if(live_rot_x > 360.0) 337 live_rot_x -= 360.0; 338 else if(live_rot_x < -360.0) 339 live_rot_x += 360.0; 340 341 if(live_rot_y > 360.0) 342 live_rot_y -= 360.0; 343 else if(live_rot_y < -360.0) 344 live_rot_y += 360.0; 345 } 346 347 void update_trans(int delta_x, int delta_y, int delta_z){ 348 live_obj_x += delta_x*0.01; 349 live_obj_y += delta_y*0.01; 350 live_obj_z += delta_z*0.01; 351 } 352 353 bool test_in_box(int box_x0, int box_y0, int box_x1, int box_y1, int x, int y){ 354 return (box_x0<=x && box_x1>=x && box_y0<=y && box_y1>=y); 355 } 356 357 void mouse(int button, int state, int x, int y){ 358 if(button==GLUT_LEFT_BUTTON){ 359 left_last_x = x; 360 left_last_y = y; 361 362 if(state==GLUT_DOWN){ 363 left_down = true; 364 right_down = false; 365 } 366 else{ 367 left_down = false; 368 right_down = true; 369 } 370 } 371 else{ 372 right_last_x = x; 373 right_last_y = y; 374 375 if(state==GLUT_DOWN){ 376 left_down = false; 377 right_down = true; 378 } 379 else{ 380 left_down = true; 381 right_down = false; 382 } 383 } 384 } 385 386 void motion(int x, int y){ 387 cerr << "client: motion() " << endl; 388 389 if(render_done){ 390 int old_x, old_y; 391 392 if(left_down){ 393 old_x = left_last_x; 394 old_y = left_last_y; 395 } 396 else{ 397 old_x = right_last_x; 398 old_y = right_last_y; 399 } 400 401 int delta_x = x - old_x; 402 int delta_y = y - old_y; 403 404 //more coarse event handling 405 if(abs(delta_x)<5 && abs(delta_y)<5) 406 return; 407 408 if(left_down){ 409 left_last_x = x; 410 left_last_y = y; 411 412 update_rot(delta_y, delta_x); 413 std::stringstream msgstream; 414 //msgstream << "command=" << "2" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl; 415 msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl; 416 std::string msg; 417 msg = msgstream.str(); 418 std::cout << "client msg: " << msg <<"\n"; 419 //client->send(msg); 420 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 421 } 422 else{ 423 right_last_x = x; 424 right_last_y = y; 425 426 update_trans(0, 0, delta_y); 427 std::stringstream msgstream; 428 //msgstream << "command=" << "3" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl; 429 msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl; 430 std::string msg; 431 msg = msgstream.str(); 432 std::cout << "client msg: " << msg <<"\n"; 433 //client->send(msg); 434 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 435 } 436 320 void update_rot(int delta_x, int delta_y) 321 { 322 live_rot_x += delta_x; 323 live_rot_y += delta_y; 324 325 if (live_rot_x > 360.0) 326 live_rot_x -= 360.0; 327 else if (live_rot_x < -360.0) 328 live_rot_x += 360.0; 329 330 if (live_rot_y > 360.0) 331 live_rot_y -= 360.0; 332 else if (live_rot_y < -360.0) 333 live_rot_y += 360.0; 334 } 335 336 void update_trans(int delta_x, int delta_y, int delta_z) 337 { 338 live_obj_x += delta_x*0.01; 339 live_obj_y += delta_y*0.01; 340 live_obj_z += delta_z*0.01; 341 } 342 343 bool test_in_box(int box_x0, int box_y0, int box_x1, int box_y1, int x, int y) 344 { 345 return (box_x0<=x && box_x1>=x && box_y0<=y && box_y1>=y); 346 } 347 348 void mouse(int button, int state, int x, int y) 349 { 350 if (button == GLUT_LEFT_BUTTON) { 351 left_last_x = x; 352 left_last_y = y; 353 354 if (state == GLUT_DOWN) { 355 left_down = true; 356 right_down = false; 357 } else { 358 left_down = false; 359 right_down = true; 360 } 361 } else { 362 right_last_x = x; 363 right_last_y = y; 364 365 if (state == GLUT_DOWN) { 366 left_down = false; 367 right_down = true; 368 } else { 369 left_down = true; 370 right_down = false; 371 } 372 } 373 } 374 375 void motion(int x, int y) 376 { 377 cerr << "client: motion() " << endl; 378 379 if (render_done) { 380 int old_x, old_y; 381 382 if (left_down) { 383 old_x = left_last_x; 384 old_y = left_last_y; 385 } else { 386 old_x = right_last_x; 387 old_y = right_last_y; 388 } 389 390 int delta_x = x - old_x; 391 int delta_y = y - old_y; 392 393 //more coarse event handling 394 if (abs(delta_x)<5 && abs(delta_y)<5) 395 return; 396 397 if (left_down) { 398 left_last_x = x; 399 left_last_y = y; 400 401 update_rot(delta_y, delta_x); 402 std::stringstream msgstream; 403 //msgstream << "command=" << "2" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl; 404 msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl; 405 std::string msg; 406 msg = msgstream.str(); 407 std::cout << "client msg: " << msg <<"\n"; 408 //client->send(msg); 409 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 410 } else { 411 right_last_x = x; 412 right_last_y = y; 413 414 update_trans(0, 0, delta_y); 415 std::stringstream msgstream; 416 //msgstream << "command=" << "3" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl; 417 msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl; 418 std::string msg; 419 msg = msgstream.str(); 420 std::cout << "client msg: " << msg <<"\n"; 421 //client->send(msg); 422 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 423 } 424 425 client->receive(client->screen_buffer, client->screen_size); 426 427 /* 428 for (int j=0; j<512; j=j+4) { 429 unsigned int r,g,b,a; 430 r = client->screen_buffer[j]; 431 g = client->screen_buffer[j+1]; 432 b = client->screen_buffer[j+2]; 433 a = client->screen_buffer[j+3]; 434 //fprintf(stderr, "(%d %d %d %d) ", r,g,b,a); //unsigned byte 435 fprintf(stderr, "(%X %X %X %X) ", r,g,b,a); //unsigned byte 436 } 437 */ 438 439 display(); 440 cerr << "client: motion() done " << endl; 441 } 442 } 443 444 void idle() 445 { 446 /* 447 struct timespec ts; 448 ts.tv_sec = 0; 449 ts.tv_nsec = 100000000; 450 451 nanosleep(&ts, 0); 452 */ 453 454 //send requests 455 Event* cur = event[cur_event]; 456 std::stringstream msgstream; 457 std::string msg; 458 459 switch (cur->type) { 460 case 0: //rotate 461 msgstream << "camera " << cur->parameter[0] << " " 462 << cur->parameter[1] << " " 463 << cur->parameter[2] << " " << endl; 464 break; 465 466 case 1: //move 467 msgstream << "move " << cur->parameter[0] << " " 468 << cur->parameter[1] << " " 469 << cur->parameter[2] << " " << endl; 470 break; 471 472 case 2: //other 473 msgstream << "refresh " << cur->parameter[0] << " " 474 << cur->parameter[1] << " " 475 << cur->parameter[2] << " " << endl; 476 break; 477 478 default: 479 return; 480 } 481 482 msg = msgstream.str(); 483 //std::cout << "client msg: " << msg <<"\n"; 484 485 //start timer 486 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 437 487 client->receive(client->screen_buffer, client->screen_size); 438 439 /* 440 for(int j=0; j<512; j=j+4){ 441 unsigned int r,g,b,a; 442 r = client->screen_buffer[j]; 443 g = client->screen_buffer[j+1]; 444 b = client->screen_buffer[j+2]; 445 a = client->screen_buffer[j+3]; 446 //fprintf(stderr, "(%d %d %d %d) ", r,g,b,a); //unsigned byte 447 fprintf(stderr, "(%X %X %X %X) ", r,g,b,a); //unsigned byte 448 } 449 */ 450 451 display(); 452 cerr << "client: motion() done " << endl; 453 } 454 } 455 456 void idle(void) 457 { 458 /* 459 struct timespec ts; 460 ts.tv_sec = 0; 461 ts.tv_nsec = 100000000; 462 463 nanosleep(&ts, 0); 464 */ 465 466 //send requests 467 Event* cur = event[cur_event]; 468 std::stringstream msgstream; 469 std::string msg; 470 471 switch(cur->type){ 472 case 0: //rotate 473 msgstream << "camera " << cur->parameter[0] << " " 474 << cur->parameter[1] << " " 475 << cur->parameter[2] << " " << endl; 476 break; 477 478 case 1: //move 479 msgstream << "move " << cur->parameter[0] << " " 480 << cur->parameter[1] << " " 481 << cur->parameter[2] << " " << endl; 482 break; 483 484 case 2: //other 485 msgstream << "refresh " << cur->parameter[0] << " " 486 << cur->parameter[1] << " " 487 << cur->parameter[2] << " " << endl; 488 break; 489 490 default: 491 return; 492 } 493 494 msg = msgstream.str(); 495 //std::cout << "client msg: " << msg <<"\n"; 496 497 //start timer 498 client->send((char*) msg.c_str(), (int) strlen(msg.c_str())); 499 client->receive(client->screen_buffer, client->screen_size); 500 //end timer 501 488 //end timer 502 489 } 503 490 … … 512 499 const GLfloat high_shininess[] = { 100.0f }; 513 500 514 515 void init_client(char* host, char* port, char* file){ 516 517 //load the event file 518 FILE* fd = fopen(file, "r"); 519 //load 5000 events 520 for(int i=0; i<5000; i++){ 521 int type; 522 float param[3]; 523 fscanf(fd, "%d %f %f %f\n", &type, param, param+1, param+2); 524 event[i] = new Event(type, param, 0); 525 fprintf(stderr, "%d %f %f %f\n", type, param[0], param[1], param[2]); 526 } 527 fclose(fd); 528 529 //std::string host = "localhost"; 530 //hostname -i 531 //std::string host = "128.46.137.192"; 501 void init_client(char* host, char* port, char* file) 502 { 503 //load the event file 504 FILE* fd = fopen(file, "r"); 505 //load 5000 events 506 for (int i=0; i<5000; i++) { 507 int type; 508 float param[3]; 509 fscanf(fd, "%d %f %f %f\n", &type, param, param+1, param+2); 510 event[i] = new Event(type, param, 0); 511 fprintf(stderr, "%d %f %f %f\n", type, param[0], param[1], param[2]); 512 } 513 fclose(fd); 514 515 //std::string host = "localhost"; 516 //hostname -i 517 //std::string host = "128.46.137.192"; 532 518 533 std::string hostname = host;534 client = new RenderClient(hostname, atoi(port));535 536 //point stdin stdout to socket537 /*538 close(0);539 close(1);540 dup2(client->Socket::m_sock, 0);541 dup2(client->Socket::m_sock, 1);542 */519 std::string hostname = host; 520 client = new RenderClient(hostname, atoi(port)); 521 522 //point stdin stdout to socket 523 /* 524 close(0); 525 close(1); 526 dup2(client->Socket::m_sock, 0); 527 dup2(client->Socket::m_sock, 1); 528 */ 543 529 544 std::string msg1 = "hello";545 std::string msg2 = " ";546 547 //cout << msg1;548 //cin >> msg2;549 550 client->send(msg1);551 client->receive(msg2);530 std::string msg1 = "hello"; 531 std::string msg2 = " "; 532 533 //cout << msg1; 534 //cin >> msg2; 535 536 client->send(msg1); 537 client->receive(msg2); 552 538 553 cerr << "client: msg received - " << msg2 << endl; 554 cerr << "connection to server established" << endl; 555 } 556 557 558 559 void print_gl_info(){ 560 fprintf(stderr, "OpenGL vendor: %s %s\n", glGetString(GL_VENDOR), glGetString(GL_VERSION)); 561 fprintf(stderr, "OpenGL renderer: %s\n", glGetString(GL_RENDERER)); 562 } 563 564 565 void menu_cb(int entry){ 566 567 std::stringstream msgstream; 568 std::string msg; 569 std::string msg2; 570 switch(entry) 571 { 539 cerr << "client: msg received - " << msg2 << endl; 540 cerr << "connection to server established" << endl; 541 } 542 543 void print_gl_info() 544 { 545 fprintf(stderr, "OpenGL vendor: %s %s\n", glGetString(GL_VENDOR), glGetString(GL_VERSION)); 546 fprintf(stderr, "OpenGL renderer: %s\n", glGetString(GL_RENDERER)); 547 } 548 549 void menu_cb(int entry) 550 { 551 std::stringstream msgstream; 552 std::string msg; 553 std::string msg2; 554 switch (entry) { 572 555 case 0: 573 556 msgstream << "command=" << "0"; 574 575 576 577 557 msgstream >> msg; 558 client->send(msg); 559 560 client->receive(msg2); 578 561 579 580 562 loaded = true; 563 break; 581 564 582 565 case 1: 583 566 msgstream << "command=" << "1"; 584 585 586 587 588 567 msgstream >> msg; 568 client->send(msg); 569 570 client->receive(msg2); 571 break; 589 572 590 573 case 2: 591 574 msgstream << "command=" << "2"; 592 593 594 595 596 597 }575 msgstream >> msg; 576 client->send(msg); 577 578 client->receive(msg2); 579 break; 580 } 598 581 } 599 582 600 583 void help(const char *argv0) 601 584 { 602 fprintf(stderr,603 "Syntax: %s addr port eventfile\n",604 argv0);605 exit(1);585 fprintf(stderr, 586 "Syntax: %s addr port eventfile\n", 587 argv0); 588 exit(1); 606 589 } 607 590 … … 610 593 { 611 594 //parameters: hostip and port and event file 612 if(argc!=4) help(argv[0]); 595 if (argc!=4) 596 help(argv[0]); 613 597 614 598 width =512; height=512; … … 623 607 624 608 /* 625 glutCreateMenu(menu_cb);626 glutAddMenuEntry("load data ", 0);627 glutAddMenuEntry("toggle mode (on/off screen)", 1);628 glutAttachMenu(GLUT_RIGHT_BUTTON);609 glutCreateMenu(menu_cb); 610 glutAddMenuEntry("load data ", 0); 611 glutAddMenuEntry("toggle mode (on/off screen)", 1); 612 glutAttachMenu(GLUT_RIGHT_BUTTON); 629 613 */ 630 614 -
trunk/packages/vizservers/nanovis/socket/RenderClient.h
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * RenderClient.h: server with OpenRenderer engine 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef _RENDER_CLIENT_H_17 #define _RENDER_CLIENT_H_12 #ifndef RENDER_CLIENT_H 13 #define RENDER_CLIENT_H 18 14 19 15 #include <iostream> … … 23 19 #include "SocketException.h" 24 20 25 class RenderClient{ 26 21 class RenderClient 22 { 23 public: 24 RenderClient(); 25 RenderClient(std::string& remote_host, int port_num); 26 27 void send(std::string& msg); 28 void receive(std::string& msg); 29 30 bool receive(char *data, int size); 31 bool send(char *data, int size); 32 33 ClientSocket *client_socket; 34 char *screen_buffer; 35 int screen_size; //units of byte 36 27 37 private: 28 int socket_num; 29 std::string host; 30 31 public: 32 ClientSocket* client_socket; 33 char* screen_buffer; 34 int screen_size; //units of byte 35 RenderClient(); 36 RenderClient(std::string& remote_host, int port_num); 37 void send(std::string& msg); 38 void receive(std::string& msg); 39 40 bool receive(char* data, int size); 41 bool send(char* data, int size); 38 int socket_num; 39 std::string host; 42 40 }; 43 41 -
trunk/packages/vizservers/nanovis/socket/RenderServer.cpp
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * RenderServer.cpp: server with OpenRenderer engine 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 16 12 #include "RenderServer.h" 17 13 18 RenderServer::RenderServer(){} 19 20 RenderServer::RenderServer(int port_num){ 21 22 socket_num = port_num; 23 24 //init socket server 25 std::cout << "server up and running....\n"; 26 27 try 28 { 29 // Create the socket 30 server_socket = new ServerSocket(socket_num); 31 //server_socket->set_non_blocking(true); 32 33 } 34 catch ( SocketException& e ) 35 { 36 std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; 37 } 14 RenderServer::RenderServer() 15 { 38 16 } 39 17 18 RenderServer::RenderServer(int port_num) 19 { 20 socket_num = port_num; 40 21 41 bool RenderServer::listen(std::string& data){ 22 //init socket server 23 std::cout << "server up and running....\n"; 42 24 43 if(!open_socket.is_connected()){ 44 if(server_socket->accept(open_socket)){ 45 fprintf(stderr, "server: connection accepted\n"); 46 try 47 { 48 //std::string data; 49 open_socket >> data; 50 std::cout << "server: msg received - " << data << "\n"; 51 52 open_socket << data; 53 return false; 54 } 55 catch ( SocketException& ) { return false;} 56 } 57 } 58 else{ 59 try 60 { 61 //std::string data; 62 open_socket >> data; 63 //std::cout << "server: msg received - " << data << "\n"; 64 65 //open_socket << data; 66 return true; 67 } 68 catch ( SocketException& ) { return false;} 25 try { 26 // Create the socket 27 server_socket = new ServerSocket(socket_num); 28 //server_socket->set_non_blocking(true); 29 } catch (SocketException& e) { 30 std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; 69 31 } 70 32 } 71 33 34 bool RenderServer::listen(std::string& data) 35 { 36 if (!open_socket.is_connected()) { 37 if (server_socket->accept(open_socket)) { 38 fprintf(stderr, "server: connection accepted\n"); 39 try { 40 //std::string data; 41 open_socket >> data; 42 std::cout << "server: msg received - " << data << "\n"; 72 43 44 open_socket << data; 45 return false; 46 } catch (SocketException&) { 47 return false; 48 } 49 } 50 } else { 51 try { 52 //std::string data; 53 open_socket >> data; 54 //std::cout << "server: msg received - " << data << "\n"; 73 55 74 bool RenderServer::send(std::string& data){ 75 76 if(!open_socket.is_connected()) 77 return false; 78 79 try 80 { 81 open_socket << data; 82 return true; 83 } 84 catch ( SocketException& ) { return false;} 56 //open_socket << data; 57 return true; 58 } catch (SocketException&) { 59 return false; 60 } 61 } 85 62 } 86 63 64 bool RenderServer::send(std::string& data) 65 { 66 if (!open_socket.is_connected()) 67 return false; 87 68 88 bool RenderServer::send(char* s, int size){ 89 if(!open_socket.is_connected()) 90 return false; 69 try { 70 open_socket << data; 71 return true; 72 } catch (SocketException&) { 73 return false; 74 } 75 } 91 76 92 try 93 { 94 open_socket.send(s, size); 95 return true; 96 } 97 catch ( SocketException& ) { return false;} 77 bool RenderServer::send(char* s, int size) 78 { 79 if (!open_socket.is_connected()) 80 return false; 81 82 try { 83 open_socket.send(s, size); 84 return true; 85 } catch (SocketException&) { 86 return false; 87 } 98 88 } -
trunk/packages/vizservers/nanovis/socket/RenderServer.h
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * RenderServer.h: server with OpenRenderer engine 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef _RENDER_SERVER_H_17 #define _RENDER_SERVER_H_12 #ifndef RENDER_SERVER_H 13 #define RENDER_SERVER_H 18 14 19 15 #include <string> … … 22 18 #include "SocketException.h" 23 19 24 class RenderServer{ 25 20 class RenderServer 21 { 22 public: 23 RenderServer(); 24 RenderServer(int port_num); 25 26 bool listen(std::string& data); 27 bool send(std::string& data); 28 bool send(char* data, int size); //send raw bytes 29 26 30 private: 27 ServerSocket* server_socket; 28 ServerSocket open_socket; 29 int socket_num; 30 31 public: 32 RenderServer(); 33 RenderServer(int port_num); 34 bool listen(std::string& data); 35 bool send(std::string& data); 36 bool send(char* data, int size); //send raw bytes 31 ServerSocket *server_socket; 32 ServerSocket open_socket; 33 int socket_num; 37 34 }; 38 35 -
trunk/packages/vizservers/nanovis/socket/ServerSocket.cpp
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Implementation of the ServerSocket class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 17 13 #include "SocketException.h" 18 14 19 ServerSocket::ServerSocket ( int port)15 ServerSocket::ServerSocket(int port) 20 16 { 21 connected = false; 22 if ( ! Socket::create() ) 23 { 24 throw SocketException ( "Could not create server socket." ); 17 connected = false; 18 if (!Socket::create()) { 19 throw SocketException("Could not create server socket."); 25 20 } 26 21 27 if ( ! Socket::bind ( port ) ) 28 { 29 throw SocketException ( "Could not bind to port." ); 22 if (!Socket::bind(port)) { 23 throw SocketException("Could not bind to port."); 30 24 } 31 25 32 if ( ! Socket::listen() ) 33 { 34 throw SocketException ( "Could not listen to socket." ); 26 if (!Socket::listen()) { 27 throw SocketException("Could not listen to socket."); 35 28 } 36 37 29 } 38 30 39 31 ServerSocket::~ServerSocket() 40 32 { 41 connected = false;33 connected = false; 42 34 } 43 35 44 45 const ServerSocket& ServerSocket::operator << ( const std::string& s ) const 36 const ServerSocket& ServerSocket::operator <<(const std::string& s) const 46 37 { 47 if ( ! Socket::send ( s ) ) 48 { 49 throw SocketException ( "Could not write to socket." ); 38 if (!Socket::send(s)) { 39 throw SocketException("Could not write to socket."); 50 40 } 51 41 52 return *this; 53 42 return *this; 54 43 } 55 44 56 57 const ServerSocket& ServerSocket::operator >> ( std::string& s ) const 45 const ServerSocket& ServerSocket::operator >>(std::string& s) const 58 46 { 59 if ( ! Socket::recv ( s ) ) 60 { 61 throw SocketException ( "Could not read from socket." ); 47 if (!Socket::recv(s)) { 48 throw SocketException("Could not read from socket."); 62 49 } 63 50 64 return *this;51 return *this; 65 52 } 66 53 67 bool ServerSocket::send 54 bool ServerSocket::send(char* s, int size) const 68 55 { 69 //printf("ServerSocket::send: 0\n"); 70 bool ret = Socket::send (s, size); 71 //printf("ServerSocket::send: 1\n"); 72 if (!ret) 73 { 74 //printf("ServerSocket::send: 2\n"); 75 throw SocketException ( "Could not write to socket." ); 56 //printf("ServerSocket::send: 0\n"); 57 bool ret = Socket::send(s, size); 58 //printf("ServerSocket::send: 1\n"); 59 if (!ret) { 60 //printf("ServerSocket::send: 2\n"); 61 throw SocketException("Could not write to socket."); 76 62 } 77 63 78 return ret;64 return ret; 79 65 } 80 66 81 int ServerSocket::recv (char* s, int size) const67 int ServerSocket::recv(char* s, int size) const 82 68 { 83 bool ret = Socket::recv (s, size); 84 if (!ret) 85 { 86 throw SocketException ( "Could not read from socket." ); 69 bool ret = Socket::recv(s, size); 70 if (!ret) { 71 throw SocketException("Could not read from socket."); 87 72 } 88 73 89 return ret;74 return ret; 90 75 } 91 76 92 bool ServerSocket::accept ( ServerSocket& sock)77 bool ServerSocket::accept(ServerSocket& sock) 93 78 { 94 /* 95 if ( ! Socket::accept ( sock ) ) 96 { 97 throw SocketException ( "Could not accept socket." ); 98 } 99 */ 100 bool ret = Socket::accept( sock ); 101 sock.set_connected(ret); 102 return ret; 79 /* 80 if (!Socket::accept(sock)) { 81 throw SocketException("Could not accept socket."); 82 } 83 */ 84 bool ret = Socket::accept(sock); 85 sock.set_connected(ret); 86 return ret; 103 87 } 104 88 105 void ServerSocket::set_non_blocking(bool val){ 106 Socket::set_non_blocking(val); 89 void ServerSocket::set_non_blocking(bool val) 90 { 91 Socket::set_non_blocking(val); 107 92 } 108 93 109 bool ServerSocket::is_connected(){ 110 return connected; 94 bool ServerSocket::is_connected() 95 { 96 return connected; 111 97 } 112 98 113 bool ServerSocket::set_connected(bool val){ 114 return connected = val; 99 bool ServerSocket::set_connected(bool val) 100 { 101 return connected = val; 115 102 } -
trunk/packages/vizservers/nanovis/socket/ServerSocket.h
r2827 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Definition of the ServerSocket class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef S erverSocket_class17 #define S erverSocket_class12 #ifndef SERVERSOCKET_H 13 #define SERVERSOCKET_H 18 14 19 15 #include "Socket.h" … … 21 17 class ServerSocket : private Socket 22 18 { 23 private: 24 bool connected; 19 public: 20 ServerSocket(int port); 21 ServerSocket() 22 {} 23 virtual ~ServerSocket(); 25 24 26 public: 25 const ServerSocket& operator <<(const std::string&) const; 26 const ServerSocket& operator >>(std::string&) const; 27 27 28 ServerSocket ( int port ); 29 ServerSocket (){}; 30 virtual ~ServerSocket(); 28 bool send(char* s, int size) const; 29 int recv(char* s, int size) const; 31 30 32 const ServerSocket& operator << ( const std::string& ) const; 33 const ServerSocket& operator >> ( std::string& ) const; 31 bool accept(ServerSocket&); 34 32 35 bool send(char* s, int size) const; 36 int recv( char* s, int size) const; 33 void set_non_blocking(bool val); 34 bool is_connected(); 35 bool set_connected(bool val); 37 36 38 bool accept ( ServerSocket& ); 39 40 void set_non_blocking(bool val); 41 bool is_connected(); 42 bool set_connected(bool val); 37 private: 38 bool connected; 43 39 }; 44 40 45 46 41 #endif -
trunk/packages/vizservers/nanovis/socket/Socket.cpp
r2822 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Implementation of the Socket class. 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 23 19 ssize_t readn(int fd, void *vptr, size_t n) ; 24 20 25 void error(int status, int err, const char *fmt, ... ){ 26 va_list ap; 27 va_start(ap, fmt); 28 //fprintf(stderr, "%s: ", program_name); 29 vfprintf(stderr, fmt, ap); 30 va_end(ap); 31 32 if(err) 33 fprintf(stderr, ":%s (%d)\n", strerror(err), err); 34 if(status) 35 exit(status); 21 void error(int status, int err, const char *fmt, ... ) 22 { 23 va_list ap; 24 va_start(ap, fmt); 25 //fprintf(stderr, "%s: ", program_name); 26 vfprintf(stderr, fmt, ap); 27 va_end(ap); 28 29 if (err) 30 fprintf(stderr, ":%s (%d)\n", strerror(err), err); 31 if (status) 32 exit(status); 36 33 } 37 34 … … 43 40 char *endptr; 44 41 short port; 45 42 46 43 bzero(sap, sizeof(*sap)); 47 44 sap->sin_family = AF_INET; 48 45 49 46 if (hname!=NULL) { 50 47 if (!inet_aton(hname, &sap->sin_addr)) { … … 71 68 void parse_GET_string(char *_str, char keys[256][256], char values[256][256], int *count) 72 69 { 73 char str[1024]; 74 strcpy(str, _str); 75 char * pch_amp; 76 77 //printf ("Splitting string \"%s\" in tokens:\n",str); 78 pch_amp = strtok (str,"&"); 79 //return; 80 int k=0; 81 while (pch_amp != NULL) 82 { 83 char keyval[128]; 84 strcpy(keyval, pch_amp); 70 char str[1024]; 71 strcpy(str, _str); 72 char * pch_amp; 73 74 //printf ("Splitting string \"%s\" in tokens:\n",str); 75 pch_amp = strtok (str,"&"); 76 //return; 77 int k=0; 78 while (pch_amp != NULL) { 79 char keyval[128]; 80 strcpy(keyval, pch_amp); 85 81 86 char *valptr = strchr(keyval, '=');82 char *valptr = strchr(keyval, '='); 87 83 88 if (valptr!=NULL) 89 { 90 *valptr='\0'; 91 valptr++; 84 if (valptr!=NULL) { 85 *valptr='\0'; 86 valptr++; 92 87 93 strcpy(keys[k], keyval); 94 strcpy(values[k], valptr); 95 } 88 strcpy(keys[k], keyval); 89 strcpy(values[k], valptr); 90 } else { 91 strcpy(keys[k], keyval); 92 strcpy(values[k], ""); 93 } 94 //printf("Key - :%s:, Value - :%s:\n", keys[k], values[k]); 95 pch_amp = strtok (NULL, "&"); 96 k++; 97 } 98 *count = k; 99 } 100 101 Socket::Socket() : 102 m_sock(-1) 103 { 104 memset(&m_addr, 0, sizeof(m_addr)); 105 } 106 107 Socket::~Socket() 108 { 109 if (is_valid()) 110 ::close(m_sock); 111 } 112 113 bool Socket::create() 114 { 115 m_sock = socket(AF_INET, SOCK_STREAM, 0); 116 117 if (! is_valid()) 118 return false; 119 120 // TIME_WAIT - argh 121 int on = 1; 122 if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) == -1) 123 return false; 124 return true; 125 } 126 127 bool Socket::bind(const int port) 128 { 129 if (! is_valid()) { 130 return false; 131 } 132 133 m_addr.sin_family = AF_INET; 134 m_addr.sin_addr.s_addr = INADDR_ANY; 135 m_addr.sin_port = htons(port); 136 137 int bind_return = ::bind(m_sock, (struct sockaddr *)&m_addr, sizeof(m_addr)); 138 139 if (bind_return == -1) { 140 return false; 141 } 142 143 return true; 144 } 145 146 bool Socket::listen() const 147 { 148 if (!is_valid()) { 149 return false; 150 } 151 152 //printf("socket:listen:1\n"); 153 //fflush(stdout); 154 int listen_return = ::listen(m_sock, MAXCONNECTIONS); 155 //printf("socket:listen:2\n"); 156 //fflush(stdout); 157 158 if (listen_return == -1) { 159 return false; 160 } 161 162 return true; 163 } 164 165 bool Socket::accept(Socket& new_socket) const 166 { 167 int addr_length = sizeof(m_addr); 168 new_socket.m_sock = ::accept(m_sock, (sockaddr *)&m_addr, (socklen_t *)&addr_length); 169 170 if (new_socket.m_sock <= 0) 171 return false; 96 172 else 97 { 98 strcpy(keys[k], keyval); 99 strcpy(values[k], ""); 100 } 101 //printf("Key - :%s:, Value - :%s:\n", keys[k], values[k]); 102 pch_amp = strtok (NULL, "&"); 103 k++; 104 } 105 *count = k; 106 } 107 108 109 110 Socket::Socket() : 111 m_sock ( -1 ) 112 { 113 114 memset ( &m_addr, 115 0, 116 sizeof ( m_addr ) ); 117 118 } 119 120 Socket::~Socket() 121 { 122 if ( is_valid() ) 123 ::close ( m_sock ); 124 } 125 126 bool Socket::create() 127 { 128 m_sock = socket ( AF_INET, 129 SOCK_STREAM, 130 0 ); 131 132 if ( ! is_valid() ) 133 return false; 134 135 136 // TIME_WAIT - argh 137 int on = 1; 138 if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) 139 return false; 140 141 142 return true; 143 144 } 145 146 147 148 bool Socket::bind ( const int port ) 149 { 150 151 if ( ! is_valid() ) 152 { 153 return false; 154 } 155 156 m_addr.sin_family = AF_INET; 157 m_addr.sin_addr.s_addr = INADDR_ANY; 158 m_addr.sin_port = htons ( port ); 159 160 int bind_return = ::bind ( m_sock, 161 ( struct sockaddr * ) &m_addr, 162 sizeof ( m_addr ) ); 163 164 165 if ( bind_return == -1 ) 166 { 167 return false; 168 } 169 170 return true; 171 } 172 173 174 bool Socket::listen() const 175 { 176 if ( ! is_valid() ) 177 { 178 return false; 179 } 180 181 //printf("socket:listen:1\n"); 182 //fflush(stdout); 183 int listen_return = ::listen ( m_sock, MAXCONNECTIONS ); 184 //printf("socket:listen:2\n"); 185 //fflush(stdout); 186 187 188 if ( listen_return == -1 ) 189 { 190 return false; 191 } 192 193 return true; 194 } 195 196 197 bool Socket::accept ( Socket& new_socket ) const 198 { 199 int addr_length = sizeof ( m_addr ); 200 new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length ); 201 202 if ( new_socket.m_sock <= 0 ) 203 return false; 204 else 205 return true; 206 } 207 208 209 bool Socket::send ( const std::string s ) const 210 { 211 int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL ); 212 if ( status == -1 ) 213 { 214 return false; 215 } 216 else 217 { 218 return true; 219 } 220 } 221 222 223 bool Socket::send (char* s, int size) const 224 { 225 //int status = ::send ( m_sock, s, size, MSG_NOSIGNAL ); 226 227 int status = writen(m_sock, s, size); 228 if ( status == -1 ) 229 { 230 return false; 231 } 232 else 233 { 234 return true; 235 } 236 } 237 238 239 int Socket::recv ( std::string& s ) const 240 { 241 char buf [ MAXRECV + 1 ]; 242 243 s = ""; 244 245 memset ( buf, 0, MAXRECV + 1 ); 246 int status = ::recv ( m_sock, buf, MAXRECV, 0 ); 247 248 if ( status == -1 ) 249 { 250 std::cout << "status == -1 errno == " << errno << " in Socket::recv\n"; 251 return 0; 252 } 253 else if ( status == 0 ) 254 { 255 return 0; 256 } 257 else 258 { 259 s = buf; 260 return status; 261 } 262 } 263 264 265 int Socket::recv (char* s, int size) const 266 { 267 268 //int status = ::recv ( m_sock, s, size, 0 ); 269 int status = readn( m_sock, s, size); 270 271 if ( status == -1 ) 272 { 273 std::cout << "status == -1 errno == " << errno << " in Socket::recv\n"; 274 return 0; 275 } 276 else if ( status == 0 ) 277 { 278 return 0; 279 } 280 else 281 { 282 return status; 283 } 284 } 285 286 287 bool Socket::connect ( const std::string host, const int port ) 288 { 289 if ( ! is_valid() ) return false; 290 291 m_addr.sin_family = AF_INET; 292 m_addr.sin_port = htons ( port ); 293 294 int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr ); 295 296 if ( errno == EAFNOSUPPORT ) return false; 297 298 status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) ); 299 300 if ( status == 0 ) 301 return true; 302 else 303 return false; 304 } 305 306 void Socket::set_non_blocking ( const bool b ) 307 { 308 309 int opts; 310 311 opts = fcntl ( m_sock, 312 F_GETFL ); 313 314 if ( opts < 0 ) 315 { 316 return; 317 } 318 319 if ( b ) 320 opts = ( opts | O_NONBLOCK ); 321 else 322 opts = ( opts & ~O_NONBLOCK ); 323 324 fcntl ( m_sock, 325 F_SETFL,opts ); 326 173 return true; 174 } 175 176 bool Socket::send(const std::string s) const 177 { 178 int status = ::send(m_sock, s.c_str(), s.size(), MSG_NOSIGNAL); 179 if (status == -1) { 180 return false; 181 } else { 182 return true; 183 } 184 } 185 186 bool Socket::send(char* s, int size) const 187 { 188 //int status = ::send (m_sock, s, size, MSG_NOSIGNAL); 189 190 int status = writen(m_sock, s, size); 191 if (status == -1) { 192 return false; 193 } else { 194 return true; 195 } 196 } 197 198 int Socket::recv(std::string& s) const 199 { 200 char buf[MAXRECV + 1]; 201 202 s = ""; 203 204 memset(buf, 0, MAXRECV + 1); 205 int status = ::recv(m_sock, buf, MAXRECV, 0); 206 207 if (status == -1) { 208 std::cout << "status == -1 errno == " << errno << " in Socket::recv\n"; 209 return 0; 210 } else if (status == 0) { 211 return 0; 212 } else { 213 s = buf; 214 return status; 215 } 216 } 217 218 int Socket::recv(char* s, int size) const 219 { 220 //int status = ::recv ( m_sock, s, size, 0 ); 221 int status = readn(m_sock, s, size); 222 223 if (status == -1) { 224 std::cout << "status == -1 errno == " << errno << " in Socket::recv\n"; 225 return 0; 226 } else if (status == 0) { 227 return 0; 228 } else { 229 return status; 230 } 231 } 232 233 bool Socket::connect(const std::string host, const int port) 234 { 235 if (!is_valid()) return false; 236 237 m_addr.sin_family = AF_INET; 238 m_addr.sin_port = htons(port); 239 240 int status = inet_pton(AF_INET, host.c_str(), &m_addr.sin_addr); 241 242 if (errno == EAFNOSUPPORT) 243 return false; 244 245 status = ::connect(m_sock, (sockaddr *)&m_addr, sizeof(m_addr)); 246 247 if (status == 0) 248 return true; 249 else 250 return false; 251 } 252 253 void Socket::set_non_blocking(const bool b) 254 { 255 int opts; 256 257 opts = fcntl(m_sock, F_GETFL); 258 259 if (opts < 0) { 260 return; 261 } 262 263 if (b) 264 opts = (opts | O_NONBLOCK); 265 else 266 opts = (opts & ~O_NONBLOCK); 267 268 fcntl(m_sock, F_SETFL, opts); 327 269 } 328 270 329 271 ssize_t readn(int fd, void *vptr, size_t n) 330 272 { 331 size_t nleft; 332 ssize_t nread; 333 char* ptr; 334 335 ptr = (char*) vptr; 336 nleft = n; 337 338 while(nleft > 0){ 339 if((nread=read(fd, ptr, nleft))<0){ 340 if(errno==EINTR) 341 nread = 0; 342 else 343 return(-1); 344 } 345 else if(nread==0){ 346 fprintf(stderr, "socket::readn nread==0\n"); 347 break; 348 } 349 350 nleft -= nread; 351 ptr += nread; 352 } 353 354 return (n-nleft); //return n>=0 355 } 356 273 size_t nleft; 274 ssize_t nread; 275 char* ptr; 276 277 ptr = (char*)vptr; 278 nleft = n; 279 280 while (nleft > 0) { 281 if ((nread=read(fd, ptr, nleft))<0) { 282 if (errno==EINTR) 283 nread = 0; 284 else 285 return(-1); 286 } else if (nread==0) { 287 fprintf(stderr, "socket::readn nread==0\n"); 288 break; 289 } 290 291 nleft -= nread; 292 ptr += nread; 293 } 294 295 return (n-nleft); //return n>=0 296 } 357 297 358 298 ssize_t writen(int fd, void *vptr, size_t n) 359 299 { 360 size_t nleft;361 ssize_t nwritten;362 const char* ptr;363 364 ptr = (char*)vptr;365 nleft = n;366 367 while(nleft > 0){368 if((nwritten=write(fd, ptr, nleft))<=0){369 if(errno==EINTR)370 371 else372 373 }374 nleft -= nwritten;375 ptr += nwritten;376 }377 378 return (n);379 } 300 size_t nleft; 301 ssize_t nwritten; 302 const char* ptr; 303 304 ptr = (char*)vptr; 305 nleft = n; 306 307 while (nleft > 0) { 308 if ((nwritten=write(fd, ptr, nleft))<=0) { 309 if (errno==EINTR) 310 nwritten = 0; 311 else 312 return(-1); 313 } 314 nleft -= nwritten; 315 ptr += nwritten; 316 } 317 318 return (n); 319 } -
trunk/packages/vizservers/nanovis/socket/Socket.h
r2798 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * Definition of the Socket class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef _SOCKET_H_17 #define _SOCKET_H_12 #ifndef SOCKET_H 13 #define SOCKET_H 18 14 19 15 #include <errno.h> … … 29 25 #include <arpa/inet.h> 30 26 #include <iostream> 31 32 27 33 28 const int MAXHOSTNAME = 200; … … 46 41 class Socket 47 42 { 48 49 Socket();50 virtual ~Socket();43 public: 44 Socket(); 45 virtual ~Socket(); 51 46 52 // Server initialization53 bool create();54 bool bind ( const int port);55 bool listen() const;56 bool accept ( Socket&) const;47 // Server initialization 48 bool create(); 49 bool bind(const int port); 50 bool listen() const; 51 bool accept(Socket&) const; 57 52 58 // Client initialization59 bool connect ( const std::string host, const int port);53 // Client initialization 54 bool connect(const std::string host, const int port); 60 55 61 // Data Transimission62 bool send ( const std::string) const;63 bool send(char* s, int size) const;64 int recv ( std::string&) const;65 int recv (char* s, int size) const;56 // Data Transimission 57 bool send(const std::string) const; 58 bool send(char* s, int size) const; 59 int recv(std::string&) const; 60 int recv(char* s, int size) const; 66 61 67 void set_non_blocking ( const bool);62 void set_non_blocking(const bool); 68 63 69 bool is_valid() const { return m_sock != -1; } 64 bool is_valid() const { 65 return m_sock != -1; 66 } 70 67 71 int m_sock;68 int m_sock; 72 69 73 74 sockaddr_in m_addr;70 private: 71 sockaddr_in m_addr; 75 72 }; 76 73 -
trunk/packages/vizservers/nanovis/socket/SocketException.h
r2798 r2898 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * ---------------------------------------------------------------------- 4 * SocketException class 5 * 6 * ====================================================================== 2 /* ====================================================================== 7 3 * AUTHOR: Wei Qiao <qiaow@purdue.edu> 8 4 * Purdue Rendering and Perceptualization Lab (PURPL) … … 14 10 * ====================================================================== 15 11 */ 16 #ifndef S ocketException_class17 #define S ocketException_class12 #ifndef SOCKETEXCEPTION_H 13 #define SOCKETEXCEPTION_H 18 14 19 15 #include <string> … … 21 17 class SocketException 22 18 { 23 public: 24 SocketException ( std::string s ) : m_s ( s ) {}; 25 ~SocketException (){}; 19 public: 20 SocketException (std::string s) : 21 m_s (s) 22 {} 26 23 27 std::string description() { return m_s; } 24 ~SocketException() 25 {} 28 26 29 private: 27 std::string description() 28 { 29 return m_s; 30 } 30 31 31 std::string m_s; 32 32 private: 33 std::string m_s; 33 34 }; 34 35
Note: See TracChangeset
for help on using the changeset viewer.