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