File: radmsgRouter.h

package info (click to toggle)
radlib 2.12.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: ansic: 15,843; sh: 8,102; makefile: 501
file content (289 lines) | stat: -rw-r--r-- 9,939 bytes parent folder | download | duplicates (3)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef INC_radmsgrouterh
#define INC_radmsgrouterh
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------------------

  FILENAME:
        radmsgRouter.h

  PURPOSE:
        Provide a standalone message routing process and API to support the 
        "route by message ID" paradigm.

  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
        12/03/2005      M.S. Teel       0               Original

  NOTES:
        There will be one message router process per radlib system ID. The 
        message router process is built and installed when radlib is built and 
        installed and will be found at "$prefix/bin/radmrouted". radmrouted
        must be started before any other radlib processes which plan to use 
        the message routing utility. radmrouted can be started and stopped like 
        any other radlib process with one requirement: the radlib system ID and
        the working directory for the application must be passed as arguments 
        to radmrouted.
        
        USAGE: [prefix]/radmrouted radSystemID workingDirectory <listenPort> <remoteIP:remotePort>
        
            radSystemID           the same system ID used by other processes in 
                                  this group
            workingDirectory      where to store FIFO and pid files for radmrouted
            <listenPort>          optional local port to listen on for remote 
                                  router connections
            <remoteIP:remotePort> optional remote server IP address and port to 
                                  connect to

        "workingDirectory" should match the working directory passed to 
        'radMsgRouterInit' by your processes.


        radlib processes which want to be a message producer and/or consumer 
        will initialize the message router interface then register or deregister 
        for particular message types as required.

  LICENSE:
        Copyright 2001-2010 Mark S. Teel. All rights reserved.

        Redistribution and use in source and binary forms, with or without 
        modification, are permitted provided that the following conditions 
        are met:

        1. Redistributions of source code must retain the above copyright 
           notice, this list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright 
           notice, this list of conditions and the following disclaimer in the 
           documentation and/or other materials provided with the distribution.

        THIS SOFTWARE IS PROVIDED BY Mark Teel ``AS IS'' AND ANY EXPRESS OR 
        IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
        DISCLAIMED. IN NO EVENT SHALL MARK TEEL OR CONTRIBUTORS BE LIABLE FOR 
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
        STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
        IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
        POSSIBILITY OF SUCH DAMAGE.
  
----------------------------------------------------------------------------*/

//  System include files
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>

//  Library include files
#include <radsysdefs.h>
#include <radsysutils.h>
#include <radprocutils.h>
#include <radlist.h>
#include <radprocess.h>
#include <radsocket.h>
#include <radthread.h>


///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///
///*/*/*/*    H I D D E N - I N T E R N A L  U S E  O N L Y    */*/*/*/*/*/*///
///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///

#define MSGRTR_LOCK_FILE_NAME           "radmrouted.pid"
#define MSGRTR_QUEUE_NAME               "radmroutedfifo"
#define PROC_NAME_MSGRTR                "radmrouted"
#define MSGRTR_NUM_TIMERS               1

#define MSGRTR_REMOTE_RETRY_INTERVAL    5000            // 5 secs
#define MSGRTR_MAX_ACK_WAIT             1000

#define MSGRTR_INTERNAL_MSGID           0xFFFFFFFF

#define MSGRTR_MAX_CLIENTS              32


// Define PIB types:
typedef enum
{
    PIB_TYPE_LOCAL      = 1,
    PIB_TYPE_REMOTE
} MSGRTR_PIB_TYPE;

// define the process information block (PIB):
typedef struct
{
    NODE            node;
    MSGRTR_PIB_TYPE type;
    char            name[PROCESS_MAX_NAME_LEN+1];

    // Local clients:
    int             pid;
    char            queueName[QUEUE_NAME_LENGTH+1];

    // Remote clients:
    RADSOCK_ID      rxclient;
    RADSOCK_ID      txclient;
    ULONG           maxMsgSize;

    // Stats:
    ULONG           transmits;
    ULONG           receives;
    ULONG           rxErrors;
    ULONG           txErrors;
} MSGRTR_PIB;


// define the consumer information block (CIB):
typedef struct
{
    NODE            node;
    MSGRTR_PIB      *pib;               // points to members of master PIB list
} MSGRTR_CIB;


