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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/**
* SAMPCOMMANDS.C -- SAMP commands used by app to send administrative messages.
*
*
* stat = samp_Register (handle)
* stat = samp_UnRegister (handle)
* stat = samp_DeclareMetadata (handle)
* stat = samp_Ping (handle, appName)
* map = samp_GetMetadata (handle, pubId)
* samp_DeclareSubscriptions (handle)
* map = samp_GetSubscriptions (handle)
* list = samp_GetRegisteredClients (handle)
* list = samp_GetSubscribedClients (handle, mtype)
*
*
* @brief SAMP commands used by app to send administrative messages.
*
* @file sampCommands.c
* @author Mike Fitzpatrick
* @date 7/10/11
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "samp.h"
/*****************************************************************************
*/
/**
* SAMP_REGISTER -- Register with the Hub using the currently stored metadata.
*
* @brief Register with the Hub using the currently stored metadata.
* @fn handle = samp_Register (handle_t handle)
*
* @param handle samp struct handle
* @return SAMP_OK or SAMP_ERR
*/
int
samp_Register (handle_t handle)
{
char *privateKey, *hubId, *selfId;
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
int reg;
if (!hub)
return (SAMP_ERR);
privateKey = hub->privateKey;
hubId = hub->hubId;
selfId = hub->selfId;
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->secret);
xr_callSync (hub->id, "samp.hub.register");
/* Check for an error response.
*/
if (samp_replyStatus (hub->id) != SAMP_OK) {
fprintf (stderr, "Hub registration failed.\n");
return (SAMP_ERR);
}
xr_getStructFromResult (hub->id, ®);
xr_getStringFromStruct (reg, "samp.private-key", &privateKey);
xr_getStringFromStruct (reg, "samp.hub-id", &hubId);
xr_getStringFromStruct (reg, "samp.self-id", &selfId);
/* Save the private key we get back. Note there is a difference in
* behavior between the hubs as to whether we strip any prefix on the
* key returned in the response.
*
* FIXME ??
*/
strcpy (hub->privateKey, &hub->privateKey[7]);
return (SAMP_OK);
}
/**
* SAMP_UNREGISTER -- Un-Register from the hub.
*
* @brief Un-Register from the hub.
* @fn stat = samp_UnRegister (handle_t handle)
*
* @param handle samp struct handle
* @return SAMP_OK or SAMP_ERR
*/
int
samp_UnRegister (handle_t handle)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
return ( hub ? samp_hubUnRegister (hub) : SAMP_ERR );
}
/**
* SAMP_DECLAREMETATA -- (Re)Declare all of our metadata.
*
* @brief (Re)Declare all of our metadata.
* @fn stat = samp_DeclareMetadata (handle_t handle)
*
* @param handle samp struct handle
* @return SAMP_OK or SAMP_ERR
*/
int
samp_DeclareMetadata (handle_t handle)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
return ( hub ? samp_hubDeclareMetadata (hub) : SAMP_ERR );
}
/**
* SAMP_PING -- Ping the hub/app to see if it is alive (returns >0).
*
* @brief Ping the hub/app to see if it is alive (returns >0).
* @fn stat = samp_Ping (handle_t handle, String appName)
*
* @param handle samp struct handle
* @param appName application name
* @return OK or ERR if no response
*/
int
samp_Ping (handle_t handle, String appName)
{
int res = -1, status = SAMP_OK;
Samp *sampP = samp_H2P(handle);
Map resp = (Map) 0;
char *tag = samp_msgTag();
char *pubId = samp_app2id (handle, appName);
if (strcasecmp (appName, "hub") == 0) {
return ( res = samp_hubPing (sampP->hub) );
} else {
Msg msg = samp_newMsg ();
Param param = samp_newParam ();
samp_msgMType (msg, "samp.app.ping");
samp_msgParam (msg, param);
if (strcasecmp (appName, "all") == 0) {
resp = samp_callAll (handle, tag, msg);
} else {
resp = samp_callAndWait (handle, pubId, tag, msg);
status = samp_setErr (handle, resp);
}
samp_freeMap (resp);
samp_freeMsg (msg);
return (status);
}
return (SAMP_ERR);
}
/**
* SAMP_GETMETADATA -- Get the metadata for a specified app.
*
* @brief Get the metadata for a specified app.
* @fn map = samp_GetMetadata (handle_t handle, String pubId)
*
* @param handle samp struct handle
* @param pubId App public-id
* @return Map to message response
*/
Map
samp_GetMetadata (handle_t handle, String pubId)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
Map resp = (Map) 0;
if (!hub)
return ((Map) 0);
xr_initParam (hub->id); /* set calling parameters */
xr_setStringInParam (hub->id, hub->privateKey);
xr_setStringInParam (hub->id, pubId);
if (xr_callSync (hub->id, "samp.hub.getMetadata") == OK) {
xr_getStructFromResult (hub->id, &resp);
return (resp);
} else {
memset (sampP->errortxt, 0, SZ_LINE);
strcpy (sampP->errortxt, xr_getErrMsg (hub->id));
fprintf (stderr, "Error: '%s'\n", sampP->errortxt);
}
return ( (Map) 0 );
}
/**
* SAMP_DECLARESUBSCRIPIONS -- Declare the messages we're interested in.
*
* @brief Declare the messages we're interested in.
* @fn stat = samp_DeclareSubscriptions (handle_t handle)
*
* @param handle samp struct handle
* @return SAMP_OK or SAMP_ERR
*/
int
samp_DeclareSubscriptions (handle_t handle)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
return ( hub ? samp_hubDeclareSubscriptions (hub) : SAMP_ERR );
}
/**
* SAMP_GETSUBSCRIPTIONS -- Get the message subscriptions for a specific app.
*
* @brief Get the message subscriptions for a specific app.
* @fn subs[] = samp_GetSubscriptions (handle_t handle, String pubId)
*
* @param handle samp struct handle
* @param pubId public ID of target app
* @return Array of target subscriptions
*/
Map
samp_GetSubscriptions (handle_t handle, String pubId)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
Map resp = (Map) 0;
if (!hub)
return ( (Map) 0);
xr_initParam (hub->id); /* set calling parameters */
xr_setStringInParam (hub->id, hub->privateKey);
xr_setStringInParam (hub->id, pubId);
if (xr_callSync (hub->id, "samp.hub.getSubscriptions") == OK) {
xr_getStructFromResult (hub->id, &resp);
return ( (Map) 0);
}
return ( (Map) 0 );
}
/**
* SAMP_GETREGISTEREDCLIENTS -- Get public-ids of the registered clients.
*
* @brief Get public-ids of the registered clients.
* @fn handle = samp_GetRegisteredClients (handle_t handle)
*
* @param handle samp struct handle
* @return handle to list of registered clients
*/
List
samp_GetRegisteredClients (handle_t handle)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
int i, res;
List lres = (List) 0;
char name[64], *str = name;
if (!hub)
return ((List) 0);
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->privateKey);
if (xr_callSync (hub->id, "samp.hub.getRegisteredClients") == OK) {
xr_getArrayFromResult (hub->id, &res);
lres = samp_newList ();
for (i=0; i < samp_listLen (res); i++) {
xr_getStringFromArray (res, i, &str);
samp_setStringInList (lres, str);
//free ((void *) str);
}
} else {
lres = samp_newList ();
samp_setStringInList (lres, "");
}
return (lres);
}
/**
* SAMP_GETSUBSCRIBEDCLIENTS -- Get clients matching the mtype subscription.
*
* @brief Get clients matching the mtype subscription.
* @fn list = samp_GetSubscribedClients (handle_t handle, String mtype)
*
* @param handle samp struct handle
* @param mtype mtype string
* @return handle to list of clients having mtype subscription
*/
List
samp_GetSubscribedClients (handle_t handle, String mtype)
{
Samp *sampP = samp_H2P(handle);
Hub *hub = sampP->hub;
int i, res;
List lres = (List) 0;
char name[64], *str = name;
if (!hub)
return ((List) 0);
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->privateKey);
xr_setStringInParam (hub->id, mtype);
xr_callSync (hub->id, "samp.hub.getSubscribedClients");
xr_getArrayFromResult (hub->id, &res);
lres = samp_newList ();
for (i=0; i < samp_listLen (res); i++) {
xr_getStringFromArray (res, i, &str);
samp_setStringInList (lres, str);
}
return (lres);
}
|