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