]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/ping/orteping.c
new version 0.2.3
[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  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  */ 
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifndef _WIN32
25   #include <signal.h>
26 #endif
27 #include "orte.h"
28 #ifdef HAVE_CONFIG_H
29   #ifdef HAVE_GETOPT_H
30     #include <getopt.h>
31   #endif
32   #ifdef HAVE_UNISTD_H
33     #include <unistd.h> //getopt.h for DarWin, Solaris, ...
34   #endif
35 #else
36   #include <getopt.h>
37 #endif
38
39 Boolean                 quite=ORTE_FALSE;
40
41 void
42 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam) {
43   uint32_t *instance=(uint32_t*)vinstance;
44   
45   switch (info->status) {
46     case NEW_DATA:
47       if (!quite)
48         printf("received fresh issue %d\n",*instance);
49       break;
50     case DEADLINE:
51       printf("deadline occured\n");
52       break;
53   }
54 }
55
56 void
57 sendCallBack(const ORTESendInfo *info,void *vinstance, void *sendCallBackParam) {
58   uint32_t *instance=(uint32_t*)vinstance;
59   
60   switch (info->status) {
61     case NEED_DATA:
62       (*instance)++;
63       if (!quite)
64         printf("sent issue %d\n",*instance);
65       break;
66     case CQL:  //criticalQueueLevel
67       break;
68   }
69 }
70
71 static void usage(void) {
72   printf("usage: orteping <parameters> \n");
73   printf("  -d, --domain <domain>         working manager domain\n");
74   printf("  -p, --publisher               create publisher Ping,PingData\n");
75   printf("  -S, --strength                strength of publisher <1>\n");
76   printf("  -D, --delay <ms>              delay between two publications <1000>\n");
77   printf("  -s, --subscriber              create subscriber Ping,PingData\n");
78   printf("  -R, --refresh <s>             refresh period of application\n");
79   printf("  -P, --purge <s>               purge time for services\n");
80   printf("  -E, --expiration <s>          expiration time of application\n");
81   printf("  -m, --minimumSeparation <s>   minimumSeparation between two issues\n");
82   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
83   printf("      examples: ORTEManager -v 51.7:32.5 sections 51 and 32\n");
84   printf("                ORTEManager -v ALL.7     all sections up to level 7\n");
85   printf("  -q  --quiet                   \n");
86   printf("  -l, --logfile <filename>      set log file name\n");
87   printf("  -V, --version                 show version\n");
88   printf("  -h, --help                    this usage screen\n");
89 }
90
91 int main(int argc,char *argv[]) {
92 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
93   static struct option long_opts[] = {
94     { "domain",1,0, 'd' },
95     { "publisher",0,0, 'p' },
96     { "strength",1,0, 'S' },
97     { "subscriber",0,0, 's' },
98     { "refresh",1,0, 'R' },
99     { "purge",1,0, 'P' },
100     { "expiration",1,0, 'E' },
101     { "minimumSeparation",1,0, 'm' },
102     { "delay",1,0, 'D' },
103     { "verbosity",1,0, 'v' },
104     { "quiet",0,0, 'q' },
105     { "logfile",1,0, 'l' },
106     { "version",0,0, 'V' },
107     { "help",  0, 0, 'h' },
108     { 0, 0, 0, 0}
109   };
110 #endif
111   ORTEDomain              *d;
112   ORTEDomainProp          dp; 
113   ORTEPublication         *p=NULL;
114   ORTESubscription        *s=NULL;
115   int                     opt,domain=ORTE_DEFAULT_DOMAIN;
116   int32_t                 strength=1;
117   int32_t                 instanceSend=0,instanceRecv=0;
118   NtpTime                 persistence,deadline,minimumSeparation,delay;
119   Boolean                 havePublisher=ORTE_FALSE;
120   Boolean                 haveSubscriber=ORTE_FALSE;
121   
122   ORTEInit();
123   ORTEDomainPropDefaultGet(&dp);
124   NTPTIME_BUILD(minimumSeparation,0); 
125   NTPTIME_BUILD(delay,1); //1s
126
127 #if defined HAVE_GETOPT_LONG || defined HAVE_GETOPT_LONG_ORTE
128   while ((opt = getopt_long(argc, argv, "m:S:d:v:R:E:P:l:D:Vhpsq",&long_opts[0], NULL)) != EOF) {
129 #else
130   while ((opt = getopt(argc, argv, "m:S:d:v:R:E:P:l:D:Vhpsq")) != EOF) {
131 #endif
132     switch (opt) {
133       case 'S':
134         strength=strtol(optarg,NULL,0);
135         break;
136       case 'd':
137         domain=strtol(optarg,NULL,0);
138         break;
139       case 'p':
140           havePublisher=ORTE_TRUE;
141         break;
142       case 's':
143           haveSubscriber=ORTE_TRUE;
144         break;
145       case 'v':
146         ORTEVerbositySetOptions(optarg);
147         break;
148       case 'R':
149         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
150         break;
151       case 'P':
152         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
153         break;
154       case 'E':
155         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
156         break;
157       case 'm':
158         NtpTimeAssembFromMs(minimumSeparation,strtol(optarg,NULL,0),0);
159         break;
160       case 'D':
161         NtpTimeAssembFromMs(delay,strtol(optarg,NULL,0)/1000,strtol(optarg,NULL,0)%1000);
162         break;
163       case 'l':
164         ORTEVerbositySetLogFile(optarg);
165       case 'q':
166         quite=ORTE_TRUE;
167         break;
168       case 'V':
169         printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
170         exit(0);
171         break;
172       case 'h':
173       default:
174         usage();
175         exit(opt == 'h' ? 0 : 1);
176     }
177   }
178   //Create application     
179   d=ORTEDomainAppCreate(domain,&dp,NULL,ORTE_FALSE);
180   //Register ser./deser. rutines with maximal size 4 Bytes
181   ORTETypeRegisterAdd(d,"PingData",NULL,NULL,4);   
182   //Create publisher
183   if (havePublisher) {
184     NTPTIME_BUILD(persistence,5); 
185     p=ORTEPublicationCreate(
186          d,
187          "Ping",
188          "PingData",
189          &instanceSend,
190          &persistence,
191          strength,
192          sendCallBack,
193          NULL,
194          &delay);
195   }
196   if (haveSubscriber) {
197     NTPTIME_BUILD(deadline,3); 
198     s=ORTESubscriptionCreate(
199          d,
200          IMMEDIATE,
201          BEST_EFFORTS,
202          "Ping",
203          "PingData",
204          &instanceRecv,
205          &deadline,
206          &minimumSeparation,
207          recvCallBack,
208          NULL);
209   }
210   //never ending loop
211   while (1) 
212     ORTESleepMs(1000);
213   exit(0);
214 }
215