2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 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
);
173 static void logentry_rescanned(disorder_eclient
*c
, int nvec
, char **vec
);
175 /* Tables ********************************************************************/
177 /** @brief One possible log entry */
178 struct logentry_handler
{
179 const char *name
; /**< @brief Entry name */
180 int min
; /**< @brief Minimum arguments */
181 int max
; /**< @brief Maximum arguments */
182 void (*handler
)(disorder_eclient
*c
,
184 char **vec
); /**< @brief Handler function */
187 /** @brief Table for parsing log entries */
188 static const struct logentry_handler logentry_handlers
[] = {
189 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
194 LE(queue
, 2, INT_MAX
),
195 LE(recent_added
, 2, INT_MAX
),
196 LE(recent_removed
, 1, 1),
204 /* Setup and teardown ********************************************************/
206 /** @brief Create a new client
208 * Does NOT connect the client - connections are made (and re-made) on demand.
210 disorder_eclient
*disorder_eclient_new(const disorder_eclient_callbacks
*cb
,
212 disorder_eclient
*c
= xmalloc(sizeof *c
);
213 D(("disorder_eclient_new"));
217 c
->opstail
= &c
->ops
;
218 vector_init(&c
->vec
);
219 dynstr_init(&c
->input
);
220 dynstr_init(&c
->output
);
221 if(!config
->password
) {
222 error(0, "no password set");
228 /** @brief Disconnect a client
229 * @param c Client to disconnect
231 * NB that this routine just disconnnects the TCP connection. It does not
232 * destroy the client! If you continue to use it then it will attempt to
235 void disorder_eclient_close(disorder_eclient
*c
) {
236 struct operation
*op
;
238 D(("disorder_eclient_close"));
240 D(("disorder_eclient_close closing fd %d", c
->fd
));
241 c
->callbacks
->poll(c
->u
, c
, c
->fd
, 0);
244 c
->state
= state_disconnected
;
250 c
->authenticated
= 0;
251 /* We'll need to resend all operations */
252 for(op
= c
->ops
; op
; op
= op
->next
)
254 /* Drop our use a hint that we're disconnected */
255 if(c
->log_callbacks
&& c
->log_callbacks
->state
)
256 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
259 /** @brief Return current state */
260 unsigned long disorder_eclient_state(const disorder_eclient
*c
) {
261 return c
->statebits
| (c
->state
> state_connected ? DISORDER_CONNECTED
: 0);
264 /* Error reporting ***********************************************************/
266 /** @brief called when a connection error occurs
268 * After this called we will be disconnected (by disorder_eclient_close()),
269 * so there will be a reconnection before any commands can be sent.
271 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
277 byte_xvasprintf(&s
, fmt
, ap
);
279 disorder_eclient_close(c
);
280 c
->callbacks
->comms_error(c
->u
, s
);
284 /** @brief called when the server reports an error */
285 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
286 int code
, const char *fmt
, ...) {
290 D(("protocol_error"));
292 byte_xvasprintf(&s
, fmt
, ap
);
294 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
298 /* State machine *************************************************************/
300 /** @brief Called when there's something to do
302 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
304 * This should be called from by your code when the file descriptor is readable
305 * or writable (as requested by the @c poll callback, see @ref
306 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
307 * = 0) to allow for retries to work.
309 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
310 struct operation
*op
;
312 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
313 c
->fd
, states
[c
->state
],
314 mode
& DISORDER_POLL_READ ?
"READ" : "",
315 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
316 /* The pattern here is to check each possible state in turn and try to
317 * advance (though on error we might go back). If we advance we leave open
318 * the possibility of falling through to the next state, but we set the mode
319 * bits to 0, to avoid false positives (which matter more in some cases than
322 if(c
->state
== state_disconnected
) {
323 D(("state_disconnected"));
324 with_sockaddr(c
, start_connect
);
325 /* might now be state_disconnected (on error), state_connecting (slow
326 * connect) or state_connected (fast connect). If state_disconnected then
327 * we just rely on a periodic callback from the event loop sometime. */
331 if(c
->state
== state_connecting
&& mode
) {
332 D(("state_connecting"));
334 /* Might be state_disconnected (on error) or state_connected (on success).
335 * In the former case we rely on the event loop for a periodic callback to
340 if(c
->state
== state_connected
) {
341 D(("state_connected"));
342 /* We just connected. Initiate the authentication protocol. */
343 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
344 0/*completed*/, 0/*v*/, 0/*cmd*/);
345 /* We never stay is state_connected very long. We could in principle jump
346 * straight to state_cmdresponse since there's actually no command to
347 * send, but that would arguably be cheating. */
348 c
->state
= state_idle
;
351 if(c
->state
== state_idle
) {
353 /* We are connected, and have finished any command we set off, look for
357 if(c
->authenticated
) {
358 /* Transmit all unsent operations */
359 for(op
= c
->ops
; op
; op
= op
->next
) {
361 put(c
, op
->cmd
, strlen(op
->cmd
));
366 /* Just send the head operation */
367 if(c
->ops
->cmd
&& !c
->ops
->sent
) {
368 put(c
, c
->ops
->cmd
, strlen(c
->ops
->cmd
));
372 /* Awaiting response for the operation at the head of the list */
373 c
->state
= state_cmdresponse
;
376 c
->callbacks
->report(c
->u
, 0);
379 if(c
->state
== state_cmdresponse
380 || c
->state
== state_body
381 || c
->state
== state_log
) {
382 D(("state_%s", states
[c
->state
]));
383 /* We are awaiting a response */
384 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
385 if(mode
& DISORDER_POLL_READ
) read_input(c
);
386 /* There are a couple of reasons we might want to re-enter the state
387 * machine from the top. state_idle is obvious: there may be further
388 * commands to process. Re-entering on state_disconnected means that we
389 * immediately retry connection if a comms error occurs during a command.
390 * This is different to the case where a connection fails, where we await a
391 * spontaneous call to initiate the retry. */
393 case state_disconnected
: /* lost connection */
394 case state_idle
: /* completed a command */
396 disorder_eclient_polled(c
, 0);
403 /* Figure out what to set the mode to */
405 case state_disconnected
:
406 D(("state_disconnected (2)"));
407 /* Probably an error occurred. Await a retry. */
410 case state_connecting
:
411 D(("state_connecting (2)"));
412 /* Waiting for connect to complete */
413 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
415 case state_connected
:
416 D(("state_connected (2)"));
417 assert(!"should never be in state_connected here");
420 D(("state_idle (2)"));
421 /* Connected but nothing to do. */
424 case state_cmdresponse
:
427 D(("state_%s (2)", states
[c
->state
]));
428 /* Gathering a response. Wait for input. */
429 mode
= DISORDER_POLL_READ
;
430 /* Flush any pending output. */
431 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
434 D(("fd=%d new mode [%s %s]",
436 mode
& DISORDER_POLL_READ ?
"READ" : "",
437 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
438 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
441 /** @brief Called to start connecting */
442 static int start_connect(void *cc
,
443 const struct sockaddr
*sa
,
446 disorder_eclient
*c
= cc
;
448 D(("start_connect"));
449 c
->ident
= xstrdup(ident
);
454 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
455 return comms_error(c
, "socket: %s", strerror(errno
));
458 if(connect(c
->fd
, sa
, len
) < 0) {
462 c
->state
= state_connecting
;
463 /* We are called from _polled so the state machine will get to do its
467 /* Signal the error to the caller. */
468 return comms_error(c
, "connecting to %s: %s", ident
, strerror(errno
));
471 c
->state
= state_connected
;
475 /** @brief Called when poll triggers while waiting for a connection */
476 static void maybe_connected(disorder_eclient
*c
) {
477 /* We either connected, or got an error. */
479 socklen_t len
= sizeof err
;
481 D(("maybe_connected"));
482 /* Work around over-enthusiastic error slippage */
483 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
486 /* The connection failed */
487 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
488 /* sets state_disconnected */
492 /* The connection succeeded */
493 c
->state
= state_connected
;
494 byte_xasprintf(&r
, "connected to %s", c
->ident
);
495 c
->callbacks
->report(c
->u
, r
);
496 /* If this is a log client we expect to get a bunch of updates from the
497 * server straight away */
501 /* Authentication ************************************************************/
503 static void authbanner_opcallback(disorder_eclient
*c
,
504 struct operation
*op
) {
506 const unsigned char *nonce
;
510 const char *algo
= "SHA1";
512 D(("authbanner_opcallback"));
514 || !(rvec
= split(c
->line
+ 4, &nrvec
, SPLIT_QUOTES
, 0, 0))
516 /* Banner told us to go away, or was malformed. We cannot proceed. */
517 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
518 disorder_eclient_close(c
);
525 nonce
= unhex(rvec
[0], &nonce_len
);
526 res
= authhash(nonce
, nonce_len
, config
->password
, algo
);
528 protocol_error(c
, op
, c
->rc
, "%s: unknown authentication algorithm '%s'",
530 disorder_eclient_close(c
);
533 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
534 "user", quoteutf8(config
->username
), quoteutf8(res
),
538 static void authuser_opcallback(disorder_eclient
*c
,
539 struct operation
*op
) {
542 D(("authuser_opcallback"));
543 if(c
->rc
/ 100 != 2) {
544 /* Wrong password or something. We cannot proceed. */
545 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
546 disorder_eclient_close(c
);
549 /* OK, we're authenticated now. */
550 c
->authenticated
= 1;
551 byte_xasprintf(&r
, "authenticated with %s", c
->ident
);
552 c
->callbacks
->report(c
->u
, r
);
553 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
554 /* We are a log client, switch to logging mode */
555 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
559 /* Output ********************************************************************/
561 /* Chop N bytes off the front of a dynstr */
562 static void consume(struct dynstr
*d
, int n
) {
563 D(("consume %d", n
));
564 assert(d
->nvec
>= n
);
565 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
569 /* Write some bytes */
570 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
571 D(("put %d %.*s", c
->fd
, (int)n
, s
));
572 dynstr_append_bytes(&c
->output
, s
, n
);
575 /* Called when we can write to our FD, or at any other time */
576 static void send_output(disorder_eclient
*c
) {
579 D(("send_output %d bytes pending", c
->output
.nvec
));
580 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
581 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
588 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
592 consume(&c
->output
, n
);
596 /* Input *********************************************************************/
598 /* Called when c->fd might be readable, or at any other time */
599 static void read_input(disorder_eclient
*c
) {
604 D(("read_input in state %s", states
[c
->state
]));
605 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
606 /* read some more input */
607 n
= read(c
->fd
, buffer
, sizeof buffer
);
614 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
617 return; /* no new input to process */
619 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
620 dynstr_append_bytes(&c
->input
, buffer
, n
);
623 /* might have more than one line to process */
624 while(c
->state
> state_connecting
625 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
626 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
627 /* we might have disconnected along the way, which zogs the input buffer */
628 if(c
->state
> state_connecting
)
629 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
632 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
633 c
->authenticated
= 0;
637 /* called with a line that has just been read */
638 static void process_line(disorder_eclient
*c
, char *line
) {
639 D(("process_line %d [%s]", c
->fd
, line
));
641 case state_cmdresponse
:
642 /* This is the first line of a response */
643 if(!(line
[0] >= '0' && line
[0] <= '9'
644 && line
[1] >= '0' && line
[1] <= '9'
645 && line
[2] >= '0' && line
[2] <= '9'
647 fatal(0, "invalid response from server: %s", line
);
648 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
652 /* We need to collect the body. */
653 c
->state
= state_body
;
654 vector_init(&c
->vec
);
657 assert(c
->log_callbacks
!= 0);
658 if(c
->log_callbacks
->connected
)
659 c
->log_callbacks
->connected(c
->log_v
);
660 c
->state
= state_log
;
663 /* We've got the whole response. Go into the idle state so the state
664 * machine knows we're done and then call the operation callback. */
670 if(strcmp(line
, ".")) {
671 /* A line from the body */
672 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
674 /* End of the body. */
675 vector_terminate(&c
->vec
);
680 if(strcmp(line
, ".")) {
681 logline(c
, line
+ (line
[0] == '.'));
686 assert(!"wrong state for location");
691 /* Called when an operation completes */
692 static void complete(disorder_eclient
*c
) {
693 struct operation
*op
;
696 /* Pop the operation off the queue */
699 if(c
->opstail
== &op
->next
)
700 c
->opstail
= &c
->ops
;
701 /* If we've pipelined a command ahead then we go straight to cmdresponser.
702 * Otherwise we go to idle, which will arrange further sends. */
703 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
704 op
->opcallback(c
, op
);
705 /* Note that we always call the opcallback even on error, though command
706 * opcallbacks generally always do the same error handling, i.e. just call
707 * protocol_error(). It's the auth* opcallbacks that have different
711 /* Operation setup ***********************************************************/
713 static void stash_command_vector(disorder_eclient
*c
,
715 operation_callback
*opcallback
,
720 struct operation
*op
= xmalloc(sizeof *op
);
726 for(n
= 0; n
< ncmd
; ++n
) {
728 dynstr_append(&d
, ' ');
729 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
731 dynstr_append(&d
, '\n');
732 dynstr_terminate(&d
);
735 op
->cmd
= 0; /* usually, awaiting challenge */
736 op
->opcallback
= opcallback
;
737 op
->completed
= completed
;
741 assert(op
->sent
== 0);
743 /* Authentication operations jump the queue of useful commands */
746 if(c
->opstail
== &c
->ops
)
747 c
->opstail
= &op
->next
;
748 for(op
= c
->ops
; op
; op
= op
->next
)
752 c
->opstail
= &op
->next
;
756 static void vstash_command(disorder_eclient
*c
,
758 operation_callback
*opcallback
,
761 const char *cmd
, va_list ap
) {
765 D(("vstash_command %s", cmd ? cmd
: "NULL"));
768 vector_append(&vec
, (char *)cmd
);
769 while((arg
= va_arg(ap
, char *)))
770 vector_append(&vec
, arg
);
771 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
774 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
, 0, 0);
777 static void stash_command(disorder_eclient
*c
,
779 operation_callback
*opcallback
,
787 vstash_command(c
, queuejump
, opcallback
, completed
, v
, cmd
, ap
);
791 /* Command support ***********************************************************/
793 /* for commands with a simple string response */
794 static void string_response_opcallback(disorder_eclient
*c
,
795 struct operation
*op
) {
796 D(("string_response_callback"));
797 if(c
->rc
/ 100 == 2) {
799 ((disorder_eclient_string_response
*)op
->completed
)(op
->v
, c
->line
+ 4);
801 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
804 /* for commands with a simple integer response */
805 static void integer_response_opcallback(disorder_eclient
*c
,
806 struct operation
*op
) {
807 D(("string_response_callback"));
808 if(c
->rc
/ 100 == 2) {
810 ((disorder_eclient_integer_response
*)op
->completed
)
811 (op
->v
, strtol(c
->line
+ 4, 0, 10));
813 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
816 /* for commands with no response */
817 static void no_response_opcallback(disorder_eclient
*c
,
818 struct operation
*op
) {
819 D(("no_response_callback"));
820 if(c
->rc
/ 100 == 2) {
822 ((disorder_eclient_no_response
*)op
->completed
)(op
->v
);
824 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
827 /* error callback for queue_unmarshall */
828 static void eclient_queue_error(const char *msg
,
830 struct operation
*op
= u
;
832 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
835 /* for commands that expect a queue dump */
836 static void queue_response_opcallback(disorder_eclient
*c
,
837 struct operation
*op
) {
839 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
841 D(("queue_response_callback"));
842 if(c
->rc
/ 100 == 2) {
843 /* parse the queue */
844 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
845 q
= xmalloc(sizeof *q
);
846 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
847 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], eclient_queue_error
, op
)) {
855 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, qh
);
857 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
861 static void playing_response_opcallback(disorder_eclient
*c
,
862 struct operation
*op
) {
863 struct queue_entry
*q
;
865 D(("playing_response_callback"));
866 if(c
->rc
/ 100 == 2) {
869 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
870 eclient_queue_error
, c
))
877 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
881 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, q
);
883 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
886 /* for commands that expect a list of some sort */
887 static void list_response_opcallback(disorder_eclient
*c
,
888 struct operation
*op
) {
889 D(("list_response_callback"));
890 if(c
->rc
/ 100 == 2) {
892 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
,
896 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
900 static void volume_response_opcallback(disorder_eclient
*c
,
901 struct operation
*op
) {
904 D(("volume_response_callback"));
905 if(c
->rc
/ 100 == 2) {
907 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
908 protocol_error(c
, op
, -1, "%s: invalid volume response: %s",
911 ((disorder_eclient_volume_response
*)op
->completed
)(op
->v
, l
, r
);
914 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
917 static int simple(disorder_eclient
*c
,
918 operation_callback
*opcallback
,
921 const char *cmd
, ...) {
925 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, cmd
, ap
);
927 /* Give the state machine a kick, since we might be in state_idle */
928 disorder_eclient_polled(c
, 0);
932 /* Commands ******************************************************************/
934 int disorder_eclient_version(disorder_eclient
*c
,
935 disorder_eclient_string_response
*completed
,
937 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
938 "version", (char *)0);
941 int disorder_eclient_namepart(disorder_eclient
*c
,
942 disorder_eclient_string_response
*completed
,
947 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
948 "part", track
, context
, part
, (char *)0);
951 int disorder_eclient_play(disorder_eclient
*c
,
953 disorder_eclient_no_response
*completed
,
955 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
956 "play", track
, (char *)0);
959 int disorder_eclient_pause(disorder_eclient
*c
,
960 disorder_eclient_no_response
*completed
,
962 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
966 int disorder_eclient_resume(disorder_eclient
*c
,
967 disorder_eclient_no_response
*completed
,
969 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
970 "resume", (char *)0);
973 int disorder_eclient_scratch(disorder_eclient
*c
,
975 disorder_eclient_no_response
*completed
,
977 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
978 "scratch", id
, (char *)0);
981 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
982 disorder_eclient_no_response
*completed
,
984 return disorder_eclient_scratch(c
, 0, completed
, v
);
987 int disorder_eclient_remove(disorder_eclient
*c
,
989 disorder_eclient_no_response
*completed
,
991 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
992 "remove", id
, (char *)0);
995 int disorder_eclient_moveafter(disorder_eclient
*c
,
999 disorder_eclient_no_response
*completed
,
1005 vector_append(&vec
, (char *)"moveafter");
1006 vector_append(&vec
, (char *)target
);
1007 for(n
= 0; n
< nids
; ++n
)
1008 vector_append(&vec
, (char *)ids
[n
]);
1009 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
1011 disorder_eclient_polled(c
, 0);
1015 int disorder_eclient_recent(disorder_eclient
*c
,
1016 disorder_eclient_queue_response
*completed
,
1018 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1019 "recent", (char *)0);
1022 int disorder_eclient_queue(disorder_eclient
*c
,
1023 disorder_eclient_queue_response
*completed
,
1025 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1026 "queue", (char *)0);
1029 int disorder_eclient_files(disorder_eclient
*c
,
1030 disorder_eclient_list_response
*completed
,
1034 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1035 "files", dir
, re
, (char *)0);
1038 int disorder_eclient_dirs(disorder_eclient
*c
,
1039 disorder_eclient_list_response
*completed
,
1043 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1044 "dirs", dir
, re
, (char *)0);
1047 int disorder_eclient_playing(disorder_eclient
*c
,
1048 disorder_eclient_queue_response
*completed
,
1050 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1051 "playing", (char *)0);
1054 int disorder_eclient_length(disorder_eclient
*c
,
1055 disorder_eclient_integer_response
*completed
,
1058 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1059 "length", track
, (char *)0);
1062 int disorder_eclient_volume(disorder_eclient
*c
,
1063 disorder_eclient_volume_response
*completed
,
1066 char sl
[64], sr
[64];
1068 if(l
< 0 && r
< 0) {
1069 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1070 "volume", (char *)0);
1071 } else if(l
>= 0 && r
>= 0) {
1074 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1075 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1076 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1077 "volume", sl
, sr
, (char *)0);
1079 assert(!"invalid arguments to disorder_eclient_volume");
1080 return -1; /* gcc is being dim */
1084 int disorder_eclient_enable(disorder_eclient
*c
,
1085 disorder_eclient_no_response
*completed
,
1087 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1088 "enable", (char *)0);
1091 int disorder_eclient_disable(disorder_eclient
*c
,
1092 disorder_eclient_no_response
*completed
,
1094 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1095 "disable", (char *)0);
1098 int disorder_eclient_random_enable(disorder_eclient
*c
,
1099 disorder_eclient_no_response
*completed
,
1101 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1102 "random-enable", (char *)0);
1105 int disorder_eclient_random_disable(disorder_eclient
*c
,
1106 disorder_eclient_no_response
*completed
,
1108 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1109 "random-disable", (char *)0);
1112 int disorder_eclient_get(disorder_eclient
*c
,
1113 disorder_eclient_string_response
*completed
,
1114 const char *track
, const char *pref
,
1116 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1117 "get", track
, pref
, (char *)0);
1120 int disorder_eclient_set(disorder_eclient
*c
,
1121 disorder_eclient_no_response
*completed
,
1122 const char *track
, const char *pref
,
1125 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1126 "set", track
, pref
, value
, (char *)0);
1129 int disorder_eclient_unset(disorder_eclient
*c
,
1130 disorder_eclient_no_response
*completed
,
1131 const char *track
, const char *pref
,
1133 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1134 "unset", track
, pref
, (char *)0);
1137 int disorder_eclient_resolve(disorder_eclient
*c
,
1138 disorder_eclient_string_response
*completed
,
1141 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1142 "resolve", track
, (char *)0);
1145 int disorder_eclient_search(disorder_eclient
*c
,
1146 disorder_eclient_list_response
*completed
,
1149 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1150 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1151 "search", terms
, (char *)0);
1154 int disorder_eclient_nop(disorder_eclient
*c
,
1155 disorder_eclient_no_response
*completed
,
1157 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1161 /** @brief Get the last @p max added tracks
1163 * @param completed Called with list
1164 * @param max Number of tracks to get, 0 for all
1165 * @param v Passed to @p completed
1167 * The first track in the list is the most recently added.
1169 int disorder_eclient_new_tracks(disorder_eclient
*c
,
1170 disorder_eclient_list_response
*completed
,
1175 sprintf(limit
, "%d", max
);
1176 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1177 "new", limit
, (char *)0);
1180 static void rtp_response_opcallback(disorder_eclient
*c
,
1181 struct operation
*op
) {
1182 D(("rtp_response_opcallback"));
1183 if(c
->rc
/ 100 == 2) {
1186 char **vec
= split(c
->line
+ 4, &nvec
, SPLIT_QUOTES
, 0, 0);
1188 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
, nvec
, vec
);
1191 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
1194 /** @brief Determine the RTP target address
1196 * @param completed Called with address details
1197 * @param v Passed to @p completed
1199 * The address details will be two elements, the first being the hostname and
1200 * the second the service (port).
1202 int disorder_eclient_rtp_address(disorder_eclient
*c
,
1203 disorder_eclient_list_response
*completed
,
1205 return simple(c
, rtp_response_opcallback
, (void (*)())completed
, v
,
1206 "rtp-address", (char *)0);
1209 /* Log clients ***************************************************************/
1211 /** @brief Monitor the server log
1213 * @param callbacks Functions to call when anything happens
1214 * @param v Passed to @p callbacks functions
1216 * Once a client is being used for logging it cannot be used for anything else.
1217 * There is magic in authuser_opcallback() to re-submit the @c log command
1218 * after reconnection.
1220 * NB that the @c state callback may be called from within this function,
1221 * i.e. not solely later on from the event loop callback.
1223 int disorder_eclient_log(disorder_eclient
*c
,
1224 const disorder_eclient_log_callbacks
*callbacks
,
1226 if(c
->log_callbacks
) return -1;
1227 c
->log_callbacks
= callbacks
;
1229 /* Repoort initial state */
1230 if(c
->log_callbacks
->state
)
1231 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1232 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1237 /* If we get here we've stopped being a log client */
1238 static void log_opcallback(disorder_eclient
*c
,
1239 struct operation
attribute((unused
)) *op
) {
1240 D(("log_opcallback"));
1241 c
->log_callbacks
= 0;
1245 /* error callback for log line parsing */
1246 static void logline_error(const char *msg
, void *u
) {
1247 disorder_eclient
*c
= u
;
1248 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1251 /* process a single log line */
1252 static void logline(disorder_eclient
*c
, const char *line
) {
1257 D(("logline [%s]", line
));
1258 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1259 if(nvec
< 2) return; /* probably an error, already
1261 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1262 /* probably the wrong side of a format change */
1263 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1266 /* TODO: do something with the time */
1267 n
= TABLE_FIND(logentry_handlers
, struct logentry_handler
, name
, vec
[1]);
1268 if(n
< 0) return; /* probably a future command */
1271 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
)
1273 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1276 static void logentry_completed(disorder_eclient
*c
,
1277 int attribute((unused
)) nvec
, char **vec
) {
1278 if(!c
->log_callbacks
->completed
) return;
1279 c
->statebits
&= ~DISORDER_PLAYING
;
1280 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1281 if(c
->log_callbacks
->state
)
1282 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1285 static void logentry_failed(disorder_eclient
*c
,
1286 int attribute((unused
)) nvec
, char **vec
) {
1287 if(!c
->log_callbacks
->failed
)return;
1288 c
->statebits
&= ~DISORDER_PLAYING
;
1289 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1290 if(c
->log_callbacks
->state
)
1291 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1294 static void logentry_moved(disorder_eclient
*c
,
1295 int attribute((unused
)) nvec
, char **vec
) {
1296 if(!c
->log_callbacks
->moved
) return;
1297 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1300 static void logentry_playing(disorder_eclient
*c
,
1301 int attribute((unused
)) nvec
, char **vec
) {
1302 if(!c
->log_callbacks
->playing
) return;
1303 c
->statebits
|= DISORDER_PLAYING
;
1304 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1305 if(c
->log_callbacks
->state
)
1306 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1309 static void logentry_queue(disorder_eclient
*c
,
1310 int attribute((unused
)) nvec
, char **vec
) {
1311 struct queue_entry
*q
;
1313 if(!c
->log_callbacks
->completed
) return;
1314 q
= xmalloc(sizeof *q
);
1315 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1317 c
->log_callbacks
->queue(c
->log_v
, q
);
1320 static void logentry_recent_added(disorder_eclient
*c
,
1321 int attribute((unused
)) nvec
, char **vec
) {
1322 struct queue_entry
*q
;
1324 if(!c
->log_callbacks
->recent_added
) return;
1325 q
= xmalloc(sizeof *q
);
1326 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1328 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1331 static void logentry_recent_removed(disorder_eclient
*c
,
1332 int attribute((unused
)) nvec
, char **vec
) {
1333 if(!c
->log_callbacks
->recent_removed
) return;
1334 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1337 static void logentry_removed(disorder_eclient
*c
,
1338 int attribute((unused
)) nvec
, char **vec
) {
1339 if(!c
->log_callbacks
->removed
) return;
1340 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1343 static void logentry_rescanned(disorder_eclient
*c
,
1344 int attribute((unused
)) nvec
,
1345 char attribute((unused
)) **vec
) {
1346 if(!c
->log_callbacks
->rescanned
) return;
1347 c
->log_callbacks
->rescanned(c
->log_v
);
1350 static void logentry_scratched(disorder_eclient
*c
,
1351 int attribute((unused
)) nvec
, char **vec
) {
1352 if(!c
->log_callbacks
->scratched
) return;
1353 c
->statebits
&= ~DISORDER_PLAYING
;
1354 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1355 if(c
->log_callbacks
->state
)
1356 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1359 static const struct {
1362 const char *disable
;
1363 } statestrings
[] = {
1364 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1365 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1366 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1367 { DISORDER_PLAYING
, "playing", "completed" },
1368 { DISORDER_PLAYING
, 0, "scratched" },
1369 { DISORDER_PLAYING
, 0, "failed" },
1371 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1373 static void logentry_state(disorder_eclient
*c
,
1374 int attribute((unused
)) nvec
, char **vec
) {
1377 for(n
= 0; n
< NSTATES
; ++n
)
1378 if(statestrings
[n
].enable
&& !strcmp(vec
[0], statestrings
[n
].enable
)) {
1379 c
->statebits
|= statestrings
[n
].bit
;
1381 } else if(statestrings
[n
].disable
&& !strcmp(vec
[0], statestrings
[n
].disable
)) {
1382 c
->statebits
&= ~statestrings
[n
].bit
;
1385 if(!c
->log_callbacks
->state
) return;
1386 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1389 static void logentry_volume(disorder_eclient
*c
,
1390 int attribute((unused
)) nvec
, char **vec
) {
1393 if(!c
->log_callbacks
->volume
) return;
1394 if(xstrtol(&l
, vec
[0], 0, 10)
1395 || xstrtol(&r
, vec
[1], 0, 10)
1396 || l
< 0 || l
> INT_MAX
1397 || r
< 0 || r
> INT_MAX
)
1399 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1402 /** @brief Convert @p statebits to a string */
1403 char *disorder_eclient_interpret_state(unsigned long statebits
) {
1407 static const struct {
1411 { DISORDER_PLAYING_ENABLED
, "playing_enabled" },
1412 { DISORDER_RANDOM_ENABLED
, "random_enabled" },
1413 { DISORDER_TRACK_PAUSED
, "track_paused" },
1414 { DISORDER_PLAYING
, "playing" },
1415 { DISORDER_CONNECTED
, "connected" },
1417 #define NBITS (sizeof bits / sizeof *bits)
1421 dynstr_append(d
, '0');
1422 for(n
= 0; n
< NBITS
; ++n
)
1423 if(statebits
& bits
[n
].bit
) {
1425 dynstr_append(d
, '|');
1426 dynstr_append_string(d
, bits
[n
].name
);
1427 statebits
^= bits
[n
].bit
;
1433 dynstr_append(d
, '|');
1434 sprintf(s
, "%#lx", statebits
);
1435 dynstr_append_string(d
, s
);
1437 dynstr_terminate(d
);
1446 indent-tabs-mode:nil