Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

helper.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright 2005 Daniel Ferullo                                             *
00003  *                                                                           *
00004  * Licensed under the Apache License, Version 2.0 (the "License");           *
00005  * you may not use this file except in compliance with the License.          *
00006  * You may obtain a copy of the License at                                   *
00007  *                                                                           *
00008  *    http://www.apache.org/licenses/LICENSE-2.0                             *
00009  *                                                                           *
00010  * Unless required by applicable law or agreed to in writing, software       *
00011  * distributed under the License is distributed on an "AS IS" BASIS,         *
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00013  * See the License for the specific language governing permissions and       *
00014  * limitations under the License.                                            *
00015  *                                                                           *
00016  *****************************************************************************/
00017 
00018 /**
00019  * @file helper.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief stub demo helper server
00023  */
00024 
00025 #include <stdio.h>
00026 #include <getopt.h>
00027 #include <stdlib.h>
00028 #include "def.h"
00029 #include "berkeleyapi.h"
00030 #include "errorcodes.h"
00031 #include "natblaster_helper.h"
00032 
00033 /**
00034  * @brief gets arguments from the command line
00035  *
00036  * uses errorcodes.h for error codes
00037  *
00038  * @param argc the number of arguments passed in
00039  * @param argv the vector of arguments
00040  * @param helper_port pointer to the helper's port (will be filled in)
00041  *
00042  * @return SUCCESS, errorcode on failure
00043  */
00044 int getArgs(int argc, char *argv[], port_t *helper_port);
00045 
00046 /**
00047  * @brief prints the program use
00048  *
00049  * @return void
00050  */
00051 void printUse();
00052 
00053 /**
00054  * @brief stud entry point
00055  *
00056  * @param argc the number of elements in the argument vector
00057  * @param argv the argument vector
00058  *
00059  * @return 0 on success, neg on failure
00060  */
00061 int main(int argc, char *argv[]) {
00062 
00063         port_t port;
00064 
00065         if (FAILED(getArgs(argc,argv,&port))) {
00066                 printUse();
00067                 return (-1);
00068         }
00069 
00070         port = htons(port);
00071 
00072         CHECK_FAILED(natblaster_server(port),-2);
00073 
00074         return (0);
00075 
00076 }
00077 
00078 void printUse() {
00079 
00080         printf("options:\n");
00081         printf("\t--listen_port : port to listen for peer connections on [required]\n");
00082         printf("\n");
00083 
00084         return;
00085 }
00086 
00087 int getArgs(int argc, char *argv[], port_t *helper_port) {
00088 
00089         char c;
00090 
00091         static struct option long_options[] =
00092         {
00093                 {"listen_port",     required_argument, 0, 'a'},
00094                 {0, 0, 0, 0 } /* for invalid args */
00095         };
00096         
00097         if (argc < 0)
00098                 return ERROR_NEG_ARG_1;
00099         if (argv==NULL)
00100                 return ERROR_NULL_ARG_2;
00101         if (helper_port==NULL)
00102                 return ERROR_NULL_ARG_3;
00103 
00104         /* set default values */
00105         *helper_port = 0 ;
00106 
00107         /* loop over the arguments, and read them in */
00108         while (1)
00109         {
00110                 /* getopt_long stores the option index here. */
00111                 int option_index = 0;
00112 
00113                 c = getopt_long (argc, argv, "a:b:",
00114                         long_options, &option_index);
00115 
00116                 /* Detect the end of the options. */
00117                 if (c == -1)
00118                         break;
00119 
00120                 switch (c) {
00121                         case 'a' :
00122                                 *helper_port = (port_t) atoi(optarg);
00123                                 break;
00124                         case '?':
00125                                 return ERROR_1;
00126                                 break;
00127                         default:
00128                                 return ERROR_2;
00129                                 break;
00130                 }
00131         }
00132 
00133         if (*helper_port==0)
00134                 return ERROR_3;
00135 
00136         return SUCCESS;
00137 }
00138 

Generated on Wed Mar 30 23:20:47 2005 for NATBLASTER by  doxygen 1.3.9.1