2 * $Id: ortemanager.c,v 0.0.0.1 2003/10/07
4 * DEBUG: section Manager
5 * AUTHOR: Petr Smolik petr.smolik@wo.cz
7 * ORTE - OCERA Real-Time Ethernet http://www.ocera.org/
8 * --------------------------------------------------------------------
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.
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.
24 #ifndef CONFIG_ORTE_RT
28 int32_t opt,domain=ORTE_DEFAULT_DOMAIN;
29 Boolean orteDaemon=ORTE_FALSE;
30 Boolean orteWinService=ORTE_FALSE;
31 ORTEDomainAppEvents *events=NULL;
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);
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);
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;
59 int managerStart(void) {
60 ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
63 int managerStop(void) {
64 ORTEDomainMgrDestroy(d);
69 #ifdef CONFIG_ORTE_UNIX
71 pthread_mutex_t mutex; //for wake up
72 pthread_cond_t cond; //for wake up
74 void sig_usr(int signo) {
75 if ((signo==SIGTERM) || (signo==SIGINT)) {
76 pthread_mutex_lock(&mutex);
78 pthread_cond_signal(&cond);
79 pthread_mutex_unlock(&mutex);
82 void waitForEndingCommand(void) {
83 pthread_mutex_init(&mutex, NULL);
84 pthread_cond_init(&cond, NULL);
86 signal(SIGTERM,sig_usr);
87 signal(SIGINT,sig_usr);
88 pthread_mutex_lock(&mutex);
90 pthread_cond_wait(&cond,&mutex);
91 pthread_mutex_unlock(&mutex);
92 pthread_mutex_destroy(&mutex);
93 pthread_cond_destroy(&cond);
95 static int daemonInit(void) {
98 if ((pid = fork()) < 0) {
102 exit(0); /* parent vanishes */
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");
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");
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");
137 printf(" -h, --help this usage screen\n");
140 int main(int argc,char *argv[]) {
141 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
142 static struct option long_opts[] = {
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' },
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' },
166 ORTEDomainPropDefaultGet(&dp);
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) {
171 while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t:l:VhDesir")) != EOF) {
181 domain=strtol(optarg,NULL,0);
184 ORTEVerbositySetOptions(optarg);
187 NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
190 NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
193 dp.multicast.enabled=ORTE_TRUE;
194 dp.multicast.ipAddress=StringToIPAddress(optarg);
197 dp.multicast.ttl=strtol(optarg,NULL,0);
200 NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
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;
211 ORTEVerbositySetLogFile(optarg);
214 printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
218 orteDaemon=ORTE_TRUE;
222 serviceDispatchTable();
227 orteWinService=ORTE_TRUE;
237 exit(opt == 'h' ? 0 : 1);
241 if (orteWinService) {
245 d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
249 #ifdef CONFIG_ORTE_UNIX
254 ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
255 #ifndef CONFIG_ORTE_UNIX
256 while(1) ORTESleepMs(1000);
259 #ifdef CONFIG_ORTE_UNIX
260 waitForEndingCommand();
261 ORTEDomainMgrDestroy(d);
270 MODULE_PARM(verbosity,"1s");
271 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
273 MODULE_PARM(peer,"1s");
274 MODULE_PARM_DESC(peer,"possible locations of fellow managers");
275 MODULE_LICENSE("GPL");
280 domainInit(void *arg) {
283 ORTEDomainPropDefaultGet(&dp);
284 ORTEVerbositySetOptions(verbosity);
286 d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
291 domainDestroy(void *arg) {
293 ORTEDomainMgrDestroy(d);
300 pthread_create(&thread,NULL,&domainInit,NULL); //allocate resources in RT
301 pthread_join(thread,NULL);
303 ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE); //manager start
307 cleanup_module(void) {
309 pthread_create(&thread,NULL,&domainDestroy,NULL);
310 pthread_join(thread,NULL);