// static int vrpn_Mutex_Remote::handle_initialize(void *userdata, vrpn_HANDLERPARAM p) { vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata; const char *b = p.buffer; // Only pay attention to the first initialize() message we get // after startup. if (me->d_myIndex != -1) { return 0; } vrpn_int32 expected_payload_len = 2 * sizeof(vrpn_int32) + sizeof(vrpn_uint32); if (p.payload_len != expected_payload_len) { fprintf(stderr, "vrpn_Mutex_Remote::handle_initialize: " "Warning: Ignoring message with length %d, expected %d\n", p.payload_len, expected_payload_len); return 0; } vrpn_uint32 ip_addr; vrpn_int32 pid; vrpn_unbuffer(&b, &ip_addr); vrpn_unbuffer(&b, &pid); vrpn_int32 my_pid = 0; #ifdef _WIN32 my_pid = _getpid(); #else my_pid = getpid(); #endif if ((ip_addr != getmyIP()) || (pid != my_pid)) { fprintf( stderr, "vrpn_Mutex_Remote::handle_initialize: " "Warning: Ignoring message that doesn't match ip/pid identifier\n"); #ifdef VERBOSE fprintf(stderr, "Got %lu %d, expected %lu %d.\n", ip_addr, pid, getmyIP(), my_pid); #endif return 0; } vrpn_unbuffer(&b, &(me->d_myIndex)); #ifdef VERBOSE fprintf(stderr, "vrpn_Mutex_Remote::handle_initialize: " "Got assigned index %d.\n", me->d_myIndex); #endif if (me->d_requestBeforeInit) { #ifdef VERBOSE fprintf(stderr, "vrpn_Mutex_Remote::handle_initialize: " "Sending request\n"); #endif me->request(); } return 0; }
vrpn_PeerMutex::vrpn_PeerMutex(const char *name, vrpn_Connection *server) : d_state(AVAILABLE) , d_server(server) , d_peer(NULL) , d_numPeers(0) , d_numConnectionsAllocated(0) , d_myIP(getmyIP(NULL)) , d_myPort(0) , d_holderIP(0) , d_holderPort(-1) , d_reqGrantedCB(NULL) , d_reqDeniedCB(NULL) , d_takeCB(NULL) , d_releaseCB(NULL) , d_peerData(NULL) { if (!name) { fprintf(stderr, "vrpn_PeerMutex: NULL name!\n"); return; } if (server) { d_server->addReference(); } else { fprintf(stderr, "vrpn_PeerMutex: NULL connection!\n"); return; } init(name); }
void vrpn_Mutex_Remote::requestIndex(void) { timeval now; vrpn_int32 buflen = sizeof(vrpn_int32) + sizeof(vrpn_uint32); char *buf = NULL; try { buf = new char[buflen]; } catch (...) { return; } char *bufptr = buf; vrpn_int32 len = buflen; vrpn_uint32 ip_addr = getmyIP(); #ifdef _WIN32 vrpn_int32 pid = _getpid(); #else vrpn_int32 pid = getpid(); #endif vrpn_buffer(&bufptr, &len, ip_addr); vrpn_buffer(&bufptr, &len, pid); #ifdef VERBOSE printf("requesting index for %lu, %d\n", ip_addr, pid); #endif vrpn_gettimeofday(&now, NULL); d_connection->pack_message(buflen, now, d_requestIndex_type, d_myId, buf, vrpn_CONNECTION_RELIABLE); try { delete[] buf; } catch (...) { fprintf(stderr, "vrpn_Mutex_Remote::requestIndex(): delete failed\n"); return; } return; }
vrpn_PeerMutex::vrpn_PeerMutex(const char *name, int port, const char *NICaddress) : d_state(AVAILABLE) , d_server(NULL) , d_peer(NULL) , d_numPeers(0) , d_numConnectionsAllocated(0) , d_myIP(getmyIP(NICaddress)) , d_myPort(port) , d_holderIP(0) , d_holderPort(-1) , d_reqGrantedCB(NULL) , d_reqDeniedCB(NULL) , d_takeCB(NULL) , d_releaseCB(NULL) , d_peerData(NULL) { if (!name) { fprintf(stderr, "vrpn_PeerMutex: NULL name!\n"); return; } // XXX Won't work with non-IP connections (MPI, for example) char con_name[512]; sprintf(con_name, "%s:%d", NICaddress, port); d_server = vrpn_create_server_connection(con_name); if (d_server) { d_server->addReference(); d_server->setAutoDeleteStatus(true); } else { fprintf(stderr, "vrpn_PeerMutex: " "Couldn't open connection on port %d!\n", port); return; } init(name); }