File: service.c

package info (click to toggle)
meanwhile 1.0.2-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 3,503
  • sloc: ansic: 13,856; sh: 8,360; makefile: 145
file content (267 lines) | stat: -rw-r--r-- 6,776 bytes parent folder | download | duplicates (5)
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

/*
  Meanwhile - Unofficial Lotus Sametime Community Client Library
  Copyright (C) 2004  Christopher (siege) O'Brien
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "mw_channel.h"
#include "mw_debug.h"
#include "mw_error.h"
#include "mw_message.h"
#include "mw_service.h"


/* I tried to be explicit with the g_return_* calls, to make the debug
   logging a bit more sensible. Hence all the explicit "foo != NULL"
   checks. */


void mwService_recvCreate(struct mwService *s, struct mwChannel *chan,
			  struct mwMsgChannelCreate *msg) {

  /* ensure none are null, ensure that the service and channel belong
     to the same session, and ensure that the message belongs on the
     channel */
  g_return_if_fail(s != NULL);
  g_return_if_fail(chan != NULL);
  g_return_if_fail(msg != NULL);
  g_return_if_fail(s->session == mwChannel_getSession(chan));
  g_return_if_fail(mwChannel_getId(chan) == msg->channel);

  if(s->recv_create) {
    s->recv_create(s, chan, msg);
  } else {
    mwChannel_destroy(chan, ERR_FAILURE, NULL);
  }
}


void mwService_recvAccept(struct mwService *s, struct mwChannel *chan,
			  struct mwMsgChannelAccept *msg) {

  /* ensure none are null, ensure that the service and channel belong
     to the same session, and ensure that the message belongs on the
     channel */
  g_return_if_fail(s != NULL);
  g_return_if_fail(chan != NULL);
  g_return_if_fail(msg != NULL);
  g_return_if_fail(s->session == mwChannel_getSession(chan));
  g_return_if_fail(mwChannel_getId(chan) == msg->head.channel);

  if(s->recv_accept)
    s->recv_accept(s, chan, msg);
}


void mwService_recvDestroy(struct mwService *s, struct mwChannel *chan,
			   struct mwMsgChannelDestroy *msg) {

  /* ensure none are null, ensure that the service and channel belong
     to the same session, and ensure that the message belongs on the
     channel */
  g_return_if_fail(s != NULL);
  g_return_if_fail(chan != NULL);
  g_return_if_fail(msg != NULL);
  g_return_if_fail(s->session == mwChannel_getSession(chan));
  g_return_if_fail(mwChannel_getId(chan) == msg->head.channel);

  if(s->recv_destroy)
    s->recv_destroy(s, chan, msg);
}


void mwService_recv(struct mwService *s, struct mwChannel *chan,
		    guint16 msg_type, struct mwOpaque *data) {

  /* ensure that none are null. zero-length messages are acceptable */
  g_return_if_fail(s != NULL);
  g_return_if_fail(chan != NULL);
  g_return_if_fail(data != NULL);
  g_return_if_fail(s->session == mwChannel_getSession(chan));

  /*
  g_message("mwService_recv: session = %p, service = %p, b = %p, n = %u",
	    mwService_getSession(s), s, data->data, data->len);
  */

  if(s->recv)
    s->recv(s, chan, msg_type, data);
}


guint32 mwService_getType(struct mwService *s) {
  g_return_val_if_fail(s != NULL, 0x00);
  return s->type;
}


const char *mwService_getName(struct mwService *s) {
  g_return_val_if_fail(s != NULL, NULL);
  g_return_val_if_fail(s->get_name != NULL, NULL);

  return s->get_name(s);
}


const char *mwService_getDesc(struct mwService *s) {
  g_return_val_if_fail(s != NULL, NULL);
  g_return_val_if_fail(s->get_desc != NULL, NULL);

  return s->get_desc(s);
}


struct mwSession *mwService_getSession(struct mwService *s) {
  g_return_val_if_fail(s != NULL, NULL);
  return s->session;
}


void mwService_init(struct mwService *srvc, struct mwSession *sess,
		    guint32 type) {

  /* ensure nothing is null, and there's no such thing as a zero
     service type */
  g_return_if_fail(srvc != NULL);
  g_return_if_fail(sess != NULL);
  g_return_if_fail(type != 0x00);

  srvc->session = sess;
  srvc->type = type;
  srvc->state = mwServiceState_STOPPED;
}


enum mwServiceState mwService_getState(struct mwService *srvc) {
  g_return_val_if_fail(srvc != NULL, mwServiceState_STOPPED);
  return srvc->state;
}


void mwService_start(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  if(! MW_SERVICE_IS_STOPPED(srvc))
    return;

  srvc->state = mwServiceState_STARTING;
  g_message("starting service %s", NSTR(mwService_getName(srvc)));

  if(srvc->start) {
    srvc->start(srvc);
  } else {
    mwService_started(srvc);
  }
}


void mwService_started(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  srvc->state = mwServiceState_STARTED;
  g_message("started service %s", NSTR(mwService_getName(srvc)));
}


void mwService_error(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  if(MW_SERVICE_IS_DEAD(srvc))
    return;

  srvc->state = mwServiceState_ERROR;
  g_message("error in service %s", NSTR(mwService_getName(srvc)));

  if(srvc->stop) {
    srvc->stop(srvc);
  } else {
    mwService_stopped(srvc);
  }
}


void mwService_stop(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  if(MW_SERVICE_IS_DEAD(srvc))
    return;

  srvc->state = mwServiceState_STOPPING;
  g_message("stopping service %s", NSTR(mwService_getName(srvc)));

  if(srvc->stop) {
    srvc->stop(srvc);
  } else {
    mwService_stopped(srvc);
  }
}


void mwService_stopped(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  if(srvc->state != mwServiceState_STOPPED) {
    srvc->state = mwServiceState_STOPPED;
    g_message("stopped service %s", NSTR(mwService_getName(srvc)));
  }
}


void mwService_free(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  mwService_stop(srvc);

  if(srvc->clear)
    srvc->clear(srvc);

  if(srvc->client_cleanup)
    srvc->client_cleanup(srvc->client_data);

  g_free(srvc);
}


/** @todo switch the following to using mw_datum */

void mwService_setClientData(struct mwService *srvc,
			     gpointer data, GDestroyNotify cleanup) {

  g_return_if_fail(srvc != NULL);

  srvc->client_data = data;
  srvc->client_cleanup = cleanup;
}


gpointer mwService_getClientData(struct mwService *srvc) {
  g_return_val_if_fail(srvc != NULL, NULL);
  return srvc->client_data;
}


void mwService_removeClientData(struct mwService *srvc) {
  g_return_if_fail(srvc != NULL);

  if(srvc->client_cleanup) {
    srvc->client_cleanup(srvc->client_data);
    srvc->client_cleanup = NULL;
  }

  srvc->client_data = NULL;
}