1 #include <stdio.h>
2 #include "machdefs.h"
3 #include "util.h"
4 #include "tsp.h"
5 
6 static int
7     receive_dominos (CC_SFILE *s),
8     parseargs (int ac, char **av);
9 
10 static void
11     usage (char *f);
12 
13 static int graphid = 1;
14 static int sendgraph = 0;
15 static int stopboss = 0;
16 static int getdominos = 0;
17 static char *hostname = (char *) NULL;
18 
main(int ac,char ** av)19 int main (int ac, char **av)
20 {
21     CC_SFILE *s = (CC_SFILE *) NULL;
22     int rval = 0;
23 
24     rval = parseargs (ac, av);
25     if (rval) return 0;
26 
27     CCutil_printlabel ();
28 
29     s = CCutil_snet_open (hostname, CCtsp_DOMINO_PORT);
30     if (!s) {
31         fprintf (stderr, "CCutil_snet_open failed\n");
32         rval = 1;  goto CLEANUP;
33     }
34 
35     if (stopboss) {
36         printf ("stop the boss\n");  fflush (stdout);
37         rval = CCutil_swrite_char (s, CCtsp_DOMINO_EXIT);
38         CCcheck_rval (rval, "CCutil_swrite_char failed (EXIT)");
39     } else if (sendgraph) {
40         printf ("Send graph id = %d\n", graphid);  fflush (stdout);
41         rval = CCutil_swrite_char (s, CCtsp_DOMINO_GRAPH);
42         CCcheck_rval (rval, "CCutil_swrite_char failed (GRAPH)");
43         rval = CCutil_swrite_int (s, graphid);
44         CCcheck_rval (rval, "CCutil_swrite_int failed");
45         rval = CCutil_swrite_int (s, 2*graphid);
46         CCcheck_rval (rval, "CCutil_swrite_int failed");
47         rval = CCutil_swrite_int (s, 3*graphid);
48         CCcheck_rval (rval, "CCutil_swrite_int failed");
49     } else if (getdominos) {
50         printf ("get the domino list\n");  fflush (stdout);
51         rval = CCutil_swrite_char (s, CCtsp_DOMINO_SEND);
52         CCcheck_rval (rval, "CCutil_swrite_char failed (SEND)");
53         rval = receive_dominos (s);
54         CCcheck_rval (rval, "receive_dominos failed");
55     } else {
56         printf ("Nothing to do.\n"); fflush (stdout);
57     }
58 
59 CLEANUP:
60 
61     if (s) CCutil_sclose (s);
62     return rval;
63 }
64 
parseargs(int ac,char ** av)65 static int parseargs (int ac, char **av)
66 {
67     int c;
68     int boptind = 1;
69     char *boptarg = (char *) NULL;
70 
71     while ((c = CCutil_bix_getopt (ac, av, "xg:d", &boptind, &boptarg)) != EOF) {
72         switch (c) {
73         case 'x':
74             stopboss = 1;
75             break;
76         case 'g':
77             sendgraph = 1;
78             graphid = atoi(boptarg);
79             break;
80         case 'd':
81             getdominos = 1;
82             break;
83         default:
84             usage (av[0]);
85             return 1;
86         }
87     }
88 
89     if (boptind >= ac) {
90         usage (av[0]);
91         return 1;
92     }
93     hostname = av[boptind++];
94     if (boptind != ac) {
95         usage (av[0]);
96         return 1;
97     }
98 
99     return 0;
100 }
101 
receive_dominos(CC_SFILE * s)102 static int receive_dominos (CC_SFILE *s)
103 {
104     int rval = 0;
105     int i, count;
106     int *list = (int *) NULL;
107 
108     rval = CCutil_sread_int (s, &count);
109     CCcheck_rval (rval, "CCutil_sread_int failed (count)");
110 
111     list = CC_SAFE_MALLOC (count, int);
112     CCcheck_NULL (list, "out memory for list");
113 
114     for (i = 0; i < count; i++) {
115         rval = CCutil_sread_int (s, &list[i]);
116         CCcheck_rval (rval, "CCutil_sread_int failed (list)");
117     }
118 
119     printf ("Dom List: %d\n", count);
120     for (i = 0; i < count; i++) {
121         printf ("%d ", list[i]);
122     }
123     printf ("\n"); fflush (stdout);
124 
125 CLEANUP:
126 
127     CC_IFFREE (list, int);
128     return rval;
129 }
130 
usage(char * f)131 static void usage (char *f)
132 {
133     fprintf (stderr, "Usage: %s [-see below-] hostname\n", f);
134     fprintf (stderr, "   -d   tells the boss to send dominos\n");
135     fprintf (stderr, "   -g # sends graph is id #\n");
136     fprintf (stderr, "   -x   tells the boss to exit\n");
137 }
138