// define the message ID information block (MIIB)
// this contains the list of registered consumers:
typedef struct
{
    NODE            node;
    ULONG           msgID;
    RADLIST         consumers;          // list of MSGRTR_CIB
} MSGRTR_MIIB;


// define the message router work data:
typedef struct
{
    UCHAR           radSystemID;
    pid_t           myPid;
    char            pidFile[128];
    char            fifoFile[128];
    TIMER_ID        remoteConnectTimer;

    char            remoteIP[256];
    int             remotePort;
    RADSOCK_ID      remoteServer;

    RADLIST         pibList;            // list of registrants (MSGRTR_PIB)
    RADLIST         miibList;           // list of messages by msgID (MSGRTR_MIIB)

    RADSOCK_ID      server;
    int             listenPort;

    ULONG           transmits;
    ULONG           receives;
} MSGRTR_WORK;


// define the process-specific message router work data:
typedef struct
{
    char            rtrQueueName[QUEUE_NAME_LENGTH+1];
} MSGRTR_LOCAL_WORK;


// define the message router message header:
#define MSGRTR_MAGIC_NUMBER             0x59E723F3
typedef struct
{
    ULONG           magicNumber;        // guard against non-API sends to router
    int             srcpid;
    ULONG           msgID;
    ULONG           length;
    UCHAR           msg[0];             // placeholder for start of message
} MSGRTR_HDR;

// define the internal admin message subtypes:
enum
{
    MSGRTR_SUBTYPE_REGISTER         = 1,
    MSGRTR_SUBTYPE_DEREGISTER,
    MSGRTR_SUBTYPE_ACK,
    MSGRTR_SUBTYPE_ENABLE_MSGID,
    MSGRTR_SUBTYPE_MSGID_IS_REGISTERED,
    MSGRTR_SUBTYPE_DISABLE_MSGID,
    MSGRTR_SUBTYPE_DUMP_STATS
};

// define the internal admin message:
typedef struct
{
    ULONG           subMsgID;
    char            name[PROCESS_MAX_NAME_LEN+1];
    ULONG           targetMsgID;
    char            srcIP[128];
    int             srcPort;
    ULONG           socketID;
    ULONG           maxMsgSize;
    int             isRegistered;
} MSGRTR_INTERNAL_MSG;
    

///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///
///*/*/*/*                  E N D  H I D D E N                 */*/*/*/*/*/*///
///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///


///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///
///*/*/*/*                 A P I  M E T H O D S                */*/*/*/*/*/*///
///*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*///

//  Register the message router API for the running process
//  - 'workingDir' specifies where the pid file and FIFOs for the message 
//    router process are to be maintained
//  - returns OK or ERROR
//  Note: 'radSystemInit' and 'radProcessInit' must have been called prior to 
//        calling this function
extern int radMsgRouterInit (char *workingDir);


//  Deregister the message router API for the running process
extern void radMsgRouterExit (void);


//  Deregister process with pid "pid" from the msgRouter
extern void radMsgRouterProcessExit (int pid);


//  Request to receive 'msgID' messages
//  - returns OK or ERROR
//  Note: msgID "MSGRTR_INTERNAL_MSGID" is reserved for internal use
extern int radMsgRouterMessageRegister (ULONG msgID);


//  Request if there are any subscribers to 'msgID' messages
//  - returns FALSE or TRUE
//  Note: msgID "MSGRTR_INTERNAL_MSGID" is reserved for internal use
extern int radMsgRouterMessageIsRegistered (ULONG msgID);


//  Request to NOT receive 'msgID' messages
//  - returns OK or ERROR
//  Note: msgID "MSGRTR_INTERNAL_MSGID" is reserved for internal use
extern int radMsgRouterMessageDeregister (ULONG msgID);


//  Send a message through the message router - all processes which have
//  subscribed to 'msgID' will receive a copy of the message;
//  'msg' will be copied - ownership of 'msg' is NOT transferred but remains 
//  with the caller;
//  - returns OK or ERROR
//  Note: msgID "MSGRTR_INTERNAL_MSGID" is reserved for internal use
extern int radMsgRouterMessageSend (ULONG msgID, void *msg, ULONG byteLength);


//  instruct the message router to dump statistics to the log file
//  - returns OK or ERROR
extern int radMsgRouterStatsDump (void);


#ifdef __cplusplus
}
#endif
#endif