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