]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/hello/h_publisher.c
9835bc58f879c883fae20c20d427c1b4ea0875bb
[orte.git] / orte / examples / hello / h_publisher.c
1 /*
2  *  $Id: h_publisher.c,v 0.0.1.0        2005/01/03
3  *
4  *  DEBUG:  section                     h_publisher
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_publisher_main
46 #define exit return
47 #endif
48
49 static ORTEDomain        *d=NULL;
50 static char              instance2Send[64];
51 static int               counter=0;
52
53 static void
54 sendCallBack(const ORTESendInfo *info,void *vinstance, void *sendCallBackParam) {
55   char *instance=(char*)vinstance;
56
57   switch (info->status) {
58     case NEED_DATA:
59       printf("Sampling publication, count %d\n", counter);
60       sprintf(instance,"Hello Universe! (%d)",counter++);
61       break;
62     case CQL:  //criticalQueueLevel
63       break;
64   }
65 }
66
67 void *
68 publisherCreate(void *arg) {
69   ORTEPublication     *p;
70   NtpTime             persistence, delay;
71
72   ORTETypeRegisterAdd(d,"HelloMsg",NULL,NULL,NULL,sizeof(instance2Send));
73   NTPTIME_BUILD(persistence,3);
74   NTPTIME_BUILD(delay,1); 
75   p=ORTEPublicationCreate(
76        d,
77       "Example HelloMsg",
78       "HelloMsg",
79       &instance2Send,
80       &persistence,
81       1,
82       sendCallBack,
83       NULL,
84       &delay);
85   if (p == NULL) {
86     printf("ORTEPublicationCreate failed\n");
87   }
88   return arg;
89 }
90
91
92 #ifndef CONFIG_ORTE_RT
93
94 int
95 main(int argc, char *args[]) {
96   ORTEInit();
97   ORTEVerbositySetOptions("ALL.10");
98   d=ORTEDomainAppCreate(ORTE_DEFAULT_DOMAIN,NULL,NULL,ORTE_FALSE);
99   if (!d) {
100     printf("ORTEDomainAppCreate failed!\n");
101     return 0;
102   }
103   publisherCreate((void*)d);
104   while(1) 
105     ORTESleepMs(1000);
106
107   ORTESleepMs(10000);
108   printf("finnished!\n");
109   ORTEDomainAppDestroy(d);
110   return 0;
111 }
112
113 #else
114 char *verbosity="";
115 MODULE_PARM(verbosity,"1s");
116 MODULE_PARM_DESC(verbosity,"set verbosity level SECTION, up to LEVEL:...");
117 char *manager="127.0.0.1";
118 MODULE_PARM(manager,"1s");
119 MODULE_PARM_DESC(manager,"IP address of local manager");
120 MODULE_LICENSE("GPL");
121 pthread_t thread;
122
123 void *
124 domainInit(void *arg) {
125   ORTEDomainProp dp;
126
127   ORTEVerbositySetOptions(verbosity);
128   ORTEDomainPropDefaultGet(&dp);
129   dp.appLocalManager=StringToIPAddress(manager);
130   d=ORTEDomainAppCreate(ORTE_DEFAULT_DOMAIN,&dp,NULL,ORTE_TRUE);
131   return arg;
132 }
133
134 void *
135 domainDestroy(void *arg) {
136   if (!d) return NULL;
137   ORTEDomainAppDestroy(d);
138   return arg;
139 }
140
141 int
142 init_module(void) {
143
144   ORTEInit();
145   pthread_create(&thread,NULL,&domainInit,NULL);  //allocate resources in RT 
146   pthread_join(thread,NULL);
147   if (d) {
148     ORTEDomainStart(d,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE,ORTE_FALSE,ORTE_TRUE); //application start
149     if (pthread_create(&thread,NULL,&publisherCreate,NULL)!=0)
150       printf("pthread_create failed!\n");
151   } else
152     printf("ORTEDomainAppCreate failed!\n");
153   return 0;
154 }
155
156
157 void
158 cleanup_module(void) {
159   if (!d) return;
160   pthread_join(thread,NULL);
161   pthread_create(&thread,NULL,&domainDestroy,NULL);
162   pthread_join(thread,NULL);
163 }
164
165 #endif
166
167