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