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