aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libsamp/sampMethods.c
blob: c76873a0f81ea3cb931da38fd12397dfb1577481 (plain) (blame)
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
/**
 *  SAMPMETHODS.C -- SAMP methods implemented by a callable client.
 *
 *  @brief      SAMP methods implemented by a callable client.
 *
 *  @file       sampMethods.c
 *  @author     Mike Fitzpatrick
 *  @date       7/10/09
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

#include "samp.h"

#define	METH_DBG   (getenv("METH_DBG")!=NULL||access("/tmp/METH_DBG",F_OK)==0)


extern	Map	   nullMap;
extern	List	   nullList;

extern	Samp	  *sampP;
extern  handle_t   sampH;

#define	OK_Map	   samp_getOKMap()

static	Map msg_reply = (Map) 0;



/**
 *  SAMP_RECEIVECALL -- receiveCall() client method.
 *
 *  @brief      receiveCall () client method.
 *  @fn         int samp_receiveCall (void *data)
 *
 *  @param  data        caller param data
 *  @return             status code or errno
 */
int
samp_receiveCall (void *data)
{
    char  *priv_key = xr_getStringFromParam (data, 0);
    char  *sender   = xr_getStringFromParam (data, 1);
    char  *msg_id   = xr_getStringFromParam (data, 2);
    int    msg_map  = xr_getStructFromParam (data, 3);
    int    params   = -1;
    char   buf[SZ_LINE], *mtype = buf;
    Map    resp;

    void  (*func)(String sender, String mtype, String msgid, Map map);


    xr_getStringFromStruct (msg_map, "samp.mtype", &mtype);
    xr_getStructFromStruct (msg_map, "samp.params", &params);

    if (sampP->trace) {
	fprintf (stderr, "rcvCall(%s) mid='%s' mtype='%s'\n", 
    	    sender, msg_id, mtype);
    }


    /*  Call the default user handler for all messages.  This is in addition
     *  to the handler installed for a particular mtype or Hub event.
     */
    if (sampP && sampP->defaultUserFunc)
        samp_execUserHandler (sender, mtype, 0, params);

    /*  Call the interface's special handler for the mtype.  This allows us
     *  to parse the message map ourselves and present the user with a 
     *  signature for their method that makes more sense.  We assume the
     *  sampHandler calls the userHandler(), if there is no handler defined
     *  then call the userHandler directly and reply() on the behalf of
     *  the user.
     */
    if ( (func = samp_getSampHandler (mtype)) )
	(*func) (sender, mtype, msg_id, params);
    else { 
        if ( (func = samp_getUserHandler (mtype)) )
	    (*func) (sender, mtype, msg_id, params);
	else
	    samp_genericMsgHandler (sender, mtype, msg_id, params);
    }

    resp = OK_Map;
resp = samp_getHandlerReply ();

    samp_Reply (sampH, msg_id, resp);	      /* send message reply   */

    xr_setStructInResult (data, resp);

    xr_freeStruct (msg_map);
    if (priv_key) free ((void *) priv_key);
    if (sender)  free ((void *) sender);
    if (msg_id)  free ((void *) msg_id);

    return (0);
}


/**
 *  SAMP_RECEIVENOTIFICATION -- receiveNotification() client method.
 *
 *  @brief      receiveNotification () client method.
 *  @fn         int samp_receiveNotification (void *data)
 *
 *  @param  data        caller param data
 *  @return             status code or errno
 */
