]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/spy/ortespy.c
cfcc751aa5bd9d6671bbb2cdd1c022c1de2f1f3e
[orte.git] / orte / examples / spy / ortespy.c
1 /*
2  *  $Id: ortespy.c,v 0.0.0.1            2003/10/07 
3  *
4  *  DEBUG:  section                     ortespy
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr@smoliku.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *  
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *  
30  */ 
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #ifndef _WIN32
35   #include <signal.h>
36 #endif
37 #include "orte.h"
38 #ifdef HAVE_CONFIG_H
39   #ifdef HAVE_GETOPT_H
40     #include <getopt.h>
41   #endif
42   #ifdef HAVE_UNISTD_H
43     #include <unistd.h> //getopt.h for DarWin, Solaris, ...
44   #endif
45 #else
46   #include <getopt.h>
47 #endif
48 #include "string.h"
49
50 #ifdef MAIN_RENAMED
51 #define main orte_spy_main
52 #define exit return
53 #endif
54
55 static ORTEDomain              *d;
56 static NtpTime                 deadline,minimumSeparation;
57 static int32_t                 instanceRecv;
58
59 static void
60 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam) {
61   char lbuff[MAX_STRING_NTPTIME_LENGTH];
62   char rbuff[MAX_STRING_NTPTIME_LENGTH];
63   switch (info->status) {
64     case NEW_DATA:
65       printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n",
66              "NEW_DATA",
67              info->topic,
68              info->type,
69              NtpTimeToStringUs(info->localTimeReceived, lbuff),
70              NtpTimeToStringUs(info->remoteTimePublished, rbuff));
71       break;
72     case DEADLINE:
73 //      printf("deadline occurred\n");
74       break;
75   }
76 }
77
78 ORTESubscription*
79 subscriptionCallBack(const char *topic, const char *type, void *param) {
80   ORTETypeRegisterAdd(d,type,NULL,NULL,NULL,0);   
81   if (strcmp((const char *)topic, (const char*)"Red")==0) return NULL;
82   return ORTESubscriptionCreate(
83         d,
84         IMMEDIATE,
85         BEST_EFFORTS,
86         topic,
87         type,
88         &instanceRecv,
89         &deadline,
90         &minimumSeparation,
91         recvCallBack,
92         NULL,
93         IPADDRESS_INVALID);
94 }
95
96 static void usage(void) {
97   printf("usage: ortespy <parameters> \n");
98   printf("  -d, --domain <domain>         working manager domain\n");
99   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
100   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
101   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
102   printf("  -R, --refresh <s>             refresh period in second(s)\n");
103   printf("  -P, --purge <s>               purge time in second(s)\n");
104   printf("  -e, --expiration <s>          expiration time of manager in second(s)\n");
105   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
106   printf("  -l, --logfile <filename>      set log file name\n");
107   printf("  -V, --version                 show version\n");
108   printf("  -h, --help                    this usage screen\n");
109 }
110
111 int main(int argc,char *argv[]) {
112 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
113   static struct option long_opts[] = {
114     { "domain",1,0, 'd' },
115     { "verbosity",1,0, 'v' },
116     { "refresh",1,0, 'R' },
117     { "purge",1,0, 'P' },
118     { "expiration",1,0, 'E' },
119     { "metaMulticast",1,0, 'I' },
120     { "logfile",1,0, 'l' },
121     { "version",0,0, 'V' },
122     { "help",  0, 0, 'h' },
123     { 0, 0, 0, 0}
124   };
125 #endif
126   ORTEDomainProp          dp; 
127   int                     opt,domain=ORTE_DEFAULT_DOMAIN;
128   
129   ORTEInit();
130   ORTEDomainPropDefaultGet(&dp);
131   NTPTIME_BUILD(deadline,3); 
132   NTPTIME_BUILD(minimumSeparation,0); 
133
134 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
135   while ((opt = getopt_long(argc, argv, "d:v:R:E:I:P:l:Vh",&long_opts[0], NULL)) != EOF) {
136 #else
137   while ((opt = getopt(argc, argv, "d:v:R:E:I:P:l:Vh")) != EOF) {
138 #endif
139     switch (opt) {
140       case 'd':
141         domain=strtol(optarg,NULL,0);
142         break;
143       case 'v':
144         ORTEVerbositySetOptions(optarg);
145         break;
146       case 'R':
147         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
148         break;
149       case 'P':
150         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
151         break;
152       case 'E':
153         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
154         break;
155       case 'I':
156         dp.multicast.enabled=ORTE_TRUE;
157         dp.multicast.ipAddress=StringToIPAddress(optarg);
158         break;  
159       case 'l':
160         ORTEVerbositySetLogFile(optarg);
161       case 'V':
162         printf("Open Real-Time Ethernet (%s).\n",dp.version);
163         exit(0);
164         break;
165       case 'h':
166       default:
167         usage();
168         exit(opt == 'h' ? 0 : 1);
169     }
170   }
171   //Create application     
172   printf("|------------------------------------------------------------------------------|\n");
173   printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n", 
174          "status", "type","topic","time received", "time sent");
175   printf("|------------------------------------------------------------------------------|\n");
176   d=ORTEDomainAppCreate(domain,&dp,NULL,ORTE_TRUE);
177   ORTEDomainAppSubscriptionPatternAdd(d,"*","*",subscriptionCallBack,NULL);
178   ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE);
179   while (1) {
180     ORTESleepMs(1000);
181   }
182   exit(0);
183 }
184