/*! * @if jp * @brief 途中のコンテキストを再帰的に bind し NamingContext を bind する * @else * @brief Bind intermediate context recursively and bind NamingContext * @endif */ void CorbaNaming::bindContextRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name& name, CosNaming::NamingContext_ptr name_cxt) { bindRecursive(context, name, name_cxt); return; }
/*! * @if jp * @brief Object を bind する * @else * @brief Bind object on specified name component position * @endif */ void CorbaNaming::bind(const CosNaming::Name& name, CORBA::Object_ptr obj, const bool force) throw (SystemException, NotFound, CannotProceed, InvalidName, AlreadyBound) { try { m_rootContext->bind(name, obj); } catch (NotFound& e) { force ? bindRecursive(m_rootContext, name, obj) : throw e; } catch (CannotProceed& e) { #ifndef ORB_IS_RTORB force ? bindRecursive(e.cxt, e.rest_of_name, obj) : throw e; #else // ORB_IS_RTORB force ? bindRecursive(e.cxt(), e.rest_of_name(), obj) : throw e; #endif // ORB_IS_RTORB } }
/*! * @if jp * @brief 新しいコンテキストを bind する * @else * pbrief Bind new namingContext * @endif */ CosNaming::NamingContext_ptr CorbaNaming::bindNewContext(const CosNaming::Name& name, bool force) throw (SystemException, NotFound, CannotProceed, InvalidName, AlreadyBound) { try { return m_rootContext->bind_new_context(name); } catch (NotFound& e) { force ? bindRecursive(m_rootContext, name, newContext()) : throw e; } catch (CannotProceed& e) { #ifndef ORB_IS_RTORB force ? bindRecursive(e.cxt, e.rest_of_name, newContext()) : throw e; #else // ORB_IS_RTORB force ? bindRecursive(e.cxt(), e.rest_of_name(), newContext()) : throw e; #endif // ORB_IS_RTORB } return CosNaming::NamingContext::_nil(); }
int bindRecursive(int socketId, int portNumber, int numberofTry){ struct sockaddr_in bindaddr; int state; bzero(&bindaddr, sizeof(bindaddr)); bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); bindaddr.sin_port = htons(portNumber); printf("------port number : %d------\n", portNumber); printf("trying to bind...\n"); state = bind(socketId, (struct sockaddr *)&bindaddr, sizeof(bindaddr)); if (state == -1) { perror("bind error "); if (numberofTry < BINDING_TRY_LIMIT) state = bindRecursive(socketId, portNumber + 1, numberofTry +1); } else { printf("binding succeed\n"); } return state; }
int main (int argc, char **argv) { int serverSocket, clientSocket; int portNumber; int count; int windowSize; socklen_t clientLen; struct sockaddr_in clientaddr; char buf[255]; clientLen = sizeof(clientaddr); // Check arguments if (argc != 2) { printf("Usage: %s <port>\n", argv[0]); exit(1); } else { portNumber = atoi(argv[1]); } if ( (serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { perror("socket error "); exit(1); } if ( bindRecursive(serverSocket, portNumber, 0) == -1 ){ printf("failed to bind for %d times, shut down\n", BINDING_TRY_LIMIT); exit(1); } if ( listen(serverSocket, 0) == -1 ) { perror("listen error "); exit(1); } printf("waiting for connection...\n"); clientSocket = accept(serverSocket, (struct sockaddr *) &clientaddr, &clientLen); if (clientSocket == -1) { perror("accept error "); exit(1); } while(1){ memset(buf, '0', 255); count = recv(clientSocket, buf, 255, 0); if (count == -1) { perror("can't recieve "); close(clientSocket); break; } buf[count] = '\0'; printf("received data : %s\n", buf); if( strncmp(buf, identifyQuestion, strlen(identifyQuestion) ) == 0) { printf("client is connected\n"); send(clientSocket, identifyAnswer, strlen(identifyAnswer), 0); } else if ( buf[0] == 'C' ) { char *integer = buf + 1; windowSize = atoi(integer); } else if ( strncmp(buf, quitStr, strlen(quitStr)) == 0 ) { printf("Connection terminated\n"); break; } else if ( buf[0] == 'R' ) { } } close(clientSocket); close(serverSocket); return 0; }