File: ijs_client.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (311 lines) | stat: -rw-r--r-- 7,822 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * Copyright (C) 2001-2002 Artifex Software, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

#include "unistd_.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ijs.h"
#include "ijs_client.h"

struct _IjsClientCtx {
  int fd_from;
  int child_pid;
  IjsSendChan send_chan;
  IjsRecvChan recv_chan;
  int version;
};

IjsClientCtx *
ijs_invoke_server (const char *server_cmd)
{
  IjsClientCtx *ctx;
  int fds_to[2], fds_from[2];
  int child_pid;
  char helo_buf[8] = IJS_HELO_STR;
  char resp_buf[8];
  const char exp_resp_buf[8] = IJS_RESP_STR;
  ijs_bool ok = TRUE;
  int nbytes;
  int version;

  if (ijs_exec_server(server_cmd, &fds_to[1], &fds_from[0], &child_pid) < 0)
    return NULL;

  ctx = (IjsClientCtx *)malloc (sizeof(IjsClientCtx));
  ctx->fd_from = fds_from[0];
  ctx->child_pid = child_pid;
  ijs_send_init (&ctx->send_chan, fds_to[1]);
  ijs_recv_init (&ctx->recv_chan, fds_from[0]);

  nbytes = write (ctx->send_chan.fd, helo_buf, sizeof(helo_buf));
  if (nbytes != sizeof(helo_buf))
    ok = FALSE;

  if (ok)
    {
      nbytes = read (ctx->recv_chan.fd, resp_buf, sizeof(resp_buf));
      if (nbytes != sizeof(resp_buf) ||
	  memcmp (resp_buf, exp_resp_buf, sizeof(resp_buf)))
	ok = FALSE;
    }

  /* exchange version information with server */
  if (ok)
    ok = ijs_client_begin_cmd (ctx, IJS_CMD_PING) >= 0;
  if (ok)
    ok = ijs_send_int (&ctx->send_chan, IJS_VERSION) >= 0;
  if (ok)
    ok = ijs_client_send_cmd_wait (ctx) >= 0;
  if (ok)
    ok = ijs_recv_int (&ctx->recv_chan, &version) >= 0;
  if (ok)
    {
      if (version > IJS_VERSION)
	version = IJS_VERSION;
      ctx->version = version;
    }

  if (!ok)
    {
      close (ctx->send_chan.fd);
      close (ctx->recv_chan.fd);
      free (ctx);
      ctx = NULL;
    }

  return ctx;
}

int
ijs_client_begin_cmd (IjsClientCtx *ctx, IjsCommand cmd)
{
  return ijs_send_begin (&ctx->send_chan, cmd);
}

int
ijs_client_send_int (IjsClientCtx *ctx, int val)
{
  return ijs_send_int (&ctx->send_chan, val);
}

int
ijs_client_send_cmd (IjsClientCtx *ctx)
{
  return ijs_send_buf (&ctx->send_chan);
}

/**
 * ijs_client_send_cmd_wait: Send command and wait for ack.
 * @ctx: IJS client context.
 *
 * Sends the command in the client context's buffer, and waits for ack.
 *
 * Return value: 0 on successful ack, otherwise negative.
 **/
int
ijs_client_send_cmd_wait (IjsClientCtx *ctx)
{
  int status;

  status = ijs_client_send_cmd (ctx);
  if (status >= 0)
    {
      status = ijs_recv_ack (&ctx->recv_chan);
    }
  return status;
}

/* This is the blocking version; it's not likely to be efficient */
int
ijs_client_send_data_wait (IjsClientCtx *ctx, IjsJobId job_id,
			   const char *buf, int size)
{
  int status;

  ijs_client_begin_cmd (ctx, IJS_CMD_SEND_DATA_BLOCK);
  ijs_send_int (&ctx->send_chan, job_id);
  ijs_send_int (&ctx->send_chan, size);
  status = ijs_client_send_cmd (ctx);
  if (status)
    return status;
  status = write (ctx->send_chan.fd, buf, size);
  if (status != size)
    return IJS_EIO;
  status = ijs_recv_ack (&ctx->recv_chan);
  return status;
}

int
ijs_client_open (IjsClientCtx *ctx)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_OPEN);
  return ijs_client_send_cmd_wait (ctx);
}

