]> rtime.felk.cvut.cz Git - orte.git/blob - orte/manager/ortemanager.c
bc42bfeddbe3636e739e309a367fb6abc229e025
[orte.git] / orte / manager / ortemanager.c
1 /*
2  *  $Id: ortemanager.c,v 0.0.0.1        2003/10/07
3  *
4  *  DEBUG:  section                     Manager
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr.smolik@wo.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  */
31  
32 #include "orte_all.h"
33
34 #ifdef HAVE_INTTYPES_H
35  #include <inttypes.h>
36  #if defined(PRIx32) && !defined(ORTE_PRI_HostId)
37   #define ORTE_PRI_HostId PRIx32
38   #define ORTE_PRI_AppId PRIx32
39  #endif
40 #endif /*HAVE_INTTYPES_H*/
41
42 #ifndef ORTE_PRI_HostId
43   #define ORTE_PRI_HostId x
44   #define ORTE_PRI_AppId x
45 #endif
46
47 #ifdef MAIN_RENAMED
48 #define main ortemanager_main
49 #define exit return
50 #endif
51
52 #ifndef CONFIG_ORTE_RT
53 //global variables
54 static ORTEDomain          *d;
55 static ORTEDomainProp      dp;
56 static int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
57 static Boolean             orteDaemon=ORTE_FALSE;
58 static Boolean             orteWinService=ORTE_FALSE;
59 static ORTEDomainAppEvents *events=NULL;
60
61 //event system
62 Boolean
63 onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
64   printf("%s 0x%"ORTE_PRI_HostId"-0x%"ORTE_PRI_AppId" was accepted\n",
65          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
66          appInfo->hostId,appInfo->appId);
67   return ORTE_TRUE;
68 }
69
70 void
71 onMgrAppDelete(const struct ORTEAppInfo *appInfo, void *param) {
72   printf("%s 0x%"ORTE_PRI_HostId"-0x%"ORTE_PRI_AppId" was deleted\n",
73          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
74          appInfo->hostId,appInfo->appId);
75 }
76
77 #ifdef _WIN32
78 //Windows service support
79 void serviceDispatchTable(void);  //forward declaration
80 void removeService(void);         //forward declaration
81 void installService(void);        //forward declaration
82 int managerInit(void) {
83   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
84   if (d==NULL) return -1;
85   return 0;
86 }
87 int managerStart(void) {
88   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
89   return 0;
90 }
91 int managerStop(void) {
92   ORTEDomainMgrDestroy(d);
93   return 0;
94 }
95 #endif
96
97 #ifdef CONFIG_ORTE_UNIX
98 #ifdef HAVE_SYS_STAT_H
99   #include <sys/stat.h>         /* For umask() */
100 #endif
101 //Unix daemon support
102 void waitForEndingCommand(void) {
103         sigset_t sigset;
104         sigemptyset(&sigset);
105         sigaddset(&sigset, SIGINT);
106         sigaddset(&sigset, SIGTERM);
107         {
108         #ifdef HAVE_SIGWAITINFO
109                 sigwaitinfo(&sigset, NULL);
110         #else /*HAVE_SIGWAITINFO*/
111                 int sig;
112                 sigwait(&sigset, &sig);
113         #endif /*HAVE_SIGWAITINFO*/
114         }
115 }
116 static int daemonInit(void) {
117   pid_t pid;
118
119   if ((pid = fork()) < 0) {
120     return -1;
121   } else
122     if (pid != 0) {
123       exit(0);  /* parent vanishes */
124     }
125   /* child process */
126   setsid();
127   umask(0);
128   close(0);
129   close(1);
130   close(2);
131   return 0;
132 }
133 #endif
134
135 static void usage(void) {
136   printf("usage: ortemanager <parameters> \n");
137   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
138   printf("  -k, --key  <IPAdd:IPAdd:...>  manualy assigned manager's keys\n");
139   printf("  -d, --domain <domain>         working manager domain\n");
140   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
141   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
142   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
143   printf("  -R, --refresh <s>             refresh period in second(s)\n");
144   printf("  -P, --purge <s>               purge time in second(s)\n");
145   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
146   printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
147   printf("  -L, --listen <IPAdd>          IP address to listen on\n");
148 #ifdef CONFIG_ORTE_UNIX
149   printf("  -D, --daemon                  start program like daemon\n");
150 #endif
151   printf("  -E, --expiration <s>          expiration time of manager in second(s)\n");
152   printf("  -e, --events                  register event system\n");
153   printf("  -l, --logfile <filename>      set log file name\n");
154   printf("  -V, --version                 show version\n");
155 #ifdef _WIN32
156   printf("  -i, --install_service         install service into service manager on Windows\n");
157   printf("  -r, --remove_service          remove service from service manager on Windows\n");
158 #endif
159   printf("  -h, --help                    this usage screen\n");
160 }
161
162 int main(int argc,char *argv[]) {
163 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
164   static struct option long_opts[] = {
165     { "peer",1,0, 'p' },
166     { "key",1,0, 'k' },
167     { "domain",1,0, 'd' },
168     { "verbosity",1,0, 'v' },
169     { "refresh",1,0, 'R' },
170     { "purge",1,0, 'P' },
171     { "metaMulticast",1,0, 'I' },
172     { "timetolive",1,0, 't' },
173     { "listen",1,0, 'L' },
174 #ifdef CONFIG_ORTE_UNIX
175     { "daemon",1,0, 'D' },
176 #endif
177     { "expiration",1,0, 'E' },
178     { "events",0,0, 'e' },
179     { "logfile",1,0, 'l' },
180     { "version",0,0, 'V' },
181     { "install_service",0,0, 'i' },
182     { "remove_service",0,0, 'r' },
183     { "help",  0, 0, 'h' },
184     { 0, 0, 0, 0}
185   };
186 #endif
187
188   ORTEInit();
189   ORTEDomainPropDefaultGet(&dp);
190
191 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
192   while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir",&long_opts[0], NULL)) != EOF) {
193 #else
194   while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir")) != EOF) {
195 #endif
196     switch (opt) {
197       case 'p':
198         dp.mgrs=optarg;
199         break;
200       case 'k':
201         dp.keys=optarg;
202         break;
203       case 'd':
204         domain=strtol(optarg,NULL,0);
205         break;
206       case 'v':
207         ORTEVerbositySetOptions(optarg);
208         break;
209       case 'R':
210         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
211         break;
212       case 'P':
213         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
214         break;
215       case 'I':
216         dp.multicast.enabled=ORTE_TRUE;
217         dp.multicast.ipAddress=StringToIPAddress(optarg);
218         break;
219       case 'L':
220         dp.listen=StringToIPAddress(optarg);
221         break;
222       case 't':
223         dp.multicast.ttl=strtol(optarg,NULL,0);
224         break;
225       case 'E':
226         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
227         break;
228       case 'e':
229         events=(ORTEDomainAppEvents*)malloc(sizeof(ORTEDomainAppEvents));
230         ORTEDomainInitEvents(events);
231         events->onMgrNew=onMgrAppRemoteNew;
232         events->onAppRemoteNew=onMgrAppRemoteNew;
233         events->onMgrDelete=onMgrAppDelete;
234         events->onAppDelete=onMgrAppDelete;
235         break;
236       case 'l':
237         ORTEVerbositySetLogFile(optarg);
238         break;
239       case 'V':
240         printf("Open Real-Time Ethernet (%s).\n",dp.version);
241         exit(0);
242         break;
243       case 'D':
244         orteDaemon=ORTE_TRUE;
245         break;
246       #ifdef _WIN32
247       case 's':
248         serviceDispatchTable();
249         exit(0);
250         break;
251       case 'i':
252         installService();
253         orteWinService=ORTE_TRUE;
254         break;
255       case 'r':
256         removeService();
257         exit(0);
258         break;
259       #endif
260       case 'h':
261       default:
262         usage();
263         exit(opt == 'h' ? 0 : 1);
264     }
265   }
266   
267   if (orteWinService) { 
268     exit(0);
269   }
270   
271   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
272   if (!d) {
273     perror("ORTEDomainMgrCreate");
274     exit(1);
275   }
276
277   #ifdef CONFIG_ORTE_UNIX
278   if (orteDaemon)
279     daemonInit();
280   #endif
281
282   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
283   #ifndef CONFIG_ORTE_UNIX
284      while(1) ORTESleepMs(1000);
285   #endif
286
287   #ifdef CONFIG_ORTE_UNIX
288   waitForEndingCommand();
289   ORTEDomainMgrDestroy(d);
290   if (events) 
291     free(events);
292   #endif
293
294   exit(0);
295 }
296 #else
297 char *verbosity="";
298 MODULE_PARM(verbosity,"1s");
299 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
300 char *peer="";
301 MODULE_PARM(peer,"1s");
302 MODULE_PARM_DESC(peer,"possible locations of fellow managers");
303 MODULE_LICENSE("GPL");
304 ORTEDomain *d=NULL;
305 pthread_t  thread;
306
307 void *
308 domainInit(void *arg) {
309   ORTEDomainProp dp;
310
311   ORTEDomainPropDefaultGet(&dp);
312   ORTEVerbositySetOptions(verbosity);
313   dp.mgrs=peer;
314   d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
315   return arg;
316 }
317
318 void *
319 domainDestroy(void *arg) {
320   if (!d) return NULL;
321   ORTEDomainMgrDestroy(d);
322   return arg;
323 }
324
325 int
326 init_module(void) {
327   ORTEInit();
328   pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
329   pthread_join(thread,NULL);
330   if (d)
331     ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE); //manager start
332   return 0;
333 }
334 void
335 cleanup_module(void) {
336   if (!d) return;
337   pthread_create(&thread,NULL,&domainDestroy,NULL);
338   pthread_join(thread,NULL);
339 }
340 #endif