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