]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/hello/h_subscriber.c
99e3f9c17a6480d33ad23cdc2098306a46469e34
[orte.git] / orte / examples / hello / h_subscriber.c
1 /*
2  *  $Id: h_subscriber.c,v 0.0.1.0       2005/01/03
3  *
4  *  DEBUG:  section                     h_subscriber
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 #include "orte.h"
22 #ifdef CONFIG_ORTE_RTL
23   #include <linux/module.h>
24   #include <posix/pthread.h>
25   #define printf rtl_printf
26 #elif defined CONFIG_ORTE_RTAI
27   #include <linux/module.h>
28   #include <rtai/compat.h>  
29   #define printf rt_printk
30 #else
31   #include <stdio.h>
32 #endif
33
34
35 ORTEDomain        *d = NULL;
36 char              instance2Recv[64];
37
38 void
39 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam) {
40   char *instance=(char*)vinstance;
41
42   switch (info->status) {
43     case NEW_DATA:
44       printf("%s\n",instance);
45       break;
46     case DEADLINE:
47       printf("deadline occurred\n");
48       break;
49   }
50 }
51
52
53 void *
54 subscriberCreate(void *arg) {
55   ORTESubscription    *s;
56   NtpTime             deadline,minimumSeparation;
57
58   ORTETypeRegisterAdd(d,"HelloMsg",NULL,NULL,NULL,sizeof(instance2Recv));
59   NTPTIME_BUILD(deadline,10);
60   NTPTIME_BUILD(minimumSeparation,0);
61   s=ORTESubscriptionCreate(
62        d,
63        IMMEDIATE,
64        BEST_EFFORTS,
65        "Example HelloMsg",
66        "HelloMsg",
67        &instance2Recv,
68        &deadline,
69        &minimumSeparation,
70        recvCallBack,
71        NULL,
72        IPADDRESS_INVALID);
73   return arg;
74 }
75
76
77 #ifndef CONFIG_ORTE_RT
78
79 int
80 main(int argc, char *args[]) {
81   ORTEInit();
82   ORTEVerbositySetOptions("ALL.10");
83   d=ORTEDomainAppCreate(ORTE_DEFAULT_DOMAIN,NULL,NULL,ORTE_FALSE);
84   if (!d) {
85     printf("ORTEDomainAppCreate failed!\n");
86     return 0;
87   }
88   subscriberCreate(NULL);
89   while (1)
90     ORTESleepMs(1000);
91   return 0;
92 }
93
94 #else
95 char *verbosity="";
96 MODULE_PARM(verbosity,"1s");
97 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
98 char *manager="127.0.0.1";
99 MODULE_PARM(manager,"s");
100 MODULE_PARM_DESC(manager,"IP address of local manager");
101 MODULE_LICENSE("GPL");
102 pthread_t thread;
103
104 void *
105 domainInit(void *arg) {
106   ORTEDomainProp dp;
107   
108   ORTEDomainPropDefaultGet(&dp);
109   ORTEVerbositySetOptions(verbosity);
110   dp.appLocalManager=StringToIPAddress(manager);
111   d=ORTEDomainAppCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
112   return arg;
113 }
114
115 void *
116 domainDestroy(void *arg) {
117   if (!d) return NULL;
118   ORTEDomainAppDestroy(d);
119   return arg;
120 }
121
122 int
123 init_module(void) {
124   ORTEInit();
125   pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
126   pthread_join(thread,NULL);
127   if (d) {
128     ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE); //application start
129     if (pthread_create(&thread,NULL,&subscriberCreate,NULL)!=0)
130       printf("pthread_create failed!\n");    
131   } else
132     printf("ORTEDomainAppCreate failed!\n");
133   return 0;
134 }
135
136
137 void
138 cleanup_module(void) {
139   if (!d) return;
140   pthread_join(thread,NULL);
141   pthread_create(&thread,NULL,&domainDestroy,NULL);
142   pthread_join(thread,NULL);
143 }
144
145 #endif
146
147
148
149
150
151