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