00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
| /* server.c - see http://ultramessaging.github.io/UMExamples/server/c/index.html
*
* Copyright (c) 2005-2017 Informatica Corporation. All Rights Reserved.
* Permission is granted to licensees to use
* or alter this software for any purpose, including commercial applications,
* according to the terms laid out in the Software License Agreement.
*
* This source code example is provided by Informatica for educational
* and evaluation purposes only.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INFORMATICA DISCLAIMS ALL WARRANTIES
* EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF
* NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
* PURPOSE. INFORMATICA DOES NOT WARRANT THAT USE OF THE SOFTWARE WILL BE
* UNINTERRUPTED OR ERROR-FREE. INFORMATICA SHALL NOT, UNDER ANY CIRCUMSTANCES, BE
* LIABLE TO LICENSEE FOR LOST PROFITS, CONSEQUENTIAL, INCIDENTAL, SPECIAL OR
* INDIRECT DAMAGES ARISING OUT OF OR RELATED TO THIS AGREEMENT OR THE
* TRANSACTIONS CONTEMPLATED HEREUNDER, EVEN IF INFORMATICA HAS BEEN APPRISED OF
* THE LIKELIHOOD OF SUCH DAMAGES.
*/
#include <stdio.h>
#include <string.h>
#if defined(_MSC_VER)
/* Windows-only includes */
#include <winsock2.h>
#define SLEEP(s) Sleep((s)*1000)
#else
/* Unix-only includes */
#include <stdlib.h>
#include <unistd.h>
#define SLEEP(s) sleep(s)
#endif
#include <lbm/lbm.h>
/* Header for request messages. */
typedef struct req_hdr_s {
char data[8];
} req_hdr_t;
#define MAX_RESP_SIZE 7000
typedef struct client_s {
int state; /* 0=waiting for registration, 1=registered. */
char *topic_name;
char *source_name;
lbm_context_t *ctx;
lbm_src_t *resp_src;
char send_buf[MAX_RESP_SIZE + sizeof(req_hdr_t)];
} client_t;
/* Example error checking macro. Include after each UM call. */
#define EX_LBM_CHK(err) do { \
if ((err) < 0) { \
fprintf(stderr, "%s:%d, lbm error: '%s'\n", \
__FILE__, __LINE__, lbm_errmsg()); \
exit(1); \
} \
} while (0)
/* Error if null. */
#define NULL_CHK(N_ptr) do { \
if ((N_ptr) == NULL) { \
fprintf(stderr, "Error, %s:%d: '%s' is NULL\n", \
__FILE__, __LINE__, #N_ptr); \
fflush(stderr); \
abort(); /* core dump */ \
} \
} while (0)
void handle_register(client_t *client, const char *client_resp_name)
{
int err;
/* This work should probably be passed to a separate worker thread, but
* I'll do it here to simplify the code. */
if (client->state == 1) {
printf("Source '%s' re-confirmed\n", client->topic_name);
/* Reply to client. */
err = lbm_src_send(client->resp_src, "rOK", 4, LBM_MSG_FLUSH | LBM_SRC_NONBLOCK);
EX_LBM_CHK(err);
}
else if (client->state == 0) {
lbm_src_topic_attr_t * src_attr;
lbm_topic_t *lbm_topic;
/* Create source to send responses to client. */
err = lbm_src_topic_attr_create(&src_attr);
EX_LBM_CHK(err);
err = lbm_src_topic_attr_str_setopt(src_attr, "transport", "lbt-ru");
EX_LBM_CHK(err);
err = lbm_src_topic_attr_str_setopt(src_attr, "transport_lbtru_port", "12070");
EX_LBM_CHK(err);
err = lbm_src_topic_attr_str_setopt(src_attr,
"transport_source_side_filtering_behavior", "inclusion");
EX_LBM_CHK(err);
err = lbm_src_topic_alloc(&lbm_topic, client->ctx, client_resp_name, src_attr);
EX_LBM_CHK(err);
/* The following create can fail if a new client joins with the same
* response topic name as an existing client. Should handle this cleanly. */
err = lbm_src_create(&client->resp_src, client->ctx, lbm_topic,
NULL, NULL, NULL);
if (err) {
printf("Source '%s' failed; %s\n", client_resp_name, lbm_errmsg());
}
else {
client->state = 1;
client->topic_name = strdup(client_resp_name);
NULL_CHK(client->topic_name);
printf("Source '%s' created\n", client->topic_name);
}
err = lbm_src_topic_attr_delete(src_attr);
EX_LBM_CHK(err);
}
} /* handle_register */
void handle_req(client_t *client, req_hdr_t *req_hdr, const char *req_msg, size_t len)
{
int err;
/* This work should probably be passed to a separate worker thread, but
* I'll do it here to simplify the code. */
/* Responses copy the header from the request. */
memcpy(&client->send_buf[0], (char *)req_hdr, sizeof(req_hdr_t));
/* Response message is put after the header. */
char *response_msg = &client->send_buf[sizeof(req_hdr_t)];
/* Do the work of the request and put the response in response_msg.
* (For this sample, just echo back the request.) */
strcpy(response_msg, req_msg);
/* Reply to client. */
err = lbm_src_send(client->resp_src, client->send_buf,
strlen(client->send_buf) + 1, LBM_MSG_FLUSH | LBM_SRC_NONBLOCK);
EX_LBM_CHK(err);
printf("sent response to '%s'.\n", client->topic_name);
} /* handle_req */
int request_rcv_cb(lbm_rcv_t *rcv, lbm_msg_t *msg, void *clientd)
{
client_t *client = (client_t *)msg->source_clientd;
switch (msg->type)
{
case LBM_MSG_DATA: /* Received a message from the client. */
printf("Received %ld bytes on topic %s: '%.*s'\n",
(long)msg->len, msg->topic_name, (int)msg->len, msg->data);
/* Register message. */
if (msg->len > 1 && msg->data[0] == 'r') {
handle_register(client, &msg->data[1]);
}
/* Request message. */
else if (msg->len >= sizeof(req_hdr_t) && msg->data[0] == 'R') {
req_hdr_t req_hdr;
memcpy((char *)&req_hdr, msg->data, sizeof(req_hdr_t));
handle_req(client, &req_hdr, &msg->data[sizeof(req_hdr_t)],
msg->len - sizeof(req_hdr_t));
}
else {
printf("Ignored unrecognized command '%c'\n", msg->data[0]);
break;
}
break;
case LBM_MSG_BOS:
printf("[%s][%s], BOS\n", msg->topic_name, msg->source);
break;
case LBM_MSG_EOS:
printf("[%s][%s], EOS\n", msg->topic_name, msg->source);
break;
default: /* unexpected receiver event */
printf("Receive event type %x topic='%s', source='%s'\n", msg->type, msg->topic_name, msg->source);
break;
} /* switch msg->type */
return 0;
} /* request_rcv_cb */
void *start_client_source(const char *source_name, void *clientd)
{
lbm_context_t *ctx = (lbm_context_t *)clientd;;
client_t *new_client = (client_t *)malloc(sizeof(client_t));
NULL_CHK(new_client);
new_client->state = 0; /* Waiting for register. */
new_client->topic_name = NULL;
new_client->source_name = strdup(source_name);
NULL_CHK(new_client->source_name);
new_client->ctx = ctx;
new_client->resp_src = NULL;
return new_client;
} /* start_client_source */
int end_client_source(const char *source_name, void *clientd, void *source_clientd)
{
int err;
if (source_clientd) {
client_t *client = (client_t *)source_clientd;
if (client->topic_name) {
free(client->topic_name);
}
if (client->source_name) {
free(client->source_name);
}
err = lbm_src_delete(client->resp_src);
EX_LBM_CHK(err);
free(source_clientd);
}
return 0;
} /* end_client_source */
int main(int argc, char **argv)
{
lbm_context_t *ctx;
lbm_rcv_t *rcv; /* Receiver for requests from clients. */
int err;
#if defined(_MSC_VER)
/* windows-specific code */
WSADATA wsadata;
int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
if (wsStat != 0) {
printf("line %d: wsStat=%d\n",__LINE__,wsStat); exit(1);
}
#endif
err = lbm_config("serv.cfg");
EX_LBM_CHK(err);
err = lbm_context_create(&ctx, NULL, NULL, NULL);
EX_LBM_CHK(err);
/* Create receiver for requests from clients. */
{
lbm_rcv_src_notification_func_t source_notification_function;
lbm_rcv_topic_attr_t *rcv_attr;
lbm_topic_t *topic;
err = lbm_rcv_topic_attr_create(&rcv_attr);
EX_LBM_CHK(err);
/* Set up per-source state management. */
source_notification_function.create_func = start_client_source;
source_notification_function.delete_func = end_client_source;
source_notification_function.clientd = ctx; /* Pass this to start_client_source(). */
err = lbm_rcv_topic_attr_setopt(rcv_attr, "source_notification_function",
&source_notification_function, sizeof(source_notification_function));
EX_LBM_CHK(err);
err = lbm_rcv_topic_lookup(&topic, ctx, "Server1.Request", rcv_attr);
EX_LBM_CHK(err);
/* Pass context object as clientd. */
err = lbm_rcv_create(&rcv, ctx, topic, request_rcv_cb, ctx, NULL);
EX_LBM_CHK(err);
err = lbm_rcv_topic_attr_delete(rcv_attr);
EX_LBM_CHK(err);
}
while (1) {
SLEEP(1);
}
/* Finished all receiving from this topic, delete the receiver object. */
err = lbm_rcv_delete(rcv);
EX_LBM_CHK(err);
/* Do not need to delete the topic object - LBM keeps track of topic
* objects and deletes them as-needed. */
/* Finished with all LBM functions, delete the context object. */
err = lbm_context_delete(ctx);
EX_LBM_CHK(err);
#if defined(_MSC_VER)
WSACleanup();
#endif
return 0;
} /* main */
|