int
ijs_client_close (IjsClientCtx *ctx)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_CLOSE);
  return ijs_client_send_cmd_wait (ctx);
}

int
ijs_client_begin_job (IjsClientCtx *ctx, IjsJobId job_id)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_BEGIN_JOB);
  ijs_send_int (&ctx->send_chan, job_id);
  return ijs_client_send_cmd_wait (ctx);
}

int
ijs_client_end_job (IjsClientCtx *ctx, IjsJobId job_id)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_END_JOB);
  ijs_send_int (&ctx->send_chan, job_id);
  return ijs_client_send_cmd_wait (ctx);
}

/**
 * Return value: data block size if nonnegative, or error code if
 * negative.
 **/
int
ijs_client_list_params (IjsClientCtx *ctx, IjsJobId job_id,
		      char *value, int value_size)
{
  int status;

  ijs_client_begin_cmd (ctx, IJS_CMD_LIST_PARAMS);
  ijs_send_int (&ctx->send_chan, job_id);
  status = ijs_client_send_cmd (ctx);
  if (status)
    return status;
  status = ijs_recv_ack (&ctx->recv_chan);
  if (status)
    return status;
  status = ijs_recv_block (&ctx->recv_chan, value, value_size);
  return status;
}

/**
 * Return value: data block size if nonnegative, or error code if
 * negative.
 **/
int
ijs_client_enum_param (IjsClientCtx *ctx, IjsJobId job_id,
		      const char *key,
		      char *value, int value_size)
{
  int key_size = strlen (key);
  int status;

  ijs_client_begin_cmd (ctx, IJS_CMD_ENUM_PARAM);
  ijs_send_int (&ctx->send_chan, job_id);
  status = ijs_send_block (&ctx->send_chan, key, key_size + 1);
  if (status < 0)
    return IJS_EIO;
  status = ijs_client_send_cmd (ctx);
  if (status)
    return status;
  status = ijs_recv_ack (&ctx->recv_chan);
  if (status)
    return status;
  status = ijs_recv_block (&ctx->recv_chan, value, value_size);
  return status;
}

int
ijs_client_set_param (IjsClientCtx *ctx, IjsJobId job_id,
		      const char *key,
		      const char *value, int value_size)
{
  int key_size = strlen (key);
  int status;

  ijs_client_begin_cmd (ctx, IJS_CMD_SET_PARAM);
  ijs_send_int (&ctx->send_chan, job_id);
  ijs_send_int (&ctx->send_chan, key_size + 1 + value_size);
  status = ijs_send_block (&ctx->send_chan, key, key_size+1);
  if (status)
    return status;
  status = ijs_send_block (&ctx->send_chan, value, value_size);
  if (status)
    return status;
  status = ijs_client_send_cmd (ctx);
  if (status)
    return status;
  status = ijs_recv_ack (&ctx->recv_chan);
  return status;
}

/**
 * Return value: data block size if nonnegative, or error code if
 * negative.
 **/
int
ijs_client_get_param (IjsClientCtx *ctx, IjsJobId job_id,
		      const char *key,
		      char *value, int value_size)
{
  int key_size = strlen (key);
  int status;

  ijs_client_begin_cmd (ctx, IJS_CMD_GET_PARAM);
  ijs_send_int (&ctx->send_chan, job_id);
  status = ijs_send_block (&ctx->send_chan, key, key_size + 1);
  if (status < 0)
    return IJS_EIO;
  status = ijs_client_send_cmd (ctx);
  if (status)
    return status;
  status = ijs_recv_ack (&ctx->recv_chan);
  if (status)
    return status;
  status = ijs_recv_block (&ctx->recv_chan, value, value_size);
  return status;
}

int
ijs_client_begin_page (IjsClientCtx *ctx, IjsJobId job_id)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_BEGIN_PAGE);
  ijs_send_int (&ctx->send_chan, job_id);
  return ijs_client_send_cmd_wait (ctx);
}

int
ijs_client_end_page (IjsClientCtx *ctx, IjsJobId job_id)
{
  ijs_client_begin_cmd (ctx, IJS_CMD_END_PAGE);
  ijs_send_int (&ctx->send_chan, job_id);
  return ijs_client_send_cmd_wait (ctx);
}

int
ijs_client_get_version (IjsClientCtx *ctx)
{
  return ctx->version;
}