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