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