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