-
Notifications
You must be signed in to change notification settings - Fork 0
/
topicSubscriber.c
230 lines (199 loc) · 6.46 KB
/
topicSubscriber.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2016 Solace Systems, Inc. All rights reserved.
*
* http://www.solacesystems.com
*
* This source is distributed under the terms and conditions
* of any contract or contracts between Solace and you or
* your company. If there are no contracts in place use of
* this source is not authorized. No support is provided and
* no distribution, sharing with others or re-use of this
* source is authorized unless specifically stated in the
* contracts referred to above.
*/
/*
* Solace messaging with OpenMAMA
* PublishSubscribe tutorial - Topic Subscriber
* Demonstrates subscribing to a topic for direct messages and receiving messages
*/
#include <stdio.h>
#include <signal.h>
#include <mama/mama.h>
// used in stopHandler routine
void stopAll();
/*
* This routine is invoked when Ctrl-C is pressed from the console
*/
void stopHandler(int value)
{
signal(value, SIG_IGN);
printf(" Do you want to stop the program? [y/n]: ");
char answer = getchar();
if (answer == 'y')
stopAll();
else
signal(SIGINT, stopHandler);
getchar();
}
// used in main routune
void initializeBridge(const char *);
void connectTransport(const char *);
void subscribeToTopic(const char *);
// global pointers
struct
{
mamaBridge bridge;
mamaTransport transport;
mamaSubscription subscription;
mamaMsgCallbacks receiver;
}
global;
int main(int argc, const char** argv)
{
printf("Solace OpenMAMA tutorial.\nReceiving messages with OpenMAMA.\n");
// set Ctrl-C handler
signal(SIGINT, stopHandler);
initializeBridge("solace");
connectTransport("vmr"); // see mama.properties
subscribeToTopic("tutorial.topic");
// receive messages until Ctrl-C is pressed
mama_status status;
if ((status = mama_start(global.bridge)) != MAMA_STATUS_OK)
{
printf("OpenMAMA start error: %s\n",
mamaStatus_stringForStatus(status));
}
// normal exit after Ctrl-C was pressed
printf("Closing OpenMAMA\n");
mama_close();
exit(status);
}
/*
* Stop subscription, transport and MAMA
*/
void stopAll()
{
// order is important
if (global.subscription)
{
mamaSubscription_destroy(global.subscription);
global.subscription = NULL;
}
mama_stop(global.bridge);
if (global.transport)
{
mamaTransport_destroy(global.transport);
global.transport = NULL;
}
}
/*
* Load and initialize specified middleware bridge, initialize MAMA
*/
void initializeBridge(const char * bridgeName)
{
global.bridge = NULL;
mama_status status;
if (((status = mama_loadBridge(&global.bridge, bridgeName)) == MAMA_STATUS_OK) &&
((status = mama_openWithProperties(".","mama.properties")) == MAMA_STATUS_OK))
{
// normal exit;
return;
}
// error exit
mama_close();
printf("MAMA initialization error: %s\n", mamaStatus_stringForStatus(status));
exit(status);
}
/*
* Connect to specified transport using the previously loaded bridge
*/
void connectTransport(const char * transportName)
{
global.transport = NULL;
mama_status status;
if (((status = mamaTransport_allocate(&global.transport)) == MAMA_STATUS_OK) &&
((status = mamaTransport_create(global.transport, transportName, global.bridge)) == MAMA_STATUS_OK))
{
// normal exit
return;
}
// error exit
printf("Transport %s connect error: %s\n", transportName, mamaStatus_stringForStatus(status));
mama_close();
exit(status);
}
/*
* Call this routine when subscription is created
*/
void onCreate(mamaSubscription subscription, void * closure)
{
const char * topicName = NULL;
if (mamaSubscription_getSymbol(subscription, &topicName) == MAMA_STATUS_OK)
{
printf("Created subscription to topic \"%s\"\n", topicName);
}
}
/*
* Call this routine when subscription error occurs
*/
void onError(mamaSubscription subscription, mama_status status,
void * platformError, const char * subject, void * closure)
{
const char * topicName = NULL;
if (mamaSubscription_getSymbol(subscription, &topicName) == MAMA_STATUS_OK)
printf("Error occured with subscription to topic \"%s\", error code: %s\n",
topicName, mamaStatus_stringForStatus(status));
}
/*
* Call this routine when a message is received
*/
void onMessage(mamaSubscription subscription, mamaMsg message, void * closure, void * itemClosure)
{
const char * topicName = NULL;
if (mamaSubscription_getSymbol(subscription, &topicName) == MAMA_STATUS_OK)
{
printf("Message of type %s received on topic \"%s\"\n", mamaMsgType_stringForMsg(message), topicName);
}
// extract from the message our own custom field (see topicPublisherMultiple.c)
const char * const MY_FIELD_NAME = "MdMyTimestamp";
const int MY_FIELD_ID = 99, BUFFER_SIZE = 32;
char buffer[BUFFER_SIZE];
if (mamaMsg_getFieldAsString(message, MY_FIELD_NAME, MY_FIELD_ID, buffer, BUFFER_SIZE)
== MAMA_STATUS_OK)
{
printf("This message has a field \"%s\" with value: %s", MY_FIELD_NAME, buffer);
}
}
/*
* Subscribe to specified message topic
*/
void subscribeToTopic(const char * topicName)
{
global.subscription = NULL;
mama_status status;
if ((status = mamaSubscription_allocate(&global.subscription)) == MAMA_STATUS_OK)
{
// initialize functions called by MAMA on different subscription events
memset(&global.receiver, 0, sizeof(global.receiver));
global.receiver.onCreate = onCreate; // when subscription created
global.receiver.onError = onError; // when error occurs
global.receiver.onMsg = onMessage; // when a message arrives on subscribed topic
// subscribe to messages on specified topic
mamaQueue defaultQueue;
if (((status = mama_getDefaultEventQueue(global.bridge, &defaultQueue)) == MAMA_STATUS_OK) &&
((status = mamaSubscription_createBasic(global.subscription, global.transport,
defaultQueue,
&global.receiver,
topicName,
NULL)) == MAMA_STATUS_OK))
{
// normal exit
return;
}
}
// error exit
printf("Error subscribing to topic %s, error code: %s\n", topicName,
mamaStatus_stringForStatus(status));
mama_close();
exit(status);
}