]> rtime.felk.cvut.cz Git - orte.git/blobdiff - orte/manager/ortemanager.c
updated email address - petr@smoliku.cz
[orte.git] / orte / manager / ortemanager.c
index 1334480ea4de834060524e0a1121654133dd037b..16dde7403c8e29e48266d89d0700dc0026b6945b 100644 (file)
@@ -1,10 +1,20 @@
 /*
- *  $Id: ORTEManager.c,v 0.0.0.1        2003/10/07
+ *  $Id: ortemanager.c,v 0.0.0.1        2003/10/07
  *
  *  DEBUG:  section                     Manager
- *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
  *
- *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
+ *  -------------------------------------------------------------------  
+ *                                ORTE                                 
+ *                      Open Real-Time Ethernet                       
+ *                                                                    
+ *                      Copyright (C) 2001-2006                       
+ *  Department of Control Engineering FEE CTU Prague, Czech Republic  
+ *                      http://dce.felk.cvut.cz                       
+ *                      http://www.ocera.org                          
+ *                                                                    
+ *  Author:             Petr Smolik    petr@smoliku.cz             
+ *  Advisor:            Pavel Pisa                                   
+ *  Project Responsible: Zdenek Hanzalek                              
  *  --------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  */
  
-#include "orte.h"
+#include "orte_all.h"
+
+#ifdef HAVE_INTTYPES_H
+ #include <inttypes.h>
+ #if defined(PRIx32) && !defined(ORTE_PRI_HostId)
+  #define ORTE_PRI_HostId PRIx32
+  #define ORTE_PRI_AppId PRIx32
+ #endif
+#endif /*HAVE_INTTYPES_H*/
+
+#ifndef ORTE_PRI_HostId
+  #define ORTE_PRI_HostId x
+  #define ORTE_PRI_AppId x
+#endif
+
+#ifdef MAIN_RENAMED
+#define main ortemanager_main
+#define exit return
+#endif
 
 #ifndef CONFIG_ORTE_RT
+//global variables
+static ORTEDomain          *d;
+static ORTEDomainProp      dp;
+static int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
+static Boolean             orteDaemon=ORTE_FALSE;
+static Boolean             orteWinService=ORTE_FALSE;
+static ORTEDomainAppEvents *events=NULL;
+
+//event system
 Boolean
 onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
