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
351
352
353
354
355
356
357
|
/**
* SAMPCOMMANDS.C -- SAMP commands used by app to send administrative messages.
*
*
* stat = samp_Register (samp)
* stat = samp_UnRegister (samp)
* stat = samp_DeclareMetadata (samp)
* stat = samp_Ping (samp, appName)
* map = samp_GetMetadata (samp, pubId)
* samp_DeclareSubscriptions (samp, subscripMap)
* map = samp_GetSubscriptions (samp)
* list = samp_GetRegisteredClients (samp, 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. We use the currently stored
* metadata.
*
* @brief ...
* @fn handle = samp_hubOpen (Samp *samp)
*
* @param samp ...
* @return ...
*/
int
samp_Register (handle_t samp)
{
char *privateKey, *hubId, *selfId;
Samp *sampP = samp_H2P(samp);
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.
*/
strcpy (hub->privateKey, &hub->privateKey[7]);
return (SAMP_OK);
}
/**
* SAMP_UNREGISTER -- Un-Register with the hub.
*
* @brief ...
* @fn handle = samp_hubOpen (Samp *samp)
*
* @param samp ...
* @return ...
*/
int
samp_UnRegister (handle_t samp)
{
Samp *sampP = samp_H2P(samp);
Hub *hub = sampP->hub;
if (!hub)
return (SAMP_ERR);
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->privateKey);
xr_callSync (hub->id, "samp.hub.unregister");
/* Check for error result.
*/
;
return (SAMP_OK);
}
/**
* SAMP_DECLAREMETATA -- (Re)Declare all of our metadata.
*
* @brief ...
* @fn handle = samp_hubOpen (Samp *samp)
*
* @param samp ...
* @return ...
*/
int
samp_DeclareMetadata (handle_t samp)
{
Samp *sampP = samp_H2P(samp);
Hub *hub = sampP->hub;
int map;
if (!hub)
return (SAMP_ERR);
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->appId);
map = xr_newStruct ();
xr_setStringInStruct (map, "samp.name", hub->appName);
xr_setStringInStruct (map, "samp.description.text", hub->description);
xr_setStructInParam (hub->id, map);
xr_callSync (hub->id, "samp.hub.declareMetadata");
/* ignore result */
return (SAMP_OK);
}
/**
* 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 handle = samp_Ping (handle_t samp, String appName)
*
* @param samp samp struct handle
* @param appName application name
* @return OK or ERR if no response
*/
int
samp_Ping (handle_t samp, String appName)
{
Samp *sampP = samp_H2P(samp);
char *tag = (char *) NULL;
int res = -1;
Map resp = (Map) 0;
if (strncasecmp (appName, "hub", 3) == 0) {
res = samp_hubPing (sampP->hub);
return ( samp_hubPing (sampP->hub) );
} else {
Msg msg = samp_newMsg ();
Param param = samp_newParam ();
samp_msgMType (msg, "samp.app.ping");
samp_msgParam (msg, param);
tag = samp_msgTag();
if (strncasecmp (appName, "all", 3) == 0) {
/* callAll */
resp = samp_callAll (samp, tag, msg);
} else {
/* call */
resp = samp_callAndWait (samp, appName, tag, msg);
samp_freeMap (resp);
}
samp_freeMsg (msg);
}
return (SAMP_ERR);
}
/**
* SAMP_GETMETADATA -- Get the metadata for a specified app.
*
* @brief Get the metadata for a specified app.
* @fn map = samp_GetMetadata (Samp *samp, String pubId)
*
* @param samp samp struct handle
* @param pubId App public-id
* @return Map to message response
*/
Map
samp_GetMetadata (handle_t samp, String pubId)
{
Samp *sampP = samp_H2P(samp);
Hub *hub = sampP->hub;
Map resp = (Map) 0;
/*
Map err = (Map) 0, snum = (Map) 0, res = (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);
/*
char rstr[SZ_RESSTR], *sres = rstr;
resp = samp_newMap ();
xr_getStringFromStruct (snum, "samp.status", &sres);
if (strcasecmp (sres, "samp.ok") != 0) {
xr_freeStruct (snum);
return ( (Map) 0 );
} else {
xr_getStructFromStruct (snum, "samp.error", &err);
xr_getStringFromStruct (snum, "samp.errortxt", &sres);
memset (sampP->errortxt, 0, SZ_LINE);
strcpy (sampP->errortxt, sres);
xr_getStructFromStruct (snum, "samp.result", &res);
}
xr_freeStruct (snum);
*/
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 ...
* @fn handle = samp_hubOpen (Samp *samp)
*
* @param samp ...
* @return ...
*/
void
samp_DeclareSubscriptions (handle_t samp, Map subscriptions)
{
Samp *sampP = samp_H2P(samp);
Hub *hub = sampP->hub;
if (!hub)
return;
xr_initParam (hub->id);
/* NYI */
}
/**
* SAMP_GETSUBSCRIPTIONS -- Get the message subscriptions for a specific app.
*
* @brief ...
* @fn handle = samp_hubOpen (Samp *samp, String pubId)
*
* @param samp ...
* @return ...
*/
Map
samp_GetSubscriptions (handle_t samp, String pubId)
{
Samp *sampP = samp_H2P(samp);
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 (resp);
}
return ( (Map) 0 );
}
/**
* SAMP_GETREGISTEREDCLIENTS -- Get the list of public-ids of the registered
* clients.
*
* @brief ...
* @fn handle = samp_hubOpen (Samp *samp)
*
* @param samp ...
* @return ...
*/
List
samp_GetRegisteredClients (handle_t samp)
{
Samp *sampP = samp_H2P(samp);
Hub *hub = sampP->hub;
int res;
List lres = (List) 0;
if (!hub)
return ((List) 0);
xr_initParam (hub->id);
xr_setStringInParam (hub->id, hub->privateKey);
xr_callSync (hub->id, "samp.hub.getRegisteredClients");
xr_getArrayFromResult (hub->id, &res);
/*
lres = samp_newList ();
for (i=0; i < samp_listLen (res); i++) {
samp_setStringInList (lres, xr_getStringFromList (list, i);
}
*/
lres = res;
return (lres);
}
|