]> rtime.felk.cvut.cz Git - orte.git/blob - orte/manager/ortemanager.c
4b9bfb5d6c5a3846e5d021b68a4c8a7a68aa1ad4
[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  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  */
21  
22 #include "orte_all.h"
23
24 #ifndef CONFIG_ORTE_RT
25 //global variables
26 ORTEDomain          *d;
27 ORTEDomainProp      dp;
28 int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
29 Boolean             orteDaemon=ORTE_FALSE;
30 Boolean             orteWinService=ORTE_FALSE;
31 ORTEDomainAppEvents *events=NULL;
32
33 //event system
34 Boolean
35 onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
36   printf("%s 0x%x-0x%x was accepted\n",
37          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
38          appInfo->hostId,appInfo->appId);
39   return ORTE_TRUE;
40 }
41
42 void
43 onMgrAppDelete(const struct ORTEAppInfo *appInfo, void *param) {
44   printf("%s 0x%x-0x%x was deleted\n",
45          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
46          appInfo->hostId,appInfo->appId);
47 }
48
49 #ifdef _WIN32
50 //Windows service support
51 void serviceDispatchTable(void);  //forward declaration
52 void removeService(void);         //forward declaration
53 void installService(void);        //forward declaration
54 int managerInit(void) {
55   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
56   if (d==NULL) return -1;
57   return 0;
58 }
59 int managerStart(void) {
60   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
61   return 0;
62 }
63 int managerStop(void) {
64   ORTEDomainMgrDestroy(d);
65   return 0;
66 }
67 #endif
68
69 #ifdef CONFIG_ORTE_UNIX
70 //Unix daemon support
71 pthread_mutex_t     mutex; //for wake up
72 pthread_cond_t      cond; //for wake up
73 int                 cvalue;
74 void sig_usr(int signo) {
75   if ((signo==SIGTERM) || (signo==SIGINT)) {
76     pthread_mutex_lock(&mutex);
77     cvalue=1;
78     pthread_cond_signal(&cond);
79     pthread_mutex_unlock(&mutex);
80   }
81 }
82 void waitForEndingCommand(void) {
83   pthread_mutex_init(&mutex, NULL);
84   pthread_cond_init(&cond, NULL);
85   cvalue=0;
86   signal(SIGTERM,sig_usr);
87   signal(SIGINT,sig_usr);
88   pthread_mutex_lock(&mutex);
89   while(cvalue==0)
90     pthread_cond_wait(&cond,&mutex);
91   pthread_mutex_unlock(&mutex);
92   pthread_mutex_destroy(&mutex);
93   pthread_cond_destroy(&cond);
94 }
95 static int daemonInit(void) {
96   pid_t pid;
97
98   if ((pid = fork()) < 0) {
99     return -1;
100   } else
101     if (pid != 0) {
102       exit(0);  /* parent vanishes */
103     }
104   /* child process */
105   setsid();
106   umask(0);
107   close(0);
108   close(1);
109   close(2);
110   return 0;
111 }
112 #endif
113
114 static void usage(void) {
115   printf("usage: ortemanager <parameters> \n");
116   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
117   printf("  -k, --key  <IPAdd:IPAdd:...>  manualy assigned manager's keys\n");
118   printf("  -d, --domain <domain>         working manager domain\n");
119   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
120   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
121   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
122   printf("  -R, --refresh <s>             refresh period in second(s)\n");
123   printf("  -P, --purge <s>               purge time in second(s)\n");
124   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
125   printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
126 #ifdef CONFIG_ORTE_UNIX
127   printf("  -D, --daemon                  start program like daemon\n");
128 #endif
129   printf("  -E, --expiration <s>          expiration time of manager in second(s)\n");
130   printf("  -e, --events                  register event system\n");
131   printf("  -l, --logfile <filename>      set log file name\n");
132   printf("  -V, --version                 show version\n");
133 #ifdef _WIN32
134   printf("  -i, --install_service         install service into service manager on Windows\n");
135   printf("  -r, --remove_service          remove service from service manager on Windows\n");
136 #endif
137   printf("  -h, --help                    this usage screen\n");
138 }
139
140 int main(int argc,char *argv[]) {
141 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
142   static struct option long_opts[] = {
143     { "peer",1,0, 'p' },
144     { "key",1,0, 'k' },
145     { "domain",1,0, 'd' },
146     { "verbosity",1,0, 'v' },
147     { "refresh",1,0, 'R' },
148     { "purge",1,0, 'P' },
149     { "metaMulticast",1,0, 'I' },
150     { "timetolive",1,0, 't' },
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:VhDesir",&long_opts[0], NULL)) != EOF) {
170 #else
171   while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t: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 '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("Ocera 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     exit(1);
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