]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/spy/ortespy.c
Reformat the sources with orte/uncrustify script
[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 {
62   char lbuff[MAX_STRING_NTPTIME_LENGTH];
63   char rbuff[MAX_STRING_NTPTIME_LENGTH];
64
65   switch (info->status) {
66     case NEW_DATA:
67       printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n",
68              "NEW_DATA",
69              info->topic,
70              info->type,
71              NtpTimeToStringUs(info->localTimeReceived, lbuff),
72              NtpTimeToStringUs(info->remoteTimePublished, rbuff));
73       break;
74     case DEADLINE:
75 //      printf("deadline occurred\n");
76       break;
77   }
78 }
79
80 ORTESubscription *
81 subscriptionCallBack(const char *topic, const char *type, void *param)
82 {
83   ORTETypeRegisterAdd(d, type, NULL, NULL, NULL, 0);
84   if (strcmp((const char *)topic, (const char *)"Red") == 0)
85     return NULL;
86   return ORTESubscriptionCreate(
87            d,
88            IMMEDIATE,
89            BEST_EFFORTS,
90            topic,
91            type,
92            &instanceRecv,
93            &deadline,
94            &minimumSeparation,
95            recvCallBack,
96            NULL,
97            IPADDRESS_INVALID);
98 }
99
100 static void
101 usage(void)
102 {
103   printf("usage: ortespy <parameters> \n");
104   printf("  -d, --domain <domain>         working manager domain\n");
105   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
106   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
107   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
108   printf("  -R, --refresh <s>             refresh period in second(s)\n");
109   printf("  -P, --purge <s>               purge time in second(s)\n");
110   printf("  -e, --expiration <s>          expiration time of manager in second(s)\n");
111   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
112   printf("  -l, --logfile <filename>      set log file name\n");
113   printf("  -V, --version                 show version\n");
114   printf("  -h, --help                    this usage screen\n");
115 }
116
117 int
118 main(int argc, char *argv[])
119 {
120 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
121   static struct option long_opts[] = {
122     { "domain", 1, 0, 'd' },
123     { "verbosity", 1, 0, 'v' },
124     { "refresh", 1, 0, 'R' },
125     { "purge", 1, 0, 'P' },
126     { "expiration", 1, 0, 'E' },
127     { "metaMulticast", 1, 0, 'I' },
128     { "logfile", 1, 0, 'l' },
129     { "version", 0, 0, 'V' },
130     { "help",  0, 0, 'h' },
131     { 0, 0, 0, 0}
132   };
133 #endif
134   ORTEDomainProp          dp;
135   int                     opt, domain = ORTE_DEFAULT_DOMAIN;
136
137   ORTEInit();
138   ORTEDomainPropDefaultGet(&dp);
139   NTPTIME_BUILD(deadline, 3);
140   NTPTIME_BUILD(minimumSeparation, 0);
141
142 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
143   while ((opt = getopt_long(argc, argv, "d:v:R:E:I:P:l:Vh", &long_opts[0], NULL)) != EOF) {
144 #else
145   while ((opt = getopt(argc, argv, "d:v:R:E:I:P:l:Vh")) != EOF) {
146 #endif
147     switch (opt) {
148       case 'd':
149         domain = strtol(optarg, NULL, 0);
150         break;
151       case 'v':
152         ORTEVerbositySetOptions(optarg);
153         break;
154       case 'R':
155         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod, strtol(optarg, NULL, 0), 0);
156         break;
157       case 'P':
158         NtpTimeAssembFromMs(dp.baseProp.purgeTime, strtol(optarg, NULL, 0), 0);
159         break;
160       case 'E':
161         NtpTimeAssembFromMs(dp.baseProp.expirationTime, strtol(optarg, NULL, 0), 0);
162         break;
163       case 'I':
164         dp.multicast.enabled = ORTE_TRUE;
165         dp.multicast.ipAddress = StringToIPAddress(optarg);
166         break;
167       case 'l':
168         ORTEVerbositySetLogFile(optarg);
169       case 'V':
170         printf("Open Real-Time Ethernet (%s).\n", dp.version);
171         exit(0);
172         break;
173       case 'h':
174       default:
175         usage();
176         exit(opt == 'h' ? 0 : 1);
177     }
178   }
179   //Create application
180   printf("|------------------------------------------------------------------------------|\n");
181   printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n",
182          "status", "type", "topic", "time received", "time sent");
183   printf("|------------------------------------------------------------------------------|\n");
184   d = ORTEDomainAppCreate(domain, &dp, NULL, ORTE_TRUE);
185   ORTEDomainAppSubscriptionPatternAdd(d, "*", "*", subscriptionCallBack, NULL);
186   ORTEDomainStart(d, ORTE_TRUE, ORTE_FALSE, ORTE_TRUE, ORTE_FALSE, ORTE_TRUE);
187   while (1)
188     ORTESleepMs(1000);
189   exit(0);
190 }