Fixed newline characters throughout the code
[com/gs-lite.git] / src / lib / gscprts / rts_main.c
1 /* ------------------------------------------------
2  Copyright 2014 AT&T Intellectual Property
3  Licensed under the Apache License, Version 2.0 (the "License");
4  you may not use this file except in compliance with the License.
5  You may obtain a copy of the License at
6  
7  http://www.apache.org/licenses/LICENSE-2.0
8  
9  Unless required by applicable law or agreed to in writing, software
10  distributed under the License is distributed on an "AS IS" BASIS,
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  See the License for the specific language governing permissions and
13  limitations under the License.
14  ------------------------------------------- */
15
16 #include "gsconfig.h"
17 #include "gstypes.h"
18 #include "gshub.h"
19
20 #include "lapp.h"
21 #include "fta.h"
22 #include "lfta/rts.h"
23
24 #include "stdio.h"
25 #include "stdlib.h"
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <sys/ipc.h>
30 #include <sys/shm.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <sys/socket.h>
34
35 gs_retval_t main_csv(gs_int32_t devicenum, gs_sp_t device, gs_int32_t mapcnt, gs_sp_t map[]);
36 gs_retval_t main_csv2(gs_int32_t devicenum, gs_sp_t device, gs_int32_t mapcnt, gs_sp_t map[]);
37 gs_retval_t main_gdat(gs_int32_t devicenum, gs_sp_t device, gs_int32_t mapcnt, gs_sp_t map[]);
38 gs_retval_t main_dproto(gs_int32_t devicenum, gs_sp_t device, gs_int32_t mapcnt, gs_sp_t map[]);
39
40 int main (int argc, char* argv[]) {
41     gs_int32_t pid;
42     gs_int32_t x;
43     gs_int32_t y;
44     gs_sp_t* device;
45     gs_int32_t devcnt=0;
46     gs_sp_t* mappings;
47     gs_int32_t mapcnt=0;
48     gs_sp_t* lmap;
49     gs_int32_t lmapcnt=0;
50     FILE * cfg_file;
51     gs_int32_t tip1,tip2,tip3,tip4;
52     endpoint gshub;
53     gs_sp_t instance_name;
54     
55     
56         gsopenlog(argv[0]);
57     
58     if (setpgid(0,0)<0) {
59         gslog(LOG_EMERG,"Could not set process group id of rts");
60         exit(1);
61     }
62         
63     if (argc<4) {
64                 gslog(LOG_EMERG,"Wrong arguments at startup");
65         exit(1);
66     }
67     
68     /* allocate more than enough for each array */
69     if ((device=(gs_sp_t*)malloc(sizeof(gs_sp_t*) * argc))==0) {
70         gslog(LOG_EMERG,"malloc error");
71         exit(1);
72     }
73     if ((mappings=(gs_sp_t*)malloc(sizeof(gs_sp_t*) * argc))==0) {
74         gslog(LOG_EMERG,"malloc error");
75         exit(1);
76     }
77     if ((lmap=(gs_sp_t*)malloc(sizeof(gs_sp_t*) * argc))==0) {
78         gslog(LOG_EMERG,"malloc error");
79         exit(1);
80     }
81     
82     /* parse the arguments */
83     
84     if ((sscanf(argv[1],"%u.%u.%u.%u:%hu",&tip1,&tip2,&tip3,&tip4,&(gshub.port))!=5)) {
85         gslog(LOG_EMERG,"HUB IP NOT DEFINED");
86         exit(1);
87     }
88     gshub.ip=htonl(tip1<<24|tip2<<16|tip3<<8|tip4);
89     gshub.port=htons(gshub.port);
90     instance_name=strdup(argv[2]);
91     if (set_hub(gshub)!=0) {
92         gslog(LOG_EMERG,"Could not set hub");
93         exit(1);
94     }
95     if (set_instance_name(instance_name)!=0) {
96         gslog(LOG_EMERG,"Could not set instance name");
97         exit(1);
98     }
99     
100     for(x=3;x<argc;x++) {
101         if (strncmp("-D",argv[x],2)==0) {
102             /* macro definition */
103             y=2;
104             while((y<(strlen(argv[x])-1))&&(argv[x][y]!='=')) y++;
105             
106             
107             if (y<(strlen(argv[x])-1)) {
108                 /* if that is not true the define is empty and
109                  we ignore it otherwise we set the = to 0 to
110                  make two strings out of it*/
111                 argv[x][y]=0;
112                 mappings[mapcnt]=&(argv[x][2]);
113                 mapcnt++;
114             }
115         } else {
116             /* device definition */
117             device[devcnt]=argv[x];
118             devcnt++;
119         }
120     }
121     
122     if (devcnt==0) {
123         gslog(LOG_EMERG,"at least one device has to be specified");
124         exit(1);
125     }
126         
127     
128     /* now startup all the device dependend processes. */
129     
130     for (x=0;x<devcnt;x++) {
131         if ((pid=fork())==-1) {
132             gslog(LOG_EMERG,"fork error");
133             exit(1);
134         }
135         
136         if (pid==0) {
137             gs_sp_t interfacetype;
138             /* wait for clearinghouse to finish startup */
139             sleep(2);
140             for(y=0;y<mapcnt;y++) {
141                 if (strcmp(device[x],&mappings[y][strlen(mappings[y])+1])==0) {
142                     /* point to the second string */
143                     lmap[lmapcnt]=mappings[y];
144                     lmapcnt++;
145                 }
146             }
147             /* the devicename always matches */
148             lmap[lmapcnt]=device[x];
149             lmapcnt++;
150             
151             if ((interfacetype=get_iface_properties(device[x],(gs_sp_t)"interfacetype"))==0) {
152                 gslog(LOG_EMERG,"Interface Type not configured but required");
153                 exit(1);
154             }
155             
156             if (strcmp(interfacetype,"CSV")==0) {
157                 main_csv(x,device[x],lmapcnt,lmap);        
158             } else {
159                         if (strncmp(interfacetype,"GDAT",4)==0) {
160                     main_gdat(x,device[x],lmapcnt,lmap);
161                                 }else{
162                                 if (strncmp(interfacetype,"PROTO",5)==0) {
163                         main_dproto(x,device[x],lmapcnt,lmap);
164                         } else {
165                                                 fprintf(stderr,"interface type %s not recognized\n",interfacetype);
166                         gslog(LOG_ERR,"UNKNOWN InterfaceType\n");
167                         exit(0);
168                         }
169                                 }
170             }
171             
172             /* should never return */
173             gslog(LOG_EMERG,"lfta init returned");
174             exit(1);        
175         }
176     }
177     
178     /* initalize host_lib */
179     
180     gslog(LOG_INFO,"Init host lib in clearinghouse");
181     
182     if (hostlib_init(CLEARINGHOUSE,0,DEFAULTDEV,0,0)<0) {
183         gslog(LOG_EMERG,"%s::error:could not initiate host lib for clearinghouse\n",
184               argv[0]);
185         exit(7);
186     }
187     
188     
189     
190     /* start processing messages should never return*/
191     if (fta_start_service(-1)<0) {
192         gslog(LOG_EMERG,"error in processing the msg queue");
193         exit(9);
194     }
195     gslog(LOG_EMERG,"%s::error:start service returned");
196     return 0;
197 }
198