]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/ping/orteping.c
a2dc0d942e59404fac578bee712cf55fef8bd70d
[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 #include <getopt.h>
25 #ifndef _WIN32
26   #include <signal.h>
27 #endif
28 #include "orte_api.h"
29
30 Boolean                 quite=ORTE_FALSE;
31
32 void
33 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam) {
34   u_int32_t *instance=(u_int32_t*)vinstance;
35   
36   switch (info->status) {
37     case NEW_DATA:
38       if (!quite)
39         printf("received fresh issue %d\n",*instance);
40       break;
41     case DEADLINE:
42       printf("deadline occured\n");
43       break;
44   }
45 }
46
47 void
48 sendCallBack(const ORTESendInfo *info,void *vinstance, void *sendCallBackParam) {
49   u_int32_t *instance=(u_int32_t*)vinstance;
50   
51   switch (info->status) {
52     case NEED_DATA:
53       (*instance)++;
54       if (!quite)
55         printf("sent issue %d\n",*instance);
56       break;
57     case CQL:  //criticalQueueLevel
58       break;
59   }
60 }
61
62 static void usage(void) {
63   printf("usage: ORTEPing <parameters> \n");
64   printf("  -d, --domain <domain>         working manager domain\n");
65   printf("  -p, --publisher               create publisher Ping,PingData\n");
66   printf("  -S, --strength                strength of publisher <1>\n");
67   printf("  -D, --delay <ms>              delay between two publications <1000>\n");
68   printf("  -s, --subscriber              create subscriber Ping,PingData\n");
69   printf("  -R, --refresh <s>             refresh period of application\n");
70   printf("  -P, --purge <s>               purge time for services\n");
71   printf("  -E, --expiration <s>          expiration time of application\n");
72   printf("  -m, --minimumSeparation <s>   minimumSeparation between two issues\n");
73   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
74   printf("      examples: ORTEManager -v 51,7:32,5 sections 51 and 32\n");
75   printf("                ORTEManager -v ALL,7     all sections up to level 7\n");
76   printf("  -q  --quiet                   \n");
77   printf("  -l, --logfile <filename>      set log file name\n");
78   printf("  -V, --version                 show version\n");
79   printf("  -h, --help                    this usage screen\n");
80 }
81
82 int main(int argc,char *argv[]) {
83   static struct option long_opts[] = {
84     { "domain",1,0, 'd' },
85     { "publisher",0,0, 'p' },
86     { "strength",1,0, 'S' },
87     { "subscriber",0,0, 's' },
88     { "refresh",1,0, 'R' },
89     { "purge",1,0, 'P' },
90     { "expiration",1,0, 'E' },
91     { "minimumSeparation",1,0, 'm' },
92     { "delay",1,0, 'D' },
93     { "verbosity",1,0, 'v' },
94     { "quiet",0,0, 'q' },
95     { "logfile",1,0, 'l' },
96     { "version",0,0, 'V' },
97     { "help",  0, 0, 'h' },
98     { 0, 0, 0, 0}
99   };
100   ORTEDomain              *d;
101   ORTEDomainProp          dp; 
102   ORTEPublication         *p=NULL;
103   ORTESubscription        *s=NULL;
104   int                     opt,domain=ORTE_DEFAULT_DOMAIN;
105   int32_t                 strength=1;
106   int32_t                 instanceSend=0,instanceRecv=0;
107   NtpTime                 persistence,deadline,minimumSeparation,delay;
108   Boolean                 havePublisher=ORTE_FALSE;
109   Boolean                 haveSubscriber=ORTE_FALSE;
110   
111   ORTEInit();
112   ORTEDomainPropDefaultGet(&dp);
113   NTPTIME_BUILD(minimumSeparation,0); 
114   NTPTIME_BUILD(delay,1); //1s
115  
116   while ((opt = getopt_long(argc, argv, "m:S:d:v:R:E:P:l:D:Vhpsq",&long_opts[0], NULL)) != EOF) {
117     switch (opt) {
118       case 'S':
119         strength=strtol(optarg,NULL,0);
120         break;
121       case 'd':
122         domain=strtol(optarg,NULL,0);
123         break;
124       case 'p':
125           havePublisher=ORTE_TRUE;
126         break;
127       case 's':
128           haveSubscriber=ORTE_TRUE;
129         break;
130       case 'v':
131         ORTEVerbositySetOptions(optarg);
132         break;
133       case 'R':
134         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
135         break;
136       case 'P':
137         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
138         break;
139       case 'E':
140         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
141         break;
142       case 'm':
143         NtpTimeAssembFromMs(minimumSeparation,strtol(optarg,NULL,0),0);
144         break;
145       case 'D':
146         NtpTimeAssembFromMs(delay,strtol(optarg,NULL,0)/1000,strtol(optarg,NULL,0)%1000);
147         break;
148       case 'l':
149         ORTEVerbositySetLogFile(optarg);
150       case 'q':
151         quite=ORTE_TRUE;
152         break;
153       case 'V':
154         printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
155         exit(0);
156         break;
157       case 'h':
158       default:
159         usage();
160         exit(opt == 'h' ? 0 : 1);
161     }
162   }
163   //Create application     
164   d=ORTEDomainAppCreate(domain,&dp,NULL,ORTE_FALSE);
165   //Register ser./deser. rutines with maximal size 4 Bytes
166   ORTETypeRegisterAdd(d,"PingData",NULL,NULL,4);   
167   //Create publisher
168   if (havePublisher) {
169     NTPTIME_BUILD(persistence,5); 
170     p=ORTEPublicationCreate(
171          d,
172          "Ping",
173          "PingData",
174          &instanceSend,
175          &persistence,
176          strength,
177          sendCallBack,
178          NULL,
179          &delay);
180   }
181   if (haveSubscriber) {
182     NTPTIME_BUILD(deadline,3); 
183     s=ORTESubscriptionCreate(
184          d,
185          IMMEDIATE,
186          BEST_EFFORTS,
187          "Ping",
188          "PingData",
189          &instanceRecv,
190          &deadline,
191          &minimumSeparation,
192          recvCallBack,
193          NULL);
194   }
195   //never ending loop
196   while (1) 
197     ORTESleepMs(1000);
198   exit(0);
199 }
200