Miscellaneous minor code cleanup for MRW's review comments
[trust_router.git] / tr / trpc_main.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <argp.h>
39 #include <unistd.h>
40
41 #include <gsscon.h>
42 #include <tr_debug.h>
43 #include <tr_trp.h>
44
45
46 /* command-line option setup */
47
48 /* argp global parameters */
49 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
50
51 /* doc strings */
52 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router TRP Client";
53 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
54
55 /* define the options here. Fields are:
56  * { long-name, short-name, variable name, options, help description } */
57 static const struct argp_option cmdline_options[] = {
58   { "repeat", 'r', "N", OPTION_ARG_OPTIONAL, "Repeat message until terminated, or N times." },
59   {NULL}
60 };
61
62 /* structure for communicating with option parser */
63 struct cmdline_args {
64   char *msg;
65   char *server;
66   int port; /* optional */
67   int repeat; /* how many times to repeat, or -1 for infinite */
68 };
69
70 /* parser for individual options - fills in a struct cmdline_args */
71 static error_t parse_option(int key, char *arg, struct argp_state *state)
72 {
73   /* get a shorthand to the command line argument structure, part of state */
74   struct cmdline_args *arguments=state->input;
75
76   switch (key) {
77   case 'r':
78     if (arg==NULL)
79       arguments->repeat=-1;
80     else
81       arguments->repeat=strtol(arg, NULL, 10);
82     break;
83
84   case ARGP_KEY_ARG: /* handle argument (not option) */
85     switch (state->arg_num) {
86     case 0:
87       arguments->msg=arg;
88       break;
89
90     case 1:
91       arguments->server=arg;
92       break;
93
94     case 2:
95       arguments->port=strtol(arg, NULL, 10); /* optional */
96       break;
97
98     default:
99       /* too many arguments */
100       argp_usage(state);
101     }
102     break;
103
104   case ARGP_KEY_END: /* no more arguments */
105     if (state->arg_num < 2) {
106       /* not enough arguments encountered */
107       argp_usage(state);
108     }
109     break;
110
111   default:
112     return ARGP_ERR_UNKNOWN;
113   }
114
115   return 0; /* success */
116 }
117
118 /* assemble the argp parser */
119 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
120
121 int main (int argc, 
122           char *argv[]) 
123 {
124   TALLOC_CTX *main_ctx=talloc_new(NULL);
125   TRPC_INSTANCE *trpc=NULL;
126   TRP_CONNECTION *conn=NULL;
127   struct cmdline_args opts;
128
129   /* parse the command line*/
130   /* set defaults */
131   opts.msg=NULL;
132   opts.server=NULL;
133   opts.port=TRP_PORT;
134   opts.repeat=1;
135
136   argp_parse(&argp, argc, argv, 0, 0, &opts);
137
138   /* Use standalone logging */
139   tr_log_open();
140
141   /* set logging levels */
142   talloc_set_log_stderr();
143   tr_log_threshold(LOG_CRIT);
144   tr_console_threshold(LOG_DEBUG);
145
146   printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
147  
148   conn=trp_connection_new(trpc);
149   if (conn==NULL) {
150     printf("Could not allocate TRP_CONNECTION.\n");
151     return 1;
152   }
153   trpc = trpc_new(main_ctx);
154   trpc_set_server(trpc, opts.server);
155   trpc_set_port(trpc, opts.port);
156   trpc_set_conn(trpc, conn);
157   /* Set-up TRP connection */
158   if (TRP_SUCCESS != trpc_connect(trpc)) {
159     /* Handle error */
160     printf("Error in trpc_connect.\n");
161     return 1;
162   }
163
164   /* Send a TRP message */
165   while ((opts.repeat==-1) || (opts.repeat-->0)) {
166     if (TRP_SUCCESS != trpc_send_msg(trpc, opts.msg)) {
167       /* Handle error */
168       printf("Error in trpc_send_request.");
169       return 1;
170     }
171     usleep(1000000);
172   }
173     
174   /* Clean-up the TRP client instance, and exit */
175   trpc_free(trpc);
176
177   return 0;
178 }
179