]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/ping/orteping.c
Reformat the sources with orte/uncrustify script
[orte.git] / orte / examples / ping / orteping.c
1 /*
2  *  $Id: orteping.c,v 0.0.0.1           2003/10/07
3  *
4  *  DEBUG:  section                     orteping
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
49 #ifdef HAVE_INTTYPES_H
50  #include <inttypes.h>
51 #endif /*HAVE_INTTYPES_H*/
52
53 #ifndef PRIu32
54   #define PRIu32 x
55 #endif
56
57 #ifdef MAIN_RENAMED
58 #define main orte_ping_main
59 #define exit return
60 #endif
61
62 Boolean                 quite = ORTE_FALSE;
63 int                     regfail = 0;
64
65 //event system
66 void
67 onRegFail(void *param)
68 {
69   printf("registration to a manager failed\n");
70   regfail = 1;
71 }
72
73 static void
74 recvCallBack(const ORTERecvInfo *info, void *vinstance, void *recvCallBackParam)
75 {
76   uint32_t *instance = (uint32_t *)vinstance;
77
78   switch (info->status) {
79     case NEW_DATA:
80       if (!quite)
81         printf("received fresh issue %" PRIu32 "\n", *instance);
82       break;
83     case DEADLINE:
84       printf("deadline occurred\n");
85       break;
86   }
87 }
88
89 static void
90 sendCallBack(const ORTESendInfo *info, void *vinstance, void *sendCallBackParam)
91 {
92   uint32_t *instance = (uint32_t *)vinstance;
93
94   switch (info->status) {
95     case NEED_DATA:
96       (*instance)++;
97       if (!quite)
98         printf("sent issue %" PRIu32 "\n", *instance);
99       break;
100     case CQL:  //criticalQueueLevel
101       break;
102   }
103 }
104
105 static void
106 usage(void)
107 {
108   printf("usage: orteping <parameters> \n");
109   printf("  -d, --domain <domain>         working application domain\n");
110   printf("  -p, --publisher               create publisher Ping,PingData\n");
111   printf("  -S, --strength                strength of publisher <1>\n");
112   printf("  -D, --delay <ms>              delay between two publications <1000>\n");
113   printf("  -s, --subscriber              create subscriber Ping,PingData\n");
114   printf("  -R, --refresh <s>             refresh period of application\n");
115   printf("  -P, --purge <s>               purge time for services\n");
116   printf("  -E, --expiration <s>          expiration time of application\n");
117   printf("  -m, --minimumSeparation <s>   minimumSeparation between two issues\n");
118   printf("  -I, --metaMulticast <IPAdd>   use multicast IPAddr for metatraffic comm.\n");
119   printf("  -i, --dataMulticast <IPAdd>   use multicast IPAddr for userdata comm.\n");
120   printf("  -t, --timetolive <number>     time-to-live for multicast packets\n");
121   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
122   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
123   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
124   printf("  -q  --quiet                   \n");
125   printf("  -l, --logfile <filename>      set log file name\n");
126   printf("  -V, --version                 show version\n");
127   printf("  -h, --help                    this usage screen\n");
128 }
129
130 int
131 main(int argc, char *argv[])
132 {
133 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
134   static struct option long_opts[] = {
135     { "domain", 1, 0, 'd' },
136     { "publisher", 0, 0, 'p' },
137     { "strength", 1, 0, 'S' },
138     { "subscriber", 0, 0, 's' },
139     { "refresh", 1, 0, 'R' },
140     { "purge", 1, 0, 'P' },
141     { "expiration", 1, 0, 'E' },
142     { "minimumSeparation", 1, 0, 'm' },
143     { "metaMulticast", 1, 0, 'I' },
144     { "dataMulticast", 1, 0, 'i' },
145     { "timetolive", 1, 0, 't' },
146     { "delay", 1, 0, 'D' },
147     { "verbosity", 1, 0, 'v' },
148     { "quiet", 0, 0, 'q' },
149     { "logfile", 1, 0, 'l' },
150     { "version", 0, 0, 'V' },
151     { "help",  0, 0, 'h' },
152     { 0, 0, 0, 0}
153   };
154 #endif
155   ORTEDomain              *d;
156   ORTEDomainProp          dp;
157   ORTEPublication         *p = NULL;
158   ORTESubscription        *s = NULL;
159   int                     opt, domain = ORTE_DEFAULT_DOMAIN;
160   int32_t                 strength = 1;
161   int32_t                 instanceSend = 0, instanceRecv = 0;
162   NtpTime                 persistence, deadline, minimumSeparation, delay;
163   Boolean                 havePublisher = ORTE_FALSE;
164   Boolean                 haveSubscriber = ORTE_FALSE;
165   IPAddress               smIPAddress = IPADDRESS_INVALID;
166   ORTEDomainAppEvents     events;
167
168   ORTEInit();
169   ORTEDomainPropDefaultGet(&dp);
170   NTPTIME_BUILD(minimumSeparation, 0);
171   NTPTIME_BUILD(delay, 1); //1s
172
173 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
174   while ((opt = getopt_long(argc, argv, "m:S:d:v:R:E:P:l:I:i:t:D:Vhpsq", &long_opts[0], NULL)) != EOF) {
175 #else
176   while ((opt = getopt(argc, argv, "m:S:d:v:R:E:P:l:I:i:t:D:Vhpsq")) != EOF) {
177 #endif
178     switch (opt) {
179       case 'S':
180         strength = strtol(optarg, NULL, 0);
181         break;
182       case 'd':
183         domain = strtol(optarg, NULL, 0);
184         break;
185       case 'p':
186         havePublisher = ORTE_TRUE;
187         break;
188       case 's':
189         haveSubscriber = ORTE_TRUE;
190         break;
191       case 'v':
192         ORTEVerbositySetOptions(optarg);
193         break;
194       case 'R':
195         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod, strtol(optarg, NULL, 0), 0);
196         break;
197       case 'P':
198         NtpTimeAssembFromMs(dp.baseProp.purgeTime, strtol(optarg, NULL, 0), 0);
199         break;
200       case 'E':
201         NtpTimeAssembFromMs(dp.baseProp.expirationTime, strtol(optarg, NULL, 0), 0);
202         break;
203       case 'm':
204         NtpTimeAssembFromMs(minimumSeparation, strtol(optarg, NULL, 0), 0);
205         break;
206       case 'D':
207         NtpTimeAssembFromMs(delay, strtol(optarg, NULL, 0)/1000, strtol(optarg, NULL, 0)%1000);
208         break;
209       case 'I':
210         dp.multicast.enabled = ORTE_TRUE;
211         dp.multicast.ipAddress = StringToIPAddress(optarg);
212         break;
213       case 'i':
214         dp.multicast.enabled = ORTE_TRUE;
215         smIPAddress = StringToIPAddress(optarg);
216         break;
217       case 't':
218         dp.multicast.ttl = strtol(optarg, NULL, 0);
219         break;
220       case 'l':
221         ORTEVerbositySetLogFile(optarg);
222       case 'q':
223         quite = ORTE_TRUE;
224         break;
225       case 'V':
226         printf("Ocera Real-Time Ethernet (%s).\n", dp.version);
227         exit(0);
228         break;
229       case 'h':
230       default:
231         usage();
232         exit(opt == 'h' ? 0 : 1);
233     }
234   }
235
236   //initiate event system
237   ORTEDomainInitEvents(&events);
238   events.onRegFail = onRegFail;
239
240   //Create application
241   d = ORTEDomainAppCreate(domain, &dp, &events, ORTE_FALSE);
242   //Register ser./deser. rutines with maximal size 4 Bytes
243   ORTETypeRegisterAdd(d, "PingData", NULL, NULL, NULL, 4);
244   //Create publisher
245   if (havePublisher) {
246     NTPTIME_BUILD(persistence, 5);
247     p = ORTEPublicationCreate(
248       d,
249       "Ping",
250       "PingData",
251       &instanceSend,
252       &persistence,
253       strength,
254       sendCallBack,
255       NULL,
256       &delay);
257     if (p == NULL) {
258       printf("ORTEPublicationCreate failed\n");
259       exit(1);
260     }
261   }
262   if (haveSubscriber) {
263     NTPTIME_BUILD(deadline, 3);
264     s = ORTESubscriptionCreate(
265       d,
266       IMMEDIATE,
267       BEST_EFFORTS,
268       "Ping",
269       "PingData",
270       &instanceRecv,
271       &deadline,
272       &minimumSeparation,
273       recvCallBack,
274       NULL,
275       smIPAddress);
276     if (s == NULL) {
277       printf("ORTESubscriptionCreate failed\n");
278       exit(1);
279     }
280   }
281   //never ending loop
282   while (!regfail)
283     ORTESleepMs(1000);
284   exit(0);
285 }