int
samp_receiveNotification (void *data)
{
    char  *priv_key = xr_getStringFromParam (data, 0);
    char  *sender   = xr_getStringFromParam (data, 1);
    int    msg_map  = xr_getStructFromParam (data, 2);
    int    params   = -1;
    char   buf[SZ_LINE], *mtype = buf;
    Map    resp;

    void  (*func)(String sender, String mtype, String msgid, Map map);


    xr_getStringFromStruct (msg_map, "samp.mtype", &mtype);
    xr_getStructFromStruct (msg_map, "samp.params", &params);

    if (sampP->trace)
	fprintf (stderr, "rcvNotify(%s) mtype='%s'\n", sender, mtype);


    /*  Call the default user handler for all messages.  This is in addition
     *  to the handler installe for a particular mtype or Hub event.
     */
    if (sampP && sampP->defaultUserFunc)
        samp_execUserHandler (sender, mtype, 0, params);

    /*  Process any special Hub events as a special case.
     */
    if (samp_processHubEvent (mtype, params))
	goto done_;


    /*  Call the interface's special handler for the mtype.  This allows us
     *  to parse the message map ourselves and present the user with a 
     *  signature for their method that makes more sense.  We assume the
     *  sampHandler calls the userHandler(), if there is no handler defined
     *  then call the userHandler directly, no reply() to the sender is 
     *  required.
     */
    if ( (func = samp_getSampHandler (mtype)) )
	(*func) (sender, mtype, NULL, params);
    else {
	/*
	samp_execUserHandler (sender, mtype, 0, params);
	*/
        if ( (func = samp_getUserHandler (mtype)) )
	    (*func) (sender, mtype, 0, params);
	else
	    samp_genericMsgHandler (sender, mtype, 0, params);
    }

done_:
    resp = OK_Map;
resp = samp_getHandlerReply ();

    xr_setStructInResult (data, resp);

    xr_freeStruct (msg_map);
    if (priv_key) free ((void *) priv_key);
    if (sender)  free ((void *) sender);

    return (0);
}


/**
 *  SAMP_RECEIVERESPONSE -- receiveResponse() client method.
 *
 *  @brief      receiveResponse () client method.
 *  @fn         int samp_receiveResponse (void *data)
 *
 *  @param  data        caller param data
 *  @return             status code or errno
 */
int
samp_receiveResponse (void *data)
{
    char  *priv_key = xr_getStringFromParam (data, 0);
    char  *sender   = xr_getStringFromParam (data, 1);
    char  *msg_tag  = xr_getStringFromParam (data, 2);
    int    msg_map  = xr_getStructFromParam (data, 3);
    Map    resp_map = (Map) -1, params = (Map) 0;
    char   buf[SZ_LINE], *mtype = buf;


    xr_getStringFromStruct (msg_map, "samp.mtype", &mtype);
    xr_getStructFromStruct (msg_map, "samp.params", &resp_map);

    if (sampP->trace) {
	fprintf (stderr, "rcvResponse(%s) id='%s' mtype='%s'\n", 
	    sender, msg_tag, mtype);
    }

    /*  Call the default user handler for all messages.  This is in addition
     *  to the handler installe for a particular mtype or Hub event.
     */
    if (sampP && sampP->defaultUserFunc)
        samp_execUserHandler (sender, mtype, 0, params);


    /*  Reply to the Hub.
     */
    xr_setStructInResult (data, OK_Map);

    xr_freeStruct (msg_map);
    if (priv_key) free ((void *) priv_key);
    if (sender)   free ((void *) sender);
    if (msg_tag)  free ((void *) msg_tag);

    return (0);
}


/**
 *  SAMP_SETHANDLERREPLY -- Set the Samp Handler reply map.
 *
 *  @brief      Set the Samp Handler reply map.
 *  @fn         samp_setHandlerReply (Map resp)
 *
 *  @param  resp        response map
 *  @return             nothing
 */
void
samp_setHandlerReply (Map resp)
{
    msg_reply = resp;
}


/**
 *  SAMP_GETHANDLERREPLY -- Get the Samp Handler reply map.
 *
 *  @brief      Get the Samp Handler reply map.
 *  @fn         int samp_getHandlerReply (void)
 *
 *  @return             Map handle for reply map
 */
Map
samp_getHandlerReply ()
{
    Map resp = msg_reply;

    msg_reply = nullMap;
    return ( (resp != nullMap && resp != 0) ? resp : OK_Map );
}