2 * This file is part of DisOrder.
3 * Copyright (C) 2006 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/eclient.c
21 * @brief Client code for event-driven programs
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
43 #include "configuration.h"
50 #include "inputline.h"
57 #include "client-common.h"
59 /* TODO: more commands */
61 /* Types *********************************************************************/
63 /** @brief Client state */
65 state_disconnected
, /**< @brief not connected */
66 state_connecting
, /**< @brief waiting for connect() */
67 state_connected
, /**< @brief connected but not authenticated */
68 state_idle
, /**< @brief not doing anything */
69 state_cmdresponse
, /**< @brief waiting for command resonse */
70 state_body
, /**< @brief accumulating body */
71 state_log
, /**< @brief monitoring log */
74 /** @brief Names for @ref client_state */
75 static const char *const states
[] = {
85 struct operation
; /* forward decl */
87 /** @brief Type of an operation callback */
88 typedef void operation_callback(disorder_eclient
*c
, struct operation
*op
);
90 /** @brief A pending operation.
92 * This can be either a command or part of the authentication protocol. In the
93 * former case new commands are appended to the list, in the latter case they
94 * are inserted at the front. */
96 struct operation
*next
; /**< @brief next operation */
97 char *cmd
; /**< @brief command to send or 0 */
98 operation_callback
*opcallback
; /**< @brief internal completion callback */
99 void (*completed
)(); /**< @brief user completion callback or 0 */
100 void *v
; /**< @brief data for COMPLETED */
101 disorder_eclient
*client
; /**< @brief owning client */
103 /** @brief true if sent to server
105 * This is cleared by disorder_eclient_close(), forcing all queued
106 * commands to be transparently resent.
111 /** @brief Client structure */
112 struct disorder_eclient
{
114 int fd
; /**< @brief connection to server */
115 enum client_state state
; /**< @brief current state */
116 int authenticated
; /**< @brief true when authenicated */
117 struct dynstr output
; /**< @brief output buffer */
118 struct dynstr input
; /**< @brief input buffer */
119 int eof
; /**< @brief input buffer is at EOF */
120 const disorder_eclient_callbacks
*callbacks
; /**< @brief error callbacks */
121 void *u
; /**< @brief user data */
122 struct operation
*ops
; /**< @brief queue of operations */
123 struct operation
**opstail
; /**< @brief queue tail */
124 /* accumulated response */
125 int rc
; /**< @brief response code */
126 char *line
; /**< @brief complete line */
127 struct vector vec
; /**< @brief body */
128 const disorder_eclient_log_callbacks
*log_callbacks
; /**< @brief log callbacks */
129 void *log_v
; /**< @brief user data */
130 unsigned long statebits
; /**< @brief current state */
133 /* Forward declarations ******************************************************/
135 static int start_connect(void *cc
,
136 const struct sockaddr
*sa
,
139 static void process_line(disorder_eclient
*c
, char *line
);
140 static int start_connect(void *cc
,
141 const struct sockaddr
*sa
,
144 static void maybe_connected(disorder_eclient
*c
);
145 static void authbanner_opcallback(disorder_eclient
*c
,
146 struct operation
*op
);
147 static void authuser_opcallback(disorder_eclient
*c
,
148 struct operation
*op
);
149 static void complete(disorder_eclient
*c
);
150 static void send_output(disorder_eclient
*c
);
151 static void put(disorder_eclient
*c
, const char *s
, size_t n
);
152 static void read_input(disorder_eclient
*c
);
153 static void stash_command(disorder_eclient
*c
,
155 operation_callback
*opcallback
,
160 static void log_opcallback(disorder_eclient
*c
, struct operation
*op
);
161 static void logline(disorder_eclient
*c
, const char *line
);
162 static void logentry_completed(disorder_eclient
*c
, int nvec
, char **vec
);
163 static void logentry_failed(disorder_eclient
*c
, int nvec
, char **vec
);
164 static void logentry_moved(disorder_eclient
*c
, int nvec
, char **vec
);
165 static void logentry_playing(disorder_eclient
*c
, int nvec
, char **vec
);
166 static void logentry_queue(disorder_eclient
*c
, int nvec
, char **vec
);
167 static void logentry_recent_added(disorder_eclient
*c
, int nvec
, char **vec
);
168 static void logentry_recent_removed(disorder_eclient
*c
, int nvec
, char **vec
);
169 static void logentry_removed(disorder_eclient
*c
, int nvec
, char **vec
);
170 static void logentry_scratched(disorder_eclient
*c
, int nvec
, char **vec
);
171 static void logentry_state(disorder_eclient
*c
, int nvec
, char **vec
);
172 static void logentry_volume(disorder_eclient
*c
, int nvec
, char **vec
);
174 /* Tables ********************************************************************/
176 /** @brief One possible log entry */
177 struct logentry_handler
{
178 const char *name
; /**< @brief Entry name */
179 int min
; /**< @brief Minimum arguments */
180 int max
; /**< @brief Maximum arguments */
181 void (*handler
)(disorder_eclient
*c
,
183 char **vec
); /**< @brief Handler function */
186 /** @brief Table for parsing log entries */
187 static const struct logentry_handler logentry_handlers
[] = {
188 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
193 LE(queue
, 2, INT_MAX
),
194 LE(recent_added
, 2, INT_MAX
),
195 LE(recent_removed
, 1, 1),
202 /* Setup and teardown ********************************************************/
204 /** @brief Create a new client
206 * Does NOT connect the client - connections are made (and re-made) on demand.
208 disorder_eclient
*disorder_eclient_new(const disorder_eclient_callbacks
*cb
,
210 disorder_eclient
*c
= xmalloc(sizeof *c
);
211 D(("disorder_eclient_new"));
215 c
->opstail
= &c
->ops
;
216 vector_init(&c
->vec
);
217 dynstr_init(&c
->input
);
218 dynstr_init(&c
->output
);
219 if(!config
->password
) {
220 error(0, "no password set");
226 /** @brief Disconnect a client
227 * @param c Client to disconnect
229 * NB that this routine just disconnnects the TCP connection. It does not
230 * destroy the client! If you continue to use it then it will attempt to
233 void disorder_eclient_close(disorder_eclient
*c
) {
234 struct operation
*op
;
236 D(("disorder_eclient_close"));
238 D(("disorder_eclient_close closing fd %d", c
->fd
));
239 c
->callbacks
->poll(c
->u
, c
, c
->fd
, 0);
242 c
->state
= state_disconnected
;
247 c
->authenticated
= 0;
248 /* We'll need to resend all operations */
249 for(op
= c
->ops
; op
; op
= op
->next
)
253 /** @brief Return true if @c c is connected
255 * By connected it is meant that commands have a reasonable chance of being
256 * processed soon, not merely that a TCP connection exists - for instance if
257 * the client is still authenticating then that does not count as connected.
259 int disorder_eclient_connected(const disorder_eclient
*c
) {
261 case state_disconnected
:
262 case state_connecting
:
263 case state_connected
:
266 case state_cmdresponse
:
274 /* Error reporting ***********************************************************/
276 /** @brief called when a connection error occurs
278 * After this called we will be disconnected (by disorder_eclient_close()),
279 * so there will be a reconnection before any commands can be sent.
281 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
287 byte_xvasprintf(&s
, fmt
, ap
);
289 disorder_eclient_close(c
);
290 c
->callbacks
->comms_error(c
->u
, s
);
294 /** @brief called when the server reports an error */
295 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
296 int code
, const char *fmt
, ...) {
300 D(("protocol_error"));
302 byte_xvasprintf(&s
, fmt
, ap
);
304 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
308 /* State machine *************************************************************/
310 /** @brief Called when there's something to do
312 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
314 * This should be called from by your code when the file descriptor is readable
315 * or writable (as requested by the @c poll callback, see @ref
316 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
317 * = 0) to allow for retries to work.
319 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
320 struct operation
*op
;
322 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
323 c
->fd
, states
[c
->state
],
324 mode
& DISORDER_POLL_READ ?
"READ" : "",
325 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
326 /* The pattern here is to check each possible state in turn and try to
327 * advance (though on error we might go back). If we advance we leave open
328 * the possibility of falling through to the next state, but we set the mode
329 * bits to 0, to avoid false positives (which matter more in some cases than
332 if(c
->state
== state_disconnected
) {
333 D(("state_disconnected"));
334 with_sockaddr(c
, start_connect
);
335 /* might now be state_disconnected (on error), state_connecting (slow
336 * connect) or state_connected (fast connect). If state_disconnected then
337 * we just rely on a periodic callback from the event loop sometime. */
341 if(c
->state
== state_connecting
&& mode
) {
342 D(("state_connecting"));
344 /* Might be state_disconnected (on error) or state_connected (on success).
345 * In the former case we rely on the event loop for a periodic callback to
350 if(c
->state
== state_connected
) {
351 D(("state_connected"));
352 /* We just connected. Initiate the authentication protocol. */
353 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
354 0/*completed*/, 0/*v*/, 0/*cmd*/);
355 /* We never stay is state_connected very long. We could in principle jump
356 * straight to state_cmdresponse since there's actually no command to
357 * send, but that would arguably be cheating. */
358 c
->state
= state_idle
;
361 if(c
->state
== state_idle
) {
363 /* We are connected, and have finished any command we set off, look for
367 if(c
->authenticated
) {
368 /* Transmit all unsent operations */
369 for(op
= c
->ops
; op
; op
= op
->next
) {
371 put(c
, op
->cmd
, strlen(op
->cmd
));
376 /* Just send the head operation */
377 if(c
->ops
->cmd
&& !c
->ops
->sent
) {
378 put(c
, c
->ops
->cmd
, strlen(c
->ops
->cmd
));
382 /* Awaiting response for the operation at the head of the list */
383 c
->state
= state_cmdresponse
;
386 c
->callbacks
->report(c
->u
, 0);
389 if(c
->state
== state_cmdresponse
390 || c
->state
== state_body
391 || c
->state
== state_log
) {
392 D(("state_%s", states
[c
->state
]));
393 /* We are awaiting a response */
394 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
395 if(mode
& DISORDER_POLL_READ
) read_input(c
);
396 /* There are a couple of reasons we might want to re-enter the state
397 * machine from the top. state_idle is obvious: there may be further
398 * commands to process. Re-entering on state_disconnected means that we
399 * immediately retry connection if a comms error occurs during a command.
400 * This is different to the case where a connection fails, where we await a
401 * spontaneous call to initiate the retry. */
403 case state_disconnected
: /* lost connection */
404 case state_idle
: /* completed a command */
406 disorder_eclient_polled(c
, 0);
413 /* Figure out what to set the mode to */
415 case state_disconnected
:
416 D(("state_disconnected (2)"));
417 /* Probably an error occurred. Await a retry. */
420 case state_connecting
:
421 D(("state_connecting (2)"));
422 /* Waiting for connect to complete */
423 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
425 case state_connected
:
426 D(("state_connected (2)"));
427 assert(!"should never be in state_connected here");
430 D(("state_idle (2)"));
431 /* Connected but nothing to do. */
434 case state_cmdresponse
:
437 D(("state_%s (2)", states
[c
->state
]));
438 /* Gathering a response. Wait for input. */
439 mode
= DISORDER_POLL_READ
;
440 /* Flush any pending output. */
441 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
444 D(("fd=%d new mode [%s %s]",
446 mode
& DISORDER_POLL_READ ?
"READ" : "",
447 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
448 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
451 /** @brief Called to start connecting */
452 static int start_connect(void *cc
,
453 const struct sockaddr
*sa
,
456 disorder_eclient
*c
= cc
;
458 D(("start_connect"));
459 c
->ident
= xstrdup(ident
);
464 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
465 return comms_error(c
, "socket: %s", strerror(errno
));
468 if(connect(c
->fd
, sa
, len
) < 0) {
472 c
->state
= state_connecting
;
473 /* We are called from _polled so the state machine will get to do its
477 /* Signal the error to the caller. */
478 return comms_error(c
, "connecting to %s: %s", ident
, strerror(errno
));
481 c
->state
= state_connected
;
485 /** @brief Called when poll triggers while waiting for a connection */
486 static void maybe_connected(disorder_eclient
*c
) {
487 /* We either connected, or got an error. */
489 socklen_t len
= sizeof err
;
491 D(("maybe_connected"));
492 /* Work around over-enthusiastic error slippage */
493 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
496 /* The connection failed */
497 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
498 /* sets state_disconnected */
500 /* The connection succeeded */
501 c
->state
= state_connected
;
505 /* Authentication ************************************************************/
507 static void authbanner_opcallback(disorder_eclient
*c
,
508 struct operation
*op
) {
510 const unsigned char *nonce
;
513 D(("authbanner_opcallback"));
514 if(c
->rc
/ 100 != 2) {
515 /* Banner told us to go away. We cannot proceed. */
516 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
517 disorder_eclient_close(c
);
520 nonce
= unhex(c
->line
+ 4, &nonce_len
);
521 res
= authhash(nonce
, nonce_len
, config
->password
);
522 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
523 "user", quoteutf8(config
->username
), quoteutf8(res
),
527 static void authuser_opcallback(disorder_eclient
*c
,
528 struct operation
*op
) {
529 D(("authuser_opcallback"));
530 if(c
->rc
/ 100 != 2) {
531 /* Wrong password or something. We cannot proceed. */
532 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
533 disorder_eclient_close(c
);
536 /* OK, we're authenticated now. */
537 c
->authenticated
= 1;
538 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
539 /* We are a log client, switch to logging mode */
540 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
544 /* Output ********************************************************************/
546 /* Chop N bytes off the front of a dynstr */
547 static void consume(struct dynstr
*d
, int n
) {
548 D(("consume %d", n
));
549 assert(d
->nvec
>= n
);
550 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
554 /* Write some bytes */
555 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
556 D(("put %d %.*s", c
->fd
, (int)n
, s
));
557 dynstr_append_bytes(&c
->output
, s
, n
);
560 /* Called when we can write to our FD, or at any other time */
561 static void send_output(disorder_eclient
*c
) {
564 D(("send_output %d bytes pending", c
->output
.nvec
));
565 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
566 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
573 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
577 consume(&c
->output
, n
);
581 /* Input *********************************************************************/
583 /* Called when c->fd might be readable, or at any other time */
584 static void read_input(disorder_eclient
*c
) {
589 D(("read_input in state %s", states
[c
->state
]));
590 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
591 /* read some more input */
592 n
= read(c
->fd
, buffer
, sizeof buffer
);
599 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
602 return; /* no new input to process */
604 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
605 dynstr_append_bytes(&c
->input
, buffer
, n
);
608 /* might have more than one line to process */
609 while(c
->state
> state_connecting
610 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
611 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
612 /* we might have disconnected along the way, which zogs the input buffer */
613 if(c
->state
> state_connecting
)
614 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
617 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
618 c
->authenticated
= 0;
622 /* called with a line that has just been read */
623 static void process_line(disorder_eclient
*c
, char *line
) {
624 D(("process_line %d [%s]", c
->fd
, line
));
626 case state_cmdresponse
:
627 /* This is the first line of a response */
628 if(!(line
[0] >= '0' && line
[0] <= '9'
629 && line
[1] >= '0' && line
[1] <= '9'
630 && line
[2] >= '0' && line
[2] <= '9'
632 fatal(0, "invalid response from server: %s", line
);
633 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
637 /* We need to collect the body. */
638 c
->state
= state_body
;
642 assert(c
->log_callbacks
!= 0);
643 if(c
->log_callbacks
->connected
)
644 c
->log_callbacks
->connected(c
->log_v
);
645 c
->state
= state_log
;
648 /* We've got the whole response. Go into the idle state so the state
649 * machine knows we're done and then call the operation callback. */
655 if(strcmp(line
, ".")) {
656 /* A line from the body */
657 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
659 /* End of the body. */
660 vector_terminate(&c
->vec
);
665 if(strcmp(line
, ".")) {
666 logline(c
, line
+ (line
[0] == '.'));
671 assert(!"wrong state for location");
676 /* Called when an operation completes */
677 static void complete(disorder_eclient
*c
) {
678 struct operation
*op
;
681 /* Pop the operation off the queue */
684 if(c
->opstail
== &op
->next
)
685 c
->opstail
= &c
->ops
;
686 /* If we've pipelined a command ahead then we go straight to cmdresponser.
687 * Otherwise we go to idle, which will arrange further sends. */
688 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
689 op
->opcallback(c
, op
);
690 /* Note that we always call the opcallback even on error, though command
691 * opcallbacks generally always do the same error handling, i.e. just call
692 * protocol_error(). It's the auth* opcallbacks that have different
696 /* Operation setup ***********************************************************/
698 static void stash_command_vector(disorder_eclient
*c
,
700 operation_callback
*opcallback
,
705 struct operation
*op
= xmalloc(sizeof *op
);
711 for(n
= 0; n
< ncmd
; ++n
) {
713 dynstr_append(&d
, ' ');
714 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
716 dynstr_append(&d
, '\n');
717 dynstr_terminate(&d
);
720 op
->cmd
= 0; /* usually, awaiting challenge */
721 op
->opcallback
= opcallback
;
722 op
->completed
= completed
;
726 assert(op
->sent
== 0);
728 /* Authentication operations jump the queue of useful commands */
731 if(c
->opstail
== &c
->ops
)
732 c
->opstail
= &op
->next
;
733 for(op
= c
->ops
; op
; op
= op
->next
)
737 c
->opstail
= &op
->next
;
741 static void vstash_command(disorder_eclient
*c
,
743 operation_callback
*opcallback
,
746 const char *cmd
, va_list ap
) {
750 D(("vstash_command %s", cmd ? cmd
: "NULL"));
753 vector_append(&vec
, (char *)cmd
);
754 while((arg
= va_arg(ap
, char *)))
755 vector_append(&vec
, arg
);
756 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
759 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
, 0, 0);
762 static void stash_command(disorder_eclient
*c
,
764 operation_callback
*opcallback
,
772 vstash_command(c
, queuejump
, opcallback
, completed
, v
, cmd
, ap
);
776 /* Command support ***********************************************************/
778 /* for commands with a simple string response */
779 static void string_response_opcallback(disorder_eclient
*c
,
780 struct operation
*op
) {
781 D(("string_response_callback"));
782 if(c
->rc
/ 100 == 2) {
784 ((disorder_eclient_string_response
*)op
->completed
)(op
->v
, c
->line
+ 4);
786 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
789 /* for commands with a simple integer response */
790 static void integer_response_opcallback(disorder_eclient
*c
,
791 struct operation
*op
) {
792 D(("string_response_callback"));
793 if(c
->rc
/ 100 == 2) {
795 ((disorder_eclient_integer_response
*)op
->completed
)
796 (op
->v
, strtol(c
->line
+ 4, 0, 10));
798 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
801 /* for commands with no response */
802 static void no_response_opcallback(disorder_eclient
*c
,
803 struct operation
*op
) {
804 D(("no_response_callback"));
805 if(c
->rc
/ 100 == 2) {
807 ((disorder_eclient_no_response
*)op
->completed
)(op
->v
);
809 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
812 /* error callback for queue_unmarshall */
813 static void eclient_queue_error(const char *msg
,
815 struct operation
*op
= u
;
817 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
820 /* for commands that expect a queue dump */
821 static void queue_response_opcallback(disorder_eclient
*c
,
822 struct operation
*op
) {
824 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
826 D(("queue_response_callback"));
827 if(c
->rc
/ 100 == 2) {
828 /* parse the queue */
829 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
830 q
= xmalloc(sizeof *q
);
831 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
832 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], eclient_queue_error
, op
)) {
840 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, qh
);
842 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
846 static void playing_response_opcallback(disorder_eclient
*c
,
847 struct operation
*op
) {
848 struct queue_entry
*q
;
850 D(("playing_response_callback"));
851 if(c
->rc
/ 100 == 2) {
854 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
855 eclient_queue_error
, c
))
862 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
866 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, q
);
868 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
871 /* for commands that expect a list of some sort */
872 static void list_response_opcallback(disorder_eclient
*c
,
873 struct operation
*op
) {
874 D(("list_response_callback"));
875 if(c
->rc
/ 100 == 2) {
877 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
,
881 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
885 static void volume_response_opcallback(disorder_eclient
*c
,
886 struct operation
*op
) {
889 D(("volume_response_callback"));
890 if(c
->rc
/ 100 == 2) {
892 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
893 protocol_error(c
, op
, -1, "%s: invalid volume response: %s",
896 ((disorder_eclient_volume_response
*)op
->completed
)(op
->v
, l
, r
);
899 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
902 static int simple(disorder_eclient
*c
,
903 operation_callback
*opcallback
,
906 const char *cmd
, ...) {
910 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, cmd
, ap
);
912 /* Give the state machine a kick, since we might be in state_idle */
913 disorder_eclient_polled(c
, 0);
917 /* Commands ******************************************************************/
919 int disorder_eclient_version(disorder_eclient
*c
,
920 disorder_eclient_string_response
*completed
,
922 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
923 "version", (char *)0);
926 int disorder_eclient_namepart(disorder_eclient
*c
,
927 disorder_eclient_string_response
*completed
,
932 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
933 "part", track
, context
, part
, (char *)0);
936 int disorder_eclient_play(disorder_eclient
*c
,
938 disorder_eclient_no_response
*completed
,
940 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
941 "play", track
, (char *)0);
944 int disorder_eclient_pause(disorder_eclient
*c
,
945 disorder_eclient_no_response
*completed
,
947 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
951 int disorder_eclient_resume(disorder_eclient
*c
,
952 disorder_eclient_no_response
*completed
,
954 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
955 "resume", (char *)0);
958 int disorder_eclient_scratch(disorder_eclient
*c
,
960 disorder_eclient_no_response
*completed
,
962 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
963 "scratch", id
, (char *)0);
966 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
967 disorder_eclient_no_response
*completed
,
969 return disorder_eclient_scratch(c
, 0, completed
, v
);
972 int disorder_eclient_remove(disorder_eclient
*c
,
974 disorder_eclient_no_response
*completed
,
976 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
977 "remove", id
, (char *)0);
980 int disorder_eclient_moveafter(disorder_eclient
*c
,
984 disorder_eclient_no_response
*completed
,
990 vector_append(&vec
, (char *)"moveafter");
991 vector_append(&vec
, (char *)target
);
992 for(n
= 0; n
< nids
; ++n
)
993 vector_append(&vec
, (char *)ids
[n
]);
994 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
996 disorder_eclient_polled(c
, 0);
1000 int disorder_eclient_recent(disorder_eclient
*c
,
1001 disorder_eclient_queue_response
*completed
,
1003 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1004 "recent", (char *)0);
1007 int disorder_eclient_queue(disorder_eclient
*c
,
1008 disorder_eclient_queue_response
*completed
,
1010 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1011 "queue", (char *)0);
1014 int disorder_eclient_files(disorder_eclient
*c
,
1015 disorder_eclient_list_response
*completed
,
1019 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1020 "files", dir
, re
, (char *)0);
1023 int disorder_eclient_dirs(disorder_eclient
*c
,
1024 disorder_eclient_list_response
*completed
,
1028 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1029 "dirs", dir
, re
, (char *)0);
1032 int disorder_eclient_playing(disorder_eclient
*c
,
1033 disorder_eclient_queue_response
*completed
,
1035 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1036 "playing", (char *)0);
1039 int disorder_eclient_length(disorder_eclient
*c
,
1040 disorder_eclient_integer_response
*completed
,
1043 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1044 "length", track
, (char *)0);
1047 int disorder_eclient_volume(disorder_eclient
*c
,
1048 disorder_eclient_volume_response
*completed
,
1051 char sl
[64], sr
[64];
1053 if(l
< 0 && r
< 0) {
1054 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1055 "volume", (char *)0);
1056 } else if(l
>= 0 && r
>= 0) {
1059 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1060 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1061 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1062 "volume", sl
, sr
, (char *)0);
1064 assert(!"invalid arguments to disorder_eclient_volume");
1065 return -1; /* gcc is being dim */
1069 int disorder_eclient_enable(disorder_eclient
*c
,
1070 disorder_eclient_no_response
*completed
,
1072 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1073 "enable", (char *)0);
1076 int disorder_eclient_disable(disorder_eclient
*c
,
1077 disorder_eclient_no_response
*completed
,
1079 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1080 "disable", (char *)0);
1083 int disorder_eclient_random_enable(disorder_eclient
*c
,
1084 disorder_eclient_no_response
*completed
,
1086 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1087 "random-enable", (char *)0);
1090 int disorder_eclient_random_disable(disorder_eclient
*c
,
1091 disorder_eclient_no_response
*completed
,
1093 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1094 "random-disable", (char *)0);
1097 int disorder_eclient_get(disorder_eclient
*c
,
1098 disorder_eclient_string_response
*completed
,
1099 const char *track
, const char *pref
,
1101 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1102 "get", track
, pref
, (char *)0);
1105 int disorder_eclient_set(disorder_eclient
*c
,
1106 disorder_eclient_no_response
*completed
,
1107 const char *track
, const char *pref
,
1110 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1111 "set", track
, pref
, value
, (char *)0);
1114 int disorder_eclient_unset(disorder_eclient
*c
,
1115 disorder_eclient_no_response
*completed
,
1116 const char *track
, const char *pref
,
1118 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1119 "unset", track
, pref
, (char *)0);
1122 int disorder_eclient_resolve(disorder_eclient
*c
,
1123 disorder_eclient_string_response
*completed
,
1126 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1127 "resolve", track
, (char *)0);
1130 int disorder_eclient_search(disorder_eclient
*c
,
1131 disorder_eclient_list_response
*completed
,
1134 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1135 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1136 "search", terms
, (char *)0);
1139 int disorder_eclient_nop(disorder_eclient
*c
,
1140 disorder_eclient_no_response
*completed
,
1142 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1146 /* Log clients ***************************************************************/
1148 /** @brief Monitor the server log
1150 * @param callbacks Functions to call when anything happens
1151 * @param v Passed to @p callbacks functions
1153 * Once a client is being used for logging it cannot be used for anything else.
1154 * There is magic in authuser_opcallback() to re-submit the @c log command
1155 * after reconnection.
1157 int disorder_eclient_log(disorder_eclient
*c
,
1158 const disorder_eclient_log_callbacks
*callbacks
,
1160 if(c
->log_callbacks
) return -1;
1161 c
->log_callbacks
= callbacks
;
1163 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1168 /* If we get here we've stopped being a log client */
1169 static void log_opcallback(disorder_eclient
*c
,
1170 struct operation
attribute((unused
)) *op
) {
1171 D(("log_opcallback"));
1172 c
->log_callbacks
= 0;
1176 /* error callback for log line parsing */
1177 static void logline_error(const char *msg
, void *u
) {
1178 disorder_eclient
*c
= u
;
1179 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1182 /* process a single log line */
1183 static void logline(disorder_eclient
*c
, const char *line
) {
1188 D(("log_opcallback [%s]", line
));
1189 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1190 if(nvec
< 2) return; /* probably an error, already
1192 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1193 /* probably the wrong side of a format change */
1194 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1197 /* TODO: do something with the time */
1198 n
= TABLE_FIND(logentry_handlers
, struct logentry_handler
, name
, vec
[1]);
1199 if(n
< 0) return; /* probably a future command */
1202 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
)
1204 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1207 static void logentry_completed(disorder_eclient
*c
,
1208 int attribute((unused
)) nvec
, char **vec
) {
1209 if(!c
->log_callbacks
->completed
) return;
1210 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1213 static void logentry_failed(disorder_eclient
*c
,
1214 int attribute((unused
)) nvec
, char **vec
) {
1215 if(!c
->log_callbacks
->failed
)return;
1216 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1219 static void logentry_moved(disorder_eclient
*c
,
1220 int attribute((unused
)) nvec
, char **vec
) {
1221 if(!c
->log_callbacks
->moved
) return;
1222 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1225 static void logentry_playing(disorder_eclient
*c
,
1226 int attribute((unused
)) nvec
, char **vec
) {
1227 if(!c
->log_callbacks
->playing
) return;
1228 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1231 static void logentry_queue(disorder_eclient
*c
,
1232 int attribute((unused
)) nvec
, char **vec
) {
1233 struct queue_entry
*q
;
1235 if(!c
->log_callbacks
->completed
) return;
1236 q
= xmalloc(sizeof *q
);
1237 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1239 c
->log_callbacks
->queue(c
->log_v
, q
);
1242 static void logentry_recent_added(disorder_eclient
*c
,
1243 int attribute((unused
)) nvec
, char **vec
) {
1244 struct queue_entry
*q
;
1246 if(!c
->log_callbacks
->recent_added
) return;
1247 q
= xmalloc(sizeof *q
);
1248 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1250 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1253 static void logentry_recent_removed(disorder_eclient
*c
,
1254 int attribute((unused
)) nvec
, char **vec
) {
1255 if(!c
->log_callbacks
->recent_removed
) return;
1256 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1259 static void logentry_removed(disorder_eclient
*c
,
1260 int attribute((unused
)) nvec
, char **vec
) {
1261 if(!c
->log_callbacks
->removed
) return;
1262 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1265 static void logentry_scratched(disorder_eclient
*c
,
1266 int attribute((unused
)) nvec
, char **vec
) {
1267 if(!c
->log_callbacks
->scratched
) return;
1268 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1271 static const struct {
1274 const char *disable
;
1275 } statestrings
[] = {
1276 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1277 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1278 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1280 #define NSTATES (int)(sizeof states / sizeof *states)
1282 static void logentry_state(disorder_eclient
*c
,
1283 int attribute((unused
)) nvec
, char **vec
) {
1286 for(n
= 0; n
< NSTATES
; ++n
)
1287 if(!strcmp(vec
[0], statestrings
[n
].enable
)) {
1288 c
->statebits
|= statestrings
[n
].bit
;
1290 } else if(!strcmp(vec
[0], statestrings
[n
].disable
)) {
1291 c
->statebits
&= ~statestrings
[n
].bit
;
1294 if(!c
->log_callbacks
->state
) return;
1295 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1298 static void logentry_volume(disorder_eclient
*c
,
1299 int attribute((unused
)) nvec
, char **vec
) {
1302 if(!c
->log_callbacks
->volume
) return;
1303 if(xstrtol(&l
, vec
[0], 0, 10)
1304 || xstrtol(&r
, vec
[1], 0, 10)
1305 || l
< 0 || l
> INT_MAX
1306 || r
< 0 || r
> INT_MAX
)
1308 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1316 indent-tabs-mode:nil