]> rtime.felk.cvut.cz Git - orte.git/blob - orte/manager/ORTEManager.c
98e344daa0b082b10a9c6fe59136f1d8df93b533
[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 #ifdef HAVE_CONFIG_H
22   #include <orte_config.h>
23 #endif
24
25 #ifdef CONFIG_ORTE_RTAI
26   #include <linux/module.h>
27   #include <rtai.h>
28   #include <rtai_sched.h>
29   #include <rtai_sem.h>
30 #else
31   #include <stdio.h>
32   #include <stdlib.h>
33   #include <string.h>
34   #ifdef _WIN32
35     #include <getopt.h>
36   #elif defined __RTL__
37   #else
38     #include <sys/types.h>
39     #include <sys/stat.h>
40     #include <pthread.h>
41     #include <getopt.h>
42     #include <signal.h>
43     #define _UNIX
44   #endif
45 #endif
46
47 #include "orte_api.h"
48
49 #ifndef CONFIG_ORTE_RT
50 Boolean
51 onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
52   printf("%s 0x%x-0x%x was accepted\n",
53          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
54          appInfo->hostId,appInfo->appId);
55   return ORTE_TRUE;
56 }
57
58 void
59 onMgrAppDelete(const struct ORTEAppInfo *appInfo, void *param) {
60   printf("%s 0x%x-0x%x was deleted\n",
61          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
62          appInfo->hostId,appInfo->appId);
63 }
64
65 #ifdef _UNIX
66 pthread_mutex_t     mutex;
67 void sig_usr(int signo) {
68   if ((signo==SIGTERM) || (signo==SIGINT)) {
69     pthread_mutex_unlock(&mutex);
70   }
71 }
72 void waitForEndingCommand(void) {
73   pthread_mutex_init(&mutex, NULL);
74   pthread_mutex_lock(&mutex);
75   signal(SIGTERM,sig_usr);
76   signal(SIGINT,sig_usr);
77   pthread_mutex_lock(&mutex);
78 }
79 static int daemon_init(void) {
80   pid_t pid;
81
82   if ((pid = fork()) < 0) {
83     return -1;
84   } else
85     if (pid != 0) {
86       exit(0);  /* parent vanishes */
87     }
88   /* child process */
89   setsid();
90   umask(0);
91   close(0);
92   close(1);
93   close(2);
94   return 0;
95 }
96 #endif
97
98 static void usage(void) {
99   printf("usage: ORTEManager <parameters> \n");
100   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
101   printf("  -k, --key  <IPAdd>            manager's additional key\n");
102   printf("  -d, --domain <domain>         working manager domain\n");
103   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
104   printf("      examples: ORTEManager -v 51,7:32,5 sections 51 and 32\n");
105   printf("                ORTEManager -v ALL,7     all sections up to level 7\n");
106   printf("  -R, --refresh <s>             refresh period in second(s)\n");
107   printf("  -P, --purge <s>               purge time in second(s)\n");
108 #ifdef _UNIX
109   printf("  -D, --daemon                  start program like daemon\n");
110 #endif
111   printf("  -E, --expiration <s>          expiration time of manager in second(s)\n");
112   printf("  -e, --events                  register event system\n");
113   printf("  -l, --logfile <filename>      set log file name\n");
114   printf("  -V, --version                 show version\n");
115   printf("  -h, --help                    this usage screen\n");
116 }
117
118 int main(int argc,char *argv[]) {
119   static struct option long_opts[] = {
120     { "peer",1,0, 'p' },
121     { "key",1,0, 'k' },
122     { "domain",1,0, 'd' },
123     { "verbosity",1,0, 'v' },
124     { "refresh",1,0, 'R' },
125     { "purge",1,0, 'P' },
126 #ifdef _UNIX
127     { "daemon",1,0, 'D' },
128 #endif
129     { "expiration",1,0, 'E' },
130     { "events",0,0, 'e' },
131     { "logfile",1,0, 'l' },
132     { "version",0,0, 'V' },
133     { "help",  0, 0, 'h' },
134     { 0, 0, 0, 0}
135   };
136   ORTEDomain                  *d;
137   ORTEDomainProp            dp;
138   int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
139   Boolean                         daemon=ORTE_FALSE,start_sending_thread=ORTE_FALSE;
140   ORTEDomainAppEvents *events=NULL;
141
142   #ifdef _UNIX
143   start_sending_thread=ORTE_TRUE;
144   #endif
145
146   ORTEInit();
147   ORTEDomainPropDefaultGet(&dp);
148
149   while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:l:VhDe",&long_opts[0], NULL)) != EOF) {
150     switch (opt) {
151       case 'p':
152         dp.mgrs=strdup(optarg);
153         break;
154       case 'k':
155         dp.mgrAddKey=StringToIPAddress(strdup(optarg));
156         break;
157       case 'd':
158         domain=strtol(optarg,NULL,0);
159         break;
160       case 'v':
161         ORTEVerbositySetOptions(optarg);
162         break;
163       case 'R':
164         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
165         break;
166       case 'P':
167         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
168         break;
169       case 'E':
170         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
171         break;
172       case 'e':
173         events=(ORTEDomainAppEvents*)malloc(sizeof(ORTEDomainAppEvents));
174         ORTEDomainInitEvents(events);
175         events->onMgrNew=onMgrAppRemoteNew;
176         events->onAppRemoteNew=onMgrAppRemoteNew;
177         events->onMgrDelete=onMgrAppDelete;
178         events->onAppDelete=onMgrAppDelete;
179         break;
180       case 'l':
181         ORTEVerbositySetLogFile(optarg);
182         break;
183       case 'V':
184         printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
185         exit(0);
186         break;
187       case 'D':
188         daemon=ORTE_TRUE;
189         break;
190       case 'h':
191       default:
192         usage();
193         exit(opt == 'h' ? 0 : 1);
194     }
195   }
196
197   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
198   if (!d)
199     exit(1);
200
201   #ifdef _UNIX
202   if (daemon)
203     daemon_init();
204   #endif
205
206   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE);
207   if (!start_sending_thread) {
208     ORTEAppSendThread(d);
209   }
210   ORTEDomainStart(d,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
211
212   #ifdef _UNIX
213   waitForEndingCommand();
214   ORTEDomainMgrDestroy(d);
215   if (events)
216     free(events);
217   #endif
218
219   exit(0);
220 }
221 #else
222 MODULE_LICENSE("GPL");
223 ORTEDomain          *d;
224
225 void
226 createManager(void) {
227   ORTEInit();
228   ORTEVerbositySetOptions("ALL,2");
229   d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,NULL,NULL,ORTE_FALSE);
230 }
231
232 int
233 init_module(void) {
234   createManager();
235   return 0;
236 }
237 void
238 cleanup_module(void) {
239   if (d)
240     ORTEDomainMgrDestroy(d);
241 }
242 #endif