int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { if (parse_args (argc, argv) == -1) return -1; try { // Start the Naming Service tasks NamingTask name_service ("NamingORBA", argc, argv); name_service.activate(); // Wait for the Naming Service initialized. name_service.waitInit(); ACE_Argv_Type_Converter satc (argc, argv); CORBA::ORB_var sorb = CORBA::ORB_init (satc.get_argc (), satc.get_TCHAR_argv (), server_orb.c_str ()); ACE_Manual_Event me; Server_Task server_task (name_service.root (), sorb.in (), me, ACE_Thread_Manager::instance ()); if (server_task.activate (THR_NEW_LWP | THR_JOINABLE, 1, 1) == -1) { ACE_ERROR ((LM_ERROR, "Error activating server task\n")); } // Wait for the server thread to do some processing me.wait (); ACE_Argv_Type_Converter catc (argc, argv); CORBA::ORB_var corb = CORBA::ORB_init (catc.get_argc (), catc.get_TCHAR_argv (), client_orb.c_str ()); Client_Task client_task (name_service.root (), corb.in (), ACE_Thread_Manager::instance (), result); if (client_task.activate (THR_NEW_LWP | THR_JOINABLE, 1, 1) == -1) { ACE_ERROR ((LM_ERROR, "Error activating client task\n")); } // Wait for the client and server to finish ACE_Thread_Manager::instance ()->wait (); // Now that all threads have completed we can destroy the ORB sorb->destroy (); if (server_orb != client_orb) { corb->destroy (); } } catch (const CORBA::Exception&) { // Ignore exceptions.. } return 0; }
int main(int argc, char *argv[]) { int sockfd, hostfd; int flags, opt, i, n, m; char serv_name[MAXHOSTNAME]; char port[MAXPORTSIZE]; char name[MAXNAMESIZE + 1]; struct name_prtl request; // get options flags = 0; name[0] = '\0'; while ((opt = getopt(argc, argv, "adlun:")) != -1) { switch (opt) { case 'l': /* lookup */ if (flags != 0) { fprintf(stderr, "[Error] you should only specify one type of "); fprintf(stderr, "operations from add, lookup, delete and update\n"); exit(EXIT_FAILURE); } flags = 1; break; case 'a': /* add */ if (flags != 0) { fprintf(stderr, "[Error] you should only specify one type of "); fprintf(stderr, "operations from add, lookup, delete and update\n"); exit(EXIT_FAILURE); } flags = 2; break; case 'd': /* delete */ if (flags != 0) { fprintf(stderr, "[Error] you should only specify one type of "); fprintf(stderr, "operations from add, lookup, delete and update\n"); exit(EXIT_FAILURE); } flags = 3; break; case 'u': /* update */ if (flags != 0) { fprintf(stderr, "[Error] you should only specify one type of "); fprintf(stderr, "operations from add, lookup, delete and update\n"); exit(EXIT_FAILURE); } flags = 4; break; case 'n': /* name */ n = strlen(optarg); if (n > MAXNAMESIZE) { fprintf(stderr, "[Error] the name is too long, its maxsize is %d\n", MAXNAMESIZE); exit(EXIT_FAILURE); } // copy the input string to name[] for (i = 0; i < n; i++) { name[i] = optarg[i]; } for ( ; i < MAXNAMESIZE + 1; i++) { name[i] = '\0'; } break; default: /* ? */ fprintf(stderr, "[Usage] %s [-adlu] [-n name]\n", argv[0]); exit(EXIT_FAILURE); } } // check input parameters if (flags == 0 || name[0] == '\0') { fprintf(stderr, "[Error] missing args\n"); fprintf(stderr, "[Usage] %s [-adlu] [-n name]\n", argv[0]); exit(EXIT_FAILURE); } if (!((name[0] >= 48 && name[0] <= 57) || (name[0] >= 65 && name[0] <= 90) || (name[0] >= 97 && name[0] <= 122))) { fprintf(stderr, "[Error] illegal name\n "); fprintf(stderr, "the name should begin with a letter or a number\n"); exit(EXIT_FAILURE); } // open the server config file if ((hostfd = open(CONF_FILE, O_RDONLY)) < 0) { handle_err("Open config file error"); } if ((m = get_server_info(hostfd, serv_name, port)) == -1) { fprintf(stderr, "Error: failed to parse server name and port\n"); exit(EXIT_FAILURE); } // connect to name server with timeout sockfd = tcp_connect(serv_name, port); /* tcp_connect in func_wrapper */ if (sockfd == -1) { fprintf(stderr, "[Error] failed to connect to the server\n"); exit(EXIT_FAILURE); } // initialize the request pkt request.protocol = 1; request.type = flags; strncpy(request.name, name, MAXNAMESIZE + 1); if (flags == 1 || flags == 3) { request.len = 0; } else { get_attr_input(&request); /* get the attribute string */ } /* client */ name_service(sockfd, &request); close(sockfd); exit(EXIT_SUCCESS); }