-  printf("%s 0x%x-0x%x was accepted\n",
+  printf("%s 0x%"ORTE_PRI_HostId"-0x%"ORTE_PRI_AppId" was accepted\n",
          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
          appInfo->hostId,appInfo->appId);
   return ORTE_TRUE;
@@ -32,26 +69,51 @@ onMgrAppRemoteNew(const struct ORTEAppInfo *appInfo, void *param) {
 
 void
 onMgrAppDelete(const struct ORTEAppInfo *appInfo, void *param) {
-  printf("%s 0x%x-0x%x was deleted\n",
+  printf("%s 0x%"ORTE_PRI_HostId"-0x%"ORTE_PRI_AppId" was deleted\n",
          (appInfo->appId & 0x3)==MANAGER ? "manager" : "application",
          appInfo->hostId,appInfo->appId);
 }
 
-#ifdef CONFIG_ORTE_UNIX
-pthread_mutex_t     mutex;
-void sig_usr(int signo) {
-  if ((signo==SIGTERM) || (signo==SIGINT)) {
-    pthread_mutex_unlock(&mutex);
-  }
+#ifdef _WIN32
+//Windows service support
+void serviceDispatchTable(void);  //forward declaration
+void removeService(void);         //forward declaration
+void installService(void);        //forward declaration
+int managerInit(void) {
+  d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
+  if (d==NULL) return -1;
+  return 0;
+}
+int managerStart(void) {
+  ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
+  return 0;
 }
+int managerStop(void) {
+  ORTEDomainMgrDestroy(d);
+  return 0;
+}
+#endif
+
+#ifdef CONFIG_ORTE_UNIX
+#ifdef HAVE_SYS_STAT_H
+  #include <sys/stat.h>                /* For umask() */
+#endif
+//Unix daemon support
 void waitForEndingCommand(void) {
-  pthread_mutex_init(&mutex, NULL);
-  pthread_mutex_lock(&mutex);
-  signal(SIGTERM,sig_usr);
-  signal(SIGINT,sig_usr);
-  pthread_mutex_lock(&mutex);
+       sigset_t sigset;
+       sigemptyset(&sigset);
+       sigaddset(&sigset, SIGINT);
+       sigaddset(&sigset, SIGTERM);
+       {
+       #ifdef HAVE_SIGWAITINFO
+               sigwaitinfo(&sigset, NULL);
+       #else /*HAVE_SIGWAITINFO*/
+               int sig;
+               sigwait(&sigset, &sig);
+       #endif /*HAVE_SIGWAITINFO*/
+       }
 }
-static int daemon_init(void) {
+static int daemonInit(void) {
   pid_t pid;
 
   if ((pid = fork()) < 0) {
@@ -71,15 +133,18 @@ static int daemon_init(void) {
 #endif
 
 static void usage(void) {
-  printf("usage: ORTEManager <parameters> \n");
+  printf("usage: ortemanager <parameters> \n");
   printf("  -p, --peer <IPAdd:IPAdd:...>  possible locations of fellow managers\n");
   printf("  -k, --key  <IPAdd:IPAdd:...>  manualy assigned manager's keys\n");
   printf("  -d, --domain <domain>         working manager domain\n");
   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
-  printf("      examples: ORTEManager -v 51,7:32,5 sections 51 and 32\n");
-  printf("                ORTEManager -v ALL,7     all sections up to level 7\n");
+  printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
+  printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
   printf("  -R, --refresh <s>             refresh period in second(s)\n");
   printf("  -P, --purge <s>               purge time in second(s)\n");
+  printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
+  printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
+  printf("  -L, --listen <IPAdd>          IP address to listen on\n");
 #ifdef CONFIG_ORTE_UNIX
   printf("  -D, --daemon                  start program like daemon\n");
 #endif
@@ -87,10 +152,15 @@ static void usage(void) {
   printf("  -e, --events                  register event system\n");
   printf("  -l, --logfile <filename>      set log file name\n");
   printf("  -V, --version                 show version\n");
+#ifdef _WIN32
+  printf("  -i, --install_service         install service into service manager on Windows\n");
+  printf("  -r, --remove_service          remove service from service manager on Windows\n");
+#endif
   printf("  -h, --help                    this usage screen\n");
 }
 
 int main(int argc,char *argv[]) {
+#if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
   static struct option long_opts[] = {
     { "peer",1,0, 'p' },
     { "key",1,0, 'k' },
@@ -98,6 +168,9 @@ int main(int argc,char *argv[]) {
     { "verbosity",1,0, 'v' },
     { "refresh",1,0, 'R' },
     { "purge",1,0, 'P' },
+    { "metaMulticast",1,0, 'I' },
+    { "timetolive",1,0, 't' },
+    { "listen",1,0, 'L' },
 #ifdef CONFIG_ORTE_UNIX
     { "daemon",1,0, 'D' },
 #endif
@@ -105,25 +178,27 @@ int main(int argc,char *argv[]) {
     { "events",0,0, 'e' },
     { "logfile",1,0, 'l' },
     { "version",0,0, 'V' },
+    { "install_service",0,0, 'i' },
+    { "remove_service",0,0, 'r' },
     { "help",  0, 0, 'h' },
     { 0, 0, 0, 0}
   };
-  ORTEDomain         *d;
-  ORTEDomainProp      dp;
-  int32_t             opt,domain=ORTE_DEFAULT_DOMAIN;
-  Boolean            daemon=ORTE_FALSE;
-  ORTEDomainAppEvents *events=NULL;
+#endif
 
   ORTEInit();
   ORTEDomainPropDefaultGet(&dp);
 
-  while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:l:VhDe",&long_opts[0], NULL)) != EOF) {
+#if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
+  while ((opt = getopt_long(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir",&long_opts[0], NULL)) != EOF) {
+#else
+  while ((opt = getopt(argc, argv, "k:p:d:v:R:E:P:I:t:L:l:VhDesir")) != EOF) {
+#endif
     switch (opt) {
       case 'p':
-        dp.mgrs=strdup(optarg);
+        dp.mgrs=optarg;
         break;
       case 'k':
-        dp.keys=strdup(optarg);
+        dp.keys=optarg;
         break;
       case 'd':
         domain=strtol(optarg,NULL,0);
@@ -137,6 +212,16 @@ int main(int argc,char *argv[]) {
       case 'P':
         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
         break;
+      case 'I':
+        dp.multicast.enabled=ORTE_TRUE;
+        dp.multicast.ipAddress=StringToIPAddress(optarg);
+        break;
+      case 'L':
+        dp.listen=StringToIPAddress(optarg);
+        break;
+      case 't':
+        dp.multicast.ttl=strtol(optarg,NULL,0);
+        break;
       case 'E':
         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
         break;
@@ -152,42 +237,56 @@ int main(int argc,char *argv[]) {
         ORTEVerbositySetLogFile(optarg);
         break;
       case 'V':
-        printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
+        printf("Open Real-Time Ethernet (%s).\n",dp.version);
         exit(0);
         break;
       case 'D':
-        daemon=ORTE_TRUE;
+        orteDaemon=ORTE_TRUE;
+        break;
+      #ifdef _WIN32
+      case 's':
+        serviceDispatchTable();
+        exit(0);
+        break;
+      case 'i':
+        installService();
+        orteWinService=ORTE_TRUE;
+        break;
+      case 'r':
+        removeService();
+        exit(0);
         break;
+      #endif
       case 'h':
       default:
         usage();
         exit(opt == 'h' ? 0 : 1);
     }
   }
-
+  
+  if (orteWinService) { 
+    exit(0);
+  }
+  
   d=ORTEDomainMgrCreate(domain,&dp,events,ORTE_TRUE);
-  if (!d)
+  if (!d) {
+    perror("ORTEDomainMgrCreate");
     exit(1);
+  }
 
   #ifdef CONFIG_ORTE_UNIX
-  if (daemon)
-    daemon_init();
+  if (orteDaemon)
+    daemonInit();
   #endif
 
-  ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE);
+  ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
   #ifndef CONFIG_ORTE_UNIX
-    d->taskSend.terminate=ORTE_FALSE;
-    ORTEAppSendThread(d);
+     while(1) ORTESleepMs(1000);
   #endif
-  ORTEDomainStart(d,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE);
 
   #ifdef CONFIG_ORTE_UNIX
   waitForEndingCommand();
   ORTEDomainMgrDestroy(d);
-  if (dp.mgrs) 
-    free(dp.mgrs);
-  if (dp.keys) 
-    free(dp.keys);
   if (events) 
     free(events);
   #endif
@@ -205,6 +304,17 @@ MODULE_LICENSE("GPL");
 ORTEDomain *d=NULL;
 pthread_t  thread;
 
+void *
+domainInit(void *arg) {
+  ORTEDomainProp dp;
+
+  ORTEDomainPropDefaultGet(&dp);
+  ORTEVerbositySetOptions(verbosity);
+  dp.mgrs=peer;
+  d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
+  return arg;
+}
+
 void *
 domainDestroy(void *arg) {
   if (!d) return NULL;
@@ -212,16 +322,13 @@ domainDestroy(void *arg) {
   return arg;
 }
 
-
 int
 init_module(void) {
-  ORTEDomainProp      dp;
-
   ORTEInit();
-  ORTEDomainPropDefaultGet(&dp);
-  ORTEVerbositySetOptions(verbosity);
-  dp.mgrs=peer;
-  d=ORTEDomainMgrCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_FALSE);
+  pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
+  pthread_join(thread,NULL);
+  if (d)
+    ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_FALSE,ORTE_FALSE,ORTE_TRUE); //manager start
   return 0;
 }
 void