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