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 latest 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
;
248 c
->authenticated
= 0;
249 /* We'll need to resend all operations */
250 for(op
= c
->ops
; op
; op
= op
->next
)
252 /* Drop our use a hint that we're disconnected */
253 if(c
->log_callbacks
&& c
->log_callbacks
->state
)
254 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
257 /** @brief Return current state */
258 unsigned long disorder_eclient_state(const disorder_eclient
*c
) {
259 return c
->statebits
| (c
->state
> state_connected ? DISORDER_CONNECTED
: 0);
262 /* Error reporting ***********************************************************/
264 /** @brief called when a connection error occurs
266 * After this called we will be disconnected (by disorder_eclient_close()),
267 * so there will be a reconnection before any commands can be sent.
269 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
275 byte_xvasprintf(&s
, fmt
, ap
);
277 disorder_eclient_close(c
);
278 c
->callbacks
->comms_error(c
->u
, s
);
282 /** @brief called when the server reports an error */
283 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
284 int code
, const char *fmt
, ...) {
288 D(("protocol_error"));
290 byte_xvasprintf(&s
, fmt
, ap
);
292 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
296 /* State machine *************************************************************/
298 /** @brief Called when there's something to do
300 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
302 * This should be called from by your code when the file descriptor is readable
303 * or writable (as requested by the @c poll callback, see @ref
304 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
305 * = 0) to allow for retries to work.
307 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
308 struct operation
*op
;
310 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
311 c
->fd
, states
[c
->state
],
312 mode
& DISORDER_POLL_READ ?
"READ" : "",
313 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
314 /* The pattern here is to check each possible state in turn and try to
315 * advance (though on error we might go back). If we advance we leave open
316 * the possibility of falling through to the next state, but we set the mode
317 * bits to 0, to avoid false positives (which matter more in some cases than
320 if(c
->state
== state_disconnected
) {
321 D(("state_disconnected"));
322 with_sockaddr(c
, start_connect
);
323 /* might now be state_disconnected (on error), state_connecting (slow
324 * connect) or state_connected (fast connect). If state_disconnected then
325 * we just rely on a periodic callback from the event loop sometime. */
329 if(c
->state
== state_connecting
&& mode
) {
330 D(("state_connecting"));
332 /* Might be state_disconnected (on error) or state_connected (on success).
333 * In the former case we rely on the event loop for a periodic callback to
338 if(c
->state
== state_connected
) {
339 D(("state_connected"));
340 /* We just connected. Initiate the authentication protocol. */
341 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
342 0/*completed*/, 0/*v*/, 0/*cmd*/);
343 /* We never stay is state_connected very long. We could in principle jump
344 * straight to state_cmdresponse since there's actually no command to
345 * send, but that would arguably be cheating. */
346 c
->state
= state_idle
;
349 if(c
->state
== state_idle
) {
351 /* We are connected, and have finished any command we set off, look for
355 if(c
->authenticated
) {
356 /* Transmit all unsent operations */
357 for(op
= c
->ops
; op
; op
= op
->next
) {
359 put(c
, op
->cmd
, strlen(op
->cmd
));
364 /* Just send the head operation */
365 if(c
->ops
->cmd
&& !c
->ops
->sent
) {
366 put(c
, c
->ops
->cmd
, strlen(c
->ops
->cmd
));
370 /* Awaiting response for the operation at the head of the list */
371 c
->state
= state_cmdresponse
;
374 c
->callbacks
->report(c
->u
, 0);
377 if(c
->state
== state_cmdresponse
378 || c
->state
== state_body
379 || c
->state
== state_log
) {
380 D(("state_%s", states
[c
->state
]));
381 /* We are awaiting a response */
382 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
383 if(mode
& DISORDER_POLL_READ
) read_input(c
);
384 /* There are a couple of reasons we might want to re-enter the state
385 * machine from the top. state_idle is obvious: there may be further
386 * commands to process. Re-entering on state_disconnected means that we
387 * immediately retry connection if a comms error occurs during a command.
388 * This is different to the case where a connection fails, where we await a
389 * spontaneous call to initiate the retry. */
391 case state_disconnected
: /* lost connection */
392 case state_idle
: /* completed a command */
394 disorder_eclient_polled(c
, 0);
401 /* Figure out what to set the mode to */
403 case state_disconnected
:
404 D(("state_disconnected (2)"));
405 /* Probably an error occurred. Await a retry. */
408 case state_connecting
:
409 D(("state_connecting (2)"));
410 /* Waiting for connect to complete */
411 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
413 case state_connected
:
414 D(("state_connected (2)"));
415 assert(!"should never be in state_connected here");
418 D(("state_idle (2)"));
419 /* Connected but nothing to do. */
422 case state_cmdresponse
:
425 D(("state_%s (2)", states
[c
->state
]));
426 /* Gathering a response. Wait for input. */
427 mode
= DISORDER_POLL_READ
;
428 /* Flush any pending output. */
429 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
432 D(("fd=%d new mode [%s %s]",
434 mode
& DISORDER_POLL_READ ?
"READ" : "",
435 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
436 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
439 /** @brief Called to start connecting */
440 static int start_connect(void *cc
,
441 const struct sockaddr
*sa
,
444 disorder_eclient
*c
= cc
;
446 D(("start_connect"));
447 c
->ident
= xstrdup(ident
);
452 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
453 return comms_error(c
, "socket: %s", strerror(errno
));
456 if(connect(c
->fd
, sa
, len
) < 0) {
460 c
->state
= state_connecting
;
461 /* We are called from _polled so the state machine will get to do its
465 /* Signal the error to the caller. */
466 return comms_error(c
, "connecting to %s: %s", ident
, strerror(errno
));
469 c
->state
= state_connected
;
473 /** @brief Called when poll triggers while waiting for a connection */
474 static void maybe_connected(disorder_eclient
*c
) {
475 /* We either connected, or got an error. */
477 socklen_t len
= sizeof err
;
479 D(("maybe_connected"));
480 /* Work around over-enthusiastic error slippage */
481 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
484 /* The connection failed */
485 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
486 /* sets state_disconnected */
490 /* The connection succeeded */
491 c
->state
= state_connected
;
492 byte_xasprintf(&r
, "connected to %s", c
->ident
);
493 c
->callbacks
->report(c
->u
, r
);
494 /* If this is a log client we expect to get a bunch of updates from the
495 * server straight away */
499 /* Authentication ************************************************************/
501 static void authbanner_opcallback(disorder_eclient
*c
,
502 struct operation
*op
) {
504 const unsigned char *nonce
;
507 D(("authbanner_opcallback"));
508 if(c
->rc
/ 100 != 2) {
509 /* Banner told us to go away. We cannot proceed. */
510 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
511 disorder_eclient_close(c
);
514 nonce
= unhex(c
->line
+ 4, &nonce_len
);
515 res
= authhash(nonce
, nonce_len
, config
->password
);
516 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
517 "user", quoteutf8(config
->username
), quoteutf8(res
),
521 static void authuser_opcallback(disorder_eclient
*c
,
522 struct operation
*op
) {
525 D(("authuser_opcallback"));
526 if(c
->rc
/ 100 != 2) {
527 /* Wrong password or something. We cannot proceed. */
528 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
529 disorder_eclient_close(c
);
532 /* OK, we're authenticated now. */
533 c
->authenticated
= 1;
534 byte_xasprintf(&r
, "authenticated with %s", c
->ident
);
535 c
->callbacks
->report(c
->u
, r
);
536 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
537 /* We are a log client, switch to logging mode */
538 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
542 /* Output ********************************************************************/
544 /* Chop N bytes off the front of a dynstr */
545 static void consume(struct dynstr
*d
, int n
) {
546 D(("consume %d", n
));
547 assert(d
->nvec
>= n
);
548 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
552 /* Write some bytes */
553 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
554 D(("put %d %.*s", c
->fd
, (int)n
, s
));
555 dynstr_append_bytes(&c
->output
, s
, n
);
558 /* Called when we can write to our FD, or at any other time */
559 static void send_output(disorder_eclient
*c
) {
562 D(("send_output %d bytes pending", c
->output
.nvec
));
563 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
564 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
571 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
575 consume(&c
->output
, n
);
579 /* Input *********************************************************************/
581 /* Called when c->fd might be readable, or at any other time */
582 static void read_input(disorder_eclient
*c
) {
587 D(("read_input in state %s", states
[c
->state
]));
588 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
589 /* read some more input */
590 n
= read(c
->fd
, buffer
, sizeof buffer
);
597 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
600 return; /* no new input to process */
602 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
603 dynstr_append_bytes(&c
->input
, buffer
, n
);
606 /* might have more than one line to process */
607 while(c
->state
> state_connecting
608 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
609 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
610 /* we might have disconnected along the way, which zogs the input buffer */
611 if(c
->state
> state_connecting
)
612 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
615 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
616 c
->authenticated
= 0;
620 /* called with a line that has just been read */
621 static void process_line(disorder_eclient
*c
, char *line
) {
622 D(("process_line %d [%s]", c
->fd
, line
));
624 case state_cmdresponse
:
625 /* This is the first line of a response */
626 if(!(line
[0] >= '0' && line
[0] <= '9'
627 && line
[1] >= '0' && line
[1] <= '9'
628 && line
[2] >= '0' && line
[2] <= '9'
630 fatal(0, "invalid response from server: %s", line
);
631 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
635 /* We need to collect the body. */
636 c
->state
= state_body
;
640 assert(c
->log_callbacks
!= 0);
641 if(c
->log_callbacks
->connected
)
642 c
->log_callbacks
->connected(c
->log_v
);
643 c
->state
= state_log
;
646 /* We've got the whole response. Go into the idle state so the state
647 * machine knows we're done and then call the operation callback. */
653 if(strcmp(line
, ".")) {
654 /* A line from the body */
655 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
657 /* End of the body. */
658 vector_terminate(&c
->vec
);
663 if(strcmp(line
, ".")) {
664 logline(c
, line
+ (line
[0] == '.'));
669 assert(!"wrong state for location");
674 /* Called when an operation completes */
675 static void complete(disorder_eclient
*c
) {
676 struct operation
*op
;
679 /* Pop the operation off the queue */
682 if(c
->opstail
== &op
->next
)
683 c
->opstail
= &c
->ops
;
684 /* If we've pipelined a command ahead then we go straight to cmdresponser.
685 * Otherwise we go to idle, which will arrange further sends. */
686 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
687 op
->opcallback(c
, op
);
688 /* Note that we always call the opcallback even on error, though command
689 * opcallbacks generally always do the same error handling, i.e. just call
690 * protocol_error(). It's the auth* opcallbacks that have different
694 /* Operation setup ***********************************************************/
696 static void stash_command_vector(disorder_eclient
*c
,
698 operation_callback
*opcallback
,
703 struct operation
*op
= xmalloc(sizeof *op
);
709 for(n
= 0; n
< ncmd
; ++n
) {
711 dynstr_append(&d
, ' ');
712 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
714 dynstr_append(&d
, '\n');
715 dynstr_terminate(&d
);
718 op
->cmd
= 0; /* usually, awaiting challenge */
719 op
->opcallback
= opcallback
;
720 op
->completed
= completed
;
724 assert(op
->sent
== 0);
726 /* Authentication operations jump the queue of useful commands */
729 if(c
->opstail
== &c
->ops
)
730 c
->opstail
= &op
->next
;
731 for(op
= c
->ops
; op
; op
= op
->next
)
735 c
->opstail
= &op
->next
;
739 static void vstash_command(disorder_eclient
*c
,
741 operation_callback
*opcallback
,
744 const char *cmd
, va_list ap
) {
748 D(("vstash_command %s", cmd ? cmd
: "NULL"));
751 vector_append(&vec
, (char *)cmd
);
752 while((arg
= va_arg(ap
, char *)))
753 vector_append(&vec
, arg
);
754 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
757 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
, 0, 0);
760 static void stash_command(disorder_eclient
*c
,
762 operation_callback
*opcallback
,
770 vstash_command(c
, queuejump
, opcallback
, completed
, v
, cmd
, ap
);
774 /* Command support ***********************************************************/
776 /* for commands with a simple string response */
777 static void string_response_opcallback(disorder_eclient
*c
,
778 struct operation
*op
) {
779 D(("string_response_callback"));
780 if(c
->rc
/ 100 == 2) {
782 ((disorder_eclient_string_response
*)op
->completed
)(op
->v
, c
->line
+ 4);
784 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
787 /* for commands with a simple integer response */
788 static void integer_response_opcallback(disorder_eclient
*c
,
789 struct operation
*op
) {
790 D(("string_response_callback"));
791 if(c
->rc
/ 100 == 2) {
793 ((disorder_eclient_integer_response
*)op
->completed
)
794 (op
->v
, strtol(c
->line
+ 4, 0, 10));
796 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
799 /* for commands with no response */
800 static void no_response_opcallback(disorder_eclient
*c
,
801 struct operation
*op
) {
802 D(("no_response_callback"));
803 if(c
->rc
/ 100 == 2) {
805 ((disorder_eclient_no_response
*)op
->completed
)(op
->v
);
807 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
810 /* error callback for queue_unmarshall */
811 static void eclient_queue_error(const char *msg
,
813 struct operation
*op
= u
;
815 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
818 /* for commands that expect a queue dump */
819 static void queue_response_opcallback(disorder_eclient
*c
,
820 struct operation
*op
) {
822 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
824 D(("queue_response_callback"));
825 if(c
->rc
/ 100 == 2) {
826 /* parse the queue */
827 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
828 q
= xmalloc(sizeof *q
);
829 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
830 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], eclient_queue_error
, op
)) {
838 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, qh
);
840 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
844 static void playing_response_opcallback(disorder_eclient
*c
,
845 struct operation
*op
) {
846 struct queue_entry
*q
;
848 D(("playing_response_callback"));
849 if(c
->rc
/ 100 == 2) {
852 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
853 eclient_queue_error
, c
))
860 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
864 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, q
);
866 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
869 /* for commands that expect a list of some sort */
870 static void list_response_opcallback(disorder_eclient
*c
,
871 struct operation
*op
) {
872 D(("list_response_callback"));
873 if(c
->rc
/ 100 == 2) {
875 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
,
879 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
883 static void volume_response_opcallback(disorder_eclient
*c
,
884 struct operation
*op
) {
887 D(("volume_response_callback"));
888 if(c
->rc
/ 100 == 2) {
890 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
891 protocol_error(c
, op
, -1, "%s: invalid volume response: %s",
894 ((disorder_eclient_volume_response
*)op
->completed
)(op
->v
, l
, r
);
897 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
900 static int simple(disorder_eclient
*c
,
901 operation_callback
*opcallback
,
904 const char *cmd
, ...) {
908 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, cmd
, ap
);
910 /* Give the state machine a kick, since we might be in state_idle */
911 disorder_eclient_polled(c
, 0);
915 /* Commands ******************************************************************/
917 int disorder_eclient_version(disorder_eclient
*c
,
918 disorder_eclient_string_response
*completed
,
920 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
921 "version", (char *)0);
924 int disorder_eclient_namepart(disorder_eclient
*c
,
925 disorder_eclient_string_response
*completed
,
930 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
931 "part", track
, context
, part
, (char *)0);
934 int disorder_eclient_play(disorder_eclient
*c
,
936 disorder_eclient_no_response
*completed
,
938 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
939 "play", track
, (char *)0);
942 int disorder_eclient_pause(disorder_eclient
*c
,
943 disorder_eclient_no_response
*completed
,
945 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
949 int disorder_eclient_resume(disorder_eclient
*c
,
950 disorder_eclient_no_response
*completed
,
952 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
953 "resume", (char *)0);
956 int disorder_eclient_scratch(disorder_eclient
*c
,
958 disorder_eclient_no_response
*completed
,
960 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
961 "scratch", id
, (char *)0);
964 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
965 disorder_eclient_no_response
*completed
,
967 return disorder_eclient_scratch(c
, 0, completed
, v
);
970 int disorder_eclient_remove(disorder_eclient
*c
,
972 disorder_eclient_no_response
*completed
,
974 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
975 "remove", id
, (char *)0);
978 int disorder_eclient_moveafter(disorder_eclient
*c
,
982 disorder_eclient_no_response
*completed
,
988 vector_append(&vec
, (char *)"moveafter");
989 vector_append(&vec
, (char *)target
);
990 for(n
= 0; n
< nids
; ++n
)
991 vector_append(&vec
, (char *)ids
[n
]);
992 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
994 disorder_eclient_polled(c
, 0);
998 int disorder_eclient_recent(disorder_eclient
*c
,
999 disorder_eclient_queue_response
*completed
,
1001 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1002 "recent", (char *)0);
1005 int disorder_eclient_queue(disorder_eclient
*c
,
1006 disorder_eclient_queue_response
*completed
,
1008 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1009 "queue", (char *)0);
1012 int disorder_eclient_files(disorder_eclient
*c
,
1013 disorder_eclient_list_response
*completed
,
1017 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1018 "files", dir
, re
, (char *)0);
1021 int disorder_eclient_dirs(disorder_eclient
*c
,
1022 disorder_eclient_list_response
*completed
,
1026 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1027 "dirs", dir
, re
, (char *)0);
1030 int disorder_eclient_playing(disorder_eclient
*c
,
1031 disorder_eclient_queue_response
*completed
,
1033 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1034 "playing", (char *)0);
1037 int disorder_eclient_length(disorder_eclient
*c
,
1038 disorder_eclient_integer_response
*completed
,
1041 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1042 "length", track
, (char *)0);
1045 int disorder_eclient_volume(disorder_eclient
*c
,
1046 disorder_eclient_volume_response
*completed
,
1049 char sl
[64], sr
[64];
1051 if(l
< 0 && r
< 0) {
1052 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1053 "volume", (char *)0);
1054 } else if(l
>= 0 && r
>= 0) {
1057 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1058 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1059 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1060 "volume", sl
, sr
, (char *)0);
1062 assert(!"invalid arguments to disorder_eclient_volume");
1063 return -1; /* gcc is being dim */
1067 int disorder_eclient_enable(disorder_eclient
*c
,
1068 disorder_eclient_no_response
*completed
,
1070 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1071 "enable", (char *)0);
1074 int disorder_eclient_disable(disorder_eclient
*c
,
1075 disorder_eclient_no_response
*completed
,
1077 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1078 "disable", (char *)0);
1081 int disorder_eclient_random_enable(disorder_eclient
*c
,
1082 disorder_eclient_no_response
*completed
,
1084 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1085 "random-enable", (char *)0);
1088 int disorder_eclient_random_disable(disorder_eclient
*c
,
1089 disorder_eclient_no_response
*completed
,
1091 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1092 "random-disable", (char *)0);
1095 int disorder_eclient_get(disorder_eclient
*c
,
1096 disorder_eclient_string_response
*completed
,
1097 const char *track
, const char *pref
,
1099 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1100 "get", track
, pref
, (char *)0);
1103 int disorder_eclient_set(disorder_eclient
*c
,
1104 disorder_eclient_no_response
*completed
,
1105 const char *track
, const char *pref
,
1108 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1109 "set", track
, pref
, value
, (char *)0);
1112 int disorder_eclient_unset(disorder_eclient
*c
,
1113 disorder_eclient_no_response
*completed
,
1114 const char *track
, const char *pref
,
1116 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1117 "unset", track
, pref
, (char *)0);
1120 int disorder_eclient_resolve(disorder_eclient
*c
,
1121 disorder_eclient_string_response
*completed
,
1124 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1125 "resolve", track
, (char *)0);
1128 int disorder_eclient_search(disorder_eclient
*c
,
1129 disorder_eclient_list_response
*completed
,
1132 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1133 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1134 "search", terms
, (char *)0);
1137 int disorder_eclient_nop(disorder_eclient
*c
,
1138 disorder_eclient_no_response
*completed
,
1140 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1144 /* Log clients ***************************************************************/
1146 /** @brief Monitor the server log
1148 * @param callbacks Functions to call when anything happens
1149 * @param v Passed to @p callbacks functions
1151 * Once a client is being used for logging it cannot be used for anything else.
1152 * There is magic in authuser_opcallback() to re-submit the @c log command
1153 * after reconnection.
1155 * NB that the @c state callback may be called from within this function,
1156 * i.e. not solely later on from the event loop callback.
1158 int disorder_eclient_log(disorder_eclient
*c
,
1159 const disorder_eclient_log_callbacks
*callbacks
,
1161 if(c
->log_callbacks
) return -1;
1162 c
->log_callbacks
= callbacks
;
1164 /* Repoort initial state */
1165 if(c
->log_callbacks
->state
)
1166 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1167 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1172 /* If we get here we've stopped being a log client */
1173 static void log_opcallback(disorder_eclient
*c
,
1174 struct operation
attribute((unused
)) *op
) {
1175 D(("log_opcallback"));
1176 c
->log_callbacks
= 0;
1180 /* error callback for log line parsing */
1181 static void logline_error(const char *msg
, void *u
) {
1182 disorder_eclient
*c
= u
;
1183 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1186 /* process a single log line */
1187 static void logline(disorder_eclient
*c
, const char *line
) {
1192 D(("logline [%s]", line
));
1193 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1194 if(nvec
< 2) return; /* probably an error, already
1196 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1197 /* probably the wrong side of a format change */
1198 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1201 /* TODO: do something with the time */
1202 n
= TABLE_FIND(logentry_handlers
, struct logentry_handler
, name
, vec
[1]);
1203 if(n
< 0) return; /* probably a future command */
1206 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
)
1208 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1211 static void logentry_completed(disorder_eclient
*c
,
1212 int attribute((unused
)) nvec
, char **vec
) {
1213 if(!c
->log_callbacks
->completed
) return;
1214 c
->statebits
&= ~DISORDER_PLAYING
;
1215 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1216 if(c
->log_callbacks
->state
)
1217 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1220 static void logentry_failed(disorder_eclient
*c
,
1221 int attribute((unused
)) nvec
, char **vec
) {
1222 if(!c
->log_callbacks
->failed
)return;
1223 c
->statebits
&= ~DISORDER_PLAYING
;
1224 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1225 if(c
->log_callbacks
->state
)
1226 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1229 static void logentry_moved(disorder_eclient
*c
,
1230 int attribute((unused
)) nvec
, char **vec
) {
1231 if(!c
->log_callbacks
->moved
) return;
1232 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1235 static void logentry_playing(disorder_eclient
*c
,
1236 int attribute((unused
)) nvec
, char **vec
) {
1237 if(!c
->log_callbacks
->playing
) return;
1238 c
->statebits
|= DISORDER_PLAYING
;
1239 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1240 if(c
->log_callbacks
->state
)
1241 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1244 static void logentry_queue(disorder_eclient
*c
,
1245 int attribute((unused
)) nvec
, char **vec
) {
1246 struct queue_entry
*q
;
1248 if(!c
->log_callbacks
->completed
) return;
1249 q
= xmalloc(sizeof *q
);
1250 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1252 c
->log_callbacks
->queue(c
->log_v
, q
);
1255 static void logentry_recent_added(disorder_eclient
*c
,
1256 int attribute((unused
)) nvec
, char **vec
) {
1257 struct queue_entry
*q
;
1259 if(!c
->log_callbacks
->recent_added
) return;
1260 q
= xmalloc(sizeof *q
);
1261 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1263 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1266 static void logentry_recent_removed(disorder_eclient
*c
,
1267 int attribute((unused
)) nvec
, char **vec
) {
1268 if(!c
->log_callbacks
->recent_removed
) return;
1269 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1272 static void logentry_removed(disorder_eclient
*c
,
1273 int attribute((unused
)) nvec
, char **vec
) {
1274 if(!c
->log_callbacks
->removed
) return;
1275 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1278 static void logentry_scratched(disorder_eclient
*c
,
1279 int attribute((unused
)) nvec
, char **vec
) {
1280 if(!c
->log_callbacks
->scratched
) return;
1281 c
->statebits
&= ~DISORDER_PLAYING
;
1282 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1283 if(c
->log_callbacks
->state
)
1284 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1287 static const struct {
1290 const char *disable
;
1291 } statestrings
[] = {
1292 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1293 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1294 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1296 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1298 static void logentry_state(disorder_eclient
*c
,
1299 int attribute((unused
)) nvec
, char **vec
) {
1302 for(n
= 0; n
< NSTATES
; ++n
)
1303 if(!strcmp(vec
[0], statestrings
[n
].enable
)) {
1304 c
->statebits
|= statestrings
[n
].bit
;
1306 } else if(!strcmp(vec
[0], statestrings
[n
].disable
)) {
1307 c
->statebits
&= ~statestrings
[n
].bit
;
1310 if(!c
->log_callbacks
->state
) return;
1311 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1314 static void logentry_volume(disorder_eclient
*c
,
1315 int attribute((unused
)) nvec
, char **vec
) {
1318 if(!c
->log_callbacks
->volume
) return;
1319 if(xstrtol(&l
, vec
[0], 0, 10)
1320 || xstrtol(&r
, vec
[1], 0, 10)
1321 || l
< 0 || l
> INT_MAX
1322 || r
< 0 || r
> INT_MAX
)
1324 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1327 /** @brief Convert @p statebits to a string */
1328 char *disorder_eclient_interpret_state(unsigned long statebits
) {
1332 static const struct {
1336 { DISORDER_PLAYING_ENABLED
, "playing_enabled" },
1337 { DISORDER_RANDOM_ENABLED
, "random_enabled" },
1338 { DISORDER_TRACK_PAUSED
, "track_paused" },
1339 { DISORDER_PLAYING
, "playing" },
1340 { DISORDER_CONNECTED
, "connected" },
1342 #define NBITS (sizeof bits / sizeof *bits)
1346 dynstr_append(d
, '0');
1347 for(n
= 0; n
< NBITS
; ++n
)
1348 if(statebits
& bits
[n
].bit
) {
1350 dynstr_append(d
, '|');
1351 dynstr_append_string(d
, bits
[n
].name
);
1352 statebits
^= bits
[n
].bit
;
1358 dynstr_append(d
, '|');
1359 sprintf(s
, "%#lx", statebits
);
1360 dynstr_append_string(d
, s
);
1362 dynstr_terminate(d
);
1371 indent-tabs-mode:nil