]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/spy/ortespy.c
930e6492eb51e4cd09ea061cc13be80f9bba5f0b
[orte.git] / orte / examples / spy / ortespy.c
1 /*
2  *  $Id: ortespy.c,v 0.0.0.1            2003/10/07 
3  *
4  *  DEBUG:  section                     ortespy
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 #include "string.h"
31
32 ORTEDomain              *d;
33 NtpTime                 deadline,minimumSeparation;
34 int32_t                 instanceRecv;
35
36 void
37 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam) {
38   char lbuff[MAX_STRING_NTPTIME_LENGTH];
39   char rbuff[MAX_STRING_NTPTIME_LENGTH];
40   switch (info->status) {
41     case NEW_DATA:
42       printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n",
43              "NEW_DATA",
44              info->topic,
45              info->type,
46              NtpTimeToStringUs(info->localTimeReceived, lbuff),
47              NtpTimeToStringUs(info->remoteTimePublished, rbuff));
48       break;
49     case DEADLINE:
50 //      printf("deadline occured\n");
51       break;
52   }
53 }
54
55 ORTESubscription*
56 subscriptionCallBack(const char *topic, const char *type, void *param) {
57   ORTETypeRegisterAdd(d,type,NULL,NULL,0);   
58   if (strcmp(topic,"Red")==0) return NULL;
59   return ORTESubscriptionCreate(
60         d,
61         IMMEDIATE,
62         BEST_EFFORTS,
63         topic,
64         type,
65         &instanceRecv,
66         &deadline,
67         &minimumSeparation,
68         recvCallBack,
69         NULL);
70 }
71
72 static void usage(void) {
73   printf("usage: ORTESpy <parameters> \n");
74   printf("  -d, --domain <domain>         working manager domain\n");
75   printf("  -v, --verbosity <level>       set verbosity level SECTION, up to LEVEL:...\n");
76   printf("      examples: ORTEManager -v 51,7:32,5 sections 51 and 32\n");
77   printf("                ORTEManager -v ALL,7     all sections up to level 7\n");
78   printf("  -R, --refresh <s>             refresh period in second(s)\n");
79   printf("  -P, --purge <s>               purge time in second(s)\n");
80   printf("  -e, --expiration <s>          expiration time of manager in second(s)\n");
81   printf("  -l, --logfile <filename>      set log file name\n");
82   printf("  -V, --version                 show version\n");
83   printf("  -h, --help                    this usage screen\n");
84 }
85
86 int main(int argc,char *argv[]) {
87   static struct option long_opts[] = {
88     { "domain",1,0, 'd' },
89     { "verbosity",1,0, 'v' },
90     { "refresh",1,0, 'R' },
91     { "purge",1,0, 'P' },
92     { "expiration",1,0, 'E' },
93     { "logfile",1,0, 'l' },
94     { "version",0,0, 'V' },
95     { "help",  0, 0, 'h' },
96     { 0, 0, 0, 0}
97   };
98   ORTEDomainProp          dp; 
99   int                     opt,domain=ORTE_DEFAULT_DOMAIN;
100   
101   ORTEInit();
102   ORTEDomainPropDefaultGet(&dp);
103   NTPTIME_BUILD(deadline,3); 
104   NTPTIME_BUILD(minimumSeparation,0); 
105  
106   while ((opt = getopt_long(argc, argv, "d:v:R:E:P:l:Vh",&long_opts[0], NULL)) != EOF) {
107     switch (opt) {
108       case 'd':
109         domain=strtol(optarg,NULL,0);
110         break;
111       case 'v':
112         ORTEVerbositySetOptions(optarg);
113         break;
114       case 'R':
115         NtpTimeAssembFromMs(dp.baseProp.refreshPeriod,strtol(optarg,NULL,0),0);
116         break;
117       case 'P':
118         NtpTimeAssembFromMs(dp.baseProp.purgeTime,strtol(optarg,NULL,0),0);
119         break;
120       case 'E':
121         NtpTimeAssembFromMs(dp.baseProp.expirationTime,strtol(optarg,NULL,0),0);
122         break;
123       case 'l':
124         ORTEVerbositySetLogFile(optarg);
125       case 'V':
126         printf("Ocera Real-Time Ethernet (%s).\n",dp.version);
127         exit(0);
128         break;
129       case 'h':
130       default:
131         usage();
132         exit(opt == 'h' ? 0 : 1);
133     }
134   }
135   //Create application     
136   printf("|------------------------------------------------------------------------------|\n");
137   printf("| %-10s | %-9s | %-9s | %-18s | %-18s |\n", 
138          "status", "type","topic","time received", "time sent");
139   printf("|------------------------------------------------------------------------------|\n");
140   d=ORTEDomainAppCreate(domain,&dp,NULL,ORTE_TRUE);
141   ORTEDomainAppSubscriptionPatternAdd(d,"*","*",subscriptionCallBack,NULL);
142   ORTEDomainStart(d,ORTE_TRUE,ORTE_TRUE,ORTE_TRUE);
143   while (1) {
144     ORTESleepMs(1000);
145   }
146   exit(0);
147 }
148