Example #1
0
int main(int argc, char *argv[])
{
  Mem = new Memory();

  if ( Mem == NULL ){
    my_error("couldn't allocate Mem");
    exit(0);
  }

  Mem->GetOptions(argc,argv);

  Socket sock = init_connection(Mem->SP_host,Mem->SP_port);

  Mem->sock = &sock;

  if(Mem->sock->socketfd == -1) {
    cerr << "Can't open connection for player" << endl;
    exit(-1);
  }

  send_initialize_message();

  if ( wait_message(recvbuf, Mem->sock) == 0 )
    my_error("wait_message failed");

  parse_initialize_message(recvbuf);

  Mem->Initialize();
  score();  /* find out the score */

  if (Mem->IP_only_connect)
    return 0;

  sigset_t sigfullmask = init_handler();	      

  while ( Mem->ServerAlive == TRUE && wait_for_signals(&sigfullmask) );

  if (Mem->sock->socketfd != -1) close_connection(Mem->sock);

  printf("Shutting down player %d\n",Mem->MyNumber);
}
int main(int argc, char *argv[])
{
    unsigned32 status;
    rpc_binding_vector_p_t server_binding;
    char * string_binding;
    unsigned32 i;
    const char * protocol = NULL;
    const char * endpoint = NULL;
    int c;

    /*
     * Process the cmd line args
     */

    while ((c = getopt(argc, argv, "e:nut")) != EOF)
    {
        switch (c)
        {
        case 'e':
            endpoint = optarg;
            break;
        case 'n':
            protocol = PROTOCOL_NP;
            break;
        case 'u':
            protocol = PROTOCOL_UDP;
            break;
        case 't':
            protocol = PROTOCOL_TCP;
            break;
        default:
            usage();
        }
    }

#if 0
    if (endpoint && !protocol)
    {
        printf("ERROR: protocol is required when endpoint is specified\n");
        exit(1);
    }
#else
    if(!endpoint || !protocol)
    {
        usage();
        exit(1);
    }
#endif

#ifndef _WIN32
    /* Temporarily disable using all protocols because something is currently busted on Unix */
    if (!protocol)
    {
        protocol = PROTOCOL_TCP;
    }
#endif

    /*
     * Register the Interface with the local endpoint mapper (rpcd)
     */

    printf ("Registering server.... \n");
    rpc_server_register_if(echo_v1_0_s_ifspec,
                           NULL,
                           NULL,
                           &status);
    chk_dce_err(status, "rpc_server_register_if()", "", 1);

    printf("registered.\nPreparing binding handle...\n");

    bind_server(&server_binding, echo_v1_0_s_ifspec, protocol, endpoint);

    if(!endpoint)
    {
        /*
         * Register bindings with the endpoint mapper
         */
        printf("registering bindings with endpoint mapper\n");

        rpc_ep_register(echo_v1_0_s_ifspec,
                        server_binding,
                        NULL,
                        (unsigned char *)"QDA application server",
                        &status);
        chk_dce_err(status, "rpc_ep_register()", "", 1);
    }
    
    printf("registered.\n");

    /*
     * Print out the servers endpoints (TCP and UDP port numbers)
     */

    printf ("Server's communications endpoints are:\n");

    for (i=0; i<RPC_FIELD_COUNT(server_binding); i++)
    {
        rpc_binding_to_string_binding(RPC_FIELD_BINDING_H(server_binding)[i],
                                      (unsigned char **)&string_binding,
                                      &status);
        if (string_binding)
            printf("\t%s\n", string_binding);
    }

#ifndef _WIN32
    /*
     * Start the signal waiting thread in background. This thread will
     * Catch SIGINT and gracefully shutdown the server.
     */

    wait_for_signals();
#endif

    /*
     * Begin listening for calls
     */

    printf ("listening for calls....\n");

    DCETHREAD_TRY
    {
        rpc_server_listen(rpc_c_listen_max_calls_default, &status);
    }
    DCETHREAD_CATCH_ALL(THIS_CATCH)
    {
        printf ("Server stoppped listening\n");
    }
    DCETHREAD_ENDTRY;

    /*
     * If we reached this point, then the server was stopped, most likely
     * by the signal handler thread called rpc_mgmt_stop_server().
     * gracefully cleanup and unregister the bindings from the
     * endpoint mapper.
     */

#ifndef _WIN32
    /*
     * Kill the signal handling thread
     */

#endif

    if (!endpoint)
    {
        printf ("Unregistering server from the endpoint mapper....\n");
        rpc_ep_unregister(echo_v1_0_s_ifspec,
                          server_binding,
                          NULL,
                          &status);
        chk_dce_err(status, "rpc_ep_unregister()", "", 0);
    }

    /*
     * retire the binding information
     */

    printf("Cleaning up communications endpoints...\n");
    rpc_server_unregister_if(echo_v1_0_s_ifspec,
                             NULL,
                             &status);
    chk_dce_err(status, "rpc_server_unregister_if()", "", 0);

    exit(0);
}