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 /** @brief How often to send data to the server when receiving logs */
62 #define LOG_PROD_INTERVAL 10
64 /* Types *********************************************************************/
66 /** @brief Client state */
68 state_disconnected
, /**< @brief not connected */
69 state_connecting
, /**< @brief waiting for connect() */
70 state_connected
, /**< @brief connected but not authenticated */
71 state_idle
, /**< @brief not doing anything */
72 state_cmdresponse
, /**< @brief waiting for command resonse */
73 state_body
, /**< @brief accumulating body */
74 state_log
, /**< @brief monitoring log */
77 /** @brief Names for @ref client_state */
78 static const char *const states
[] = {
88 struct operation
; /* forward decl */
90 /** @brief Type of an operation callback */
91 typedef void operation_callback(disorder_eclient
*c
, struct operation
*op
);
93 /** @brief A pending operation.
95 * This can be either a command or part of the authentication protocol. In the
96 * former case new commands are appended to the list, in the latter case they
97 * are inserted at the front. */
99 struct operation
*next
; /**< @brief next operation */
100 char *cmd
; /**< @brief command to send or 0 */
101 operation_callback
*opcallback
; /**< @brief internal completion callback */
102 void (*completed
)(); /**< @brief user completion callback or 0 */
103 void *v
; /**< @brief data for COMPLETED */
104 disorder_eclient
*client
; /**< @brief owning client */
106 /** @brief true if sent to server
108 * This is cleared by disorder_eclient_close(), forcing all queued
109 * commands to be transparently resent.
114 /** @brief Client structure */
115 struct disorder_eclient
{
117 int fd
; /**< @brief connection to server */
118 enum client_state state
; /**< @brief current state */
119 int authenticated
; /**< @brief true when authenicated */
120 struct dynstr output
; /**< @brief output buffer */
121 struct dynstr input
; /**< @brief input buffer */
122 int eof
; /**< @brief input buffer is at EOF */
123 const disorder_eclient_callbacks
*callbacks
; /**< @brief error callbacks */
124 void *u
; /**< @brief user data */
125 struct operation
*ops
; /**< @brief queue of operations */
126 struct operation
**opstail
; /**< @brief queue tail */
127 /* accumulated response */
128 int rc
; /**< @brief response code */
129 char *line
; /**< @brief complete line */
130 struct vector vec
; /**< @brief body */
132 const disorder_eclient_log_callbacks
*log_callbacks
;
133 /**< @brief log callbacks
135 * Once disorder_eclient_log() has been issued this is always set. When we
136 * re-connect it is checked to re-issue the log command.
138 void *log_v
; /**< @brief user data */
139 unsigned long statebits
; /**< @brief latest state */
142 /**< @brief last time we sent a prod
144 * When we are receiving log data we send a "prod" byte to the server from
145 * time to time so that we detect broken connections reasonably quickly. The
146 * server just ignores these bytes.
150 /* Forward declarations ******************************************************/
152 static int start_connect(void *cc
,
153 const struct sockaddr
*sa
,
156 static void process_line(disorder_eclient
*c
, char *line
);
157 static int start_connect(void *cc
,
158 const struct sockaddr
*sa
,
161 static void maybe_connected(disorder_eclient
*c
);
162 static void authbanner_opcallback(disorder_eclient
*c
,
163 struct operation
*op
);
164 static void authuser_opcallback(disorder_eclient
*c
,
165 struct operation
*op
);
166 static void complete(disorder_eclient
*c
);
167 static void send_output(disorder_eclient
*c
);
168 static void put(disorder_eclient
*c
, const char *s
, size_t n
);
169 static void read_input(disorder_eclient
*c
);
170 static void stash_command(disorder_eclient
*c
,
172 operation_callback
*opcallback
,
177 static void log_opcallback(disorder_eclient
*c
, struct operation
*op
);
178 static void logline(disorder_eclient
*c
, const char *line
);
179 static void logentry_completed(disorder_eclient
*c
, int nvec
, char **vec
);
180 static void logentry_failed(disorder_eclient
*c
, int nvec
, char **vec
);
181 static void logentry_moved(disorder_eclient
*c
, int nvec
, char **vec
);
182 static void logentry_playing(disorder_eclient
*c
, int nvec
, char **vec
);
183 static void logentry_queue(disorder_eclient
*c
, int nvec
, char **vec
);
184 static void logentry_recent_added(disorder_eclient
*c
, int nvec
, char **vec
);
185 static void logentry_recent_removed(disorder_eclient
*c
, int nvec
, char **vec
);
186 static void logentry_removed(disorder_eclient
*c
, int nvec
, char **vec
);
187 static void logentry_scratched(disorder_eclient
*c
, int nvec
, char **vec
);
188 static void logentry_state(disorder_eclient
*c
, int nvec
, char **vec
);
189 static void logentry_volume(disorder_eclient
*c
, int nvec
, char **vec
);
190 static void logentry_rescanned(disorder_eclient
*c
, int nvec
, char **vec
);
192 /* Tables ********************************************************************/
194 /** @brief One possible log entry */
195 struct logentry_handler
{
196 const char *name
; /**< @brief Entry name */
197 int min
; /**< @brief Minimum arguments */
198 int max
; /**< @brief Maximum arguments */
199 void (*handler
)(disorder_eclient
*c
,
201 char **vec
); /**< @brief Handler function */
204 /** @brief Table for parsing log entries */
205 static const struct logentry_handler logentry_handlers
[] = {
206 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
211 LE(queue
, 2, INT_MAX
),
212 LE(recent_added
, 2, INT_MAX
),
213 LE(recent_removed
, 1, 1),
221 /* Setup and teardown ********************************************************/
223 /** @brief Create a new client
225 * Does NOT connect the client - connections are made (and re-made) on demand.
227 disorder_eclient
*disorder_eclient_new(const disorder_eclient_callbacks
*cb
,
229 disorder_eclient
*c
= xmalloc(sizeof *c
);
230 D(("disorder_eclient_new"));
234 c
->opstail
= &c
->ops
;
235 vector_init(&c
->vec
);
236 dynstr_init(&c
->input
);
237 dynstr_init(&c
->output
);
238 if(!config
->password
) {
239 error(0, "no password set");
245 /** @brief Disconnect a client
246 * @param c Client to disconnect
248 * NB that this routine just disconnnects the TCP connection. It does not
249 * destroy the client! If you continue to use it then it will attempt to
252 void disorder_eclient_close(disorder_eclient
*c
) {
253 struct operation
*op
;
255 D(("disorder_eclient_close"));
257 D(("disorder_eclient_close closing fd %d", c
->fd
));
258 c
->callbacks
->poll(c
->u
, c
, c
->fd
, 0);
261 c
->state
= state_disconnected
;
267 c
->authenticated
= 0;
268 /* We'll need to resend all operations */
269 for(op
= c
->ops
; op
; op
= op
->next
)
271 /* Drop our use a hint that we're disconnected */
272 if(c
->log_callbacks
&& c
->log_callbacks
->state
)
273 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
276 /** @brief Return current state */
277 unsigned long disorder_eclient_state(const disorder_eclient
*c
) {
278 return c
->statebits
| (c
->state
> state_connected ? DISORDER_CONNECTED
: 0);
281 /* Error reporting ***********************************************************/
283 /** @brief called when a connection error occurs
285 * After this called we will be disconnected (by disorder_eclient_close()),
286 * so there will be a reconnection before any commands can be sent.
288 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
294 byte_xvasprintf(&s
, fmt
, ap
);
296 disorder_eclient_close(c
);
297 c
->callbacks
->comms_error(c
->u
, s
);
301 /** @brief called when the server reports an error */
302 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
303 int code
, const char *fmt
, ...) {
307 D(("protocol_error"));
309 byte_xvasprintf(&s
, fmt
, ap
);
311 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
315 /* State machine *************************************************************/
317 /** @brief Called when there's something to do
319 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
321 * This should be called from by your code when the file descriptor is readable
322 * or writable (as requested by the @c poll callback, see @ref
323 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
324 * = 0) to allow for retries to work.
326 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
327 struct operation
*op
;
330 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
331 c
->fd
, states
[c
->state
],
332 mode
& DISORDER_POLL_READ ?
"READ" : "",
333 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
334 /* The pattern here is to check each possible state in turn and try to
335 * advance (though on error we might go back). If we advance we leave open
336 * the possibility of falling through to the next state, but we set the mode
337 * bits to 0, to avoid false positives (which matter more in some cases than
340 if(c
->state
== state_disconnected
) {
341 D(("state_disconnected"));
342 with_sockaddr(c
, start_connect
);
343 /* might now be state_disconnected (on error), state_connecting (slow
344 * connect) or state_connected (fast connect). If state_disconnected then
345 * we just rely on a periodic callback from the event loop sometime. */
349 if(c
->state
== state_connecting
&& mode
) {
350 D(("state_connecting"));
352 /* Might be state_disconnected (on error) or state_connected (on success).
353 * In the former case we rely on the event loop for a periodic callback to
358 if(c
->state
== state_connected
) {
359 D(("state_connected"));
360 /* We just connected. Initiate the authentication protocol. */
361 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
362 0/*completed*/, 0/*v*/, 0/*cmd*/);
363 /* We never stay is state_connected very long. We could in principle jump
364 * straight to state_cmdresponse since there's actually no command to
365 * send, but that would arguably be cheating. */
366 c
->state
= state_idle
;
369 if(c
->state
== state_idle
) {
371 /* We are connected, and have finished any command we set off, look for
375 if(c
->authenticated
) {
376 /* Transmit all unsent operations */
377 for(op
= c
->ops
; op
; op
= op
->next
) {
379 put(c
, op
->cmd
, strlen(op
->cmd
));
384 /* Just send the head operation */
385 if(c
->ops
->cmd
&& !c
->ops
->sent
) {
386 put(c
, c
->ops
->cmd
, strlen(c
->ops
->cmd
));
390 /* Awaiting response for the operation at the head of the list */
391 c
->state
= state_cmdresponse
;
394 c
->callbacks
->report(c
->u
, 0);
397 /* Queue up a byte to send */
398 if(c
->state
== state_log
399 && c
->output
.nvec
== 0
400 && time(&now
) - c
->last_prod
> LOG_PROD_INTERVAL
) {
405 if(c
->state
== state_cmdresponse
406 || c
->state
== state_body
407 || c
->state
== state_log
) {
408 D(("state_%s", states
[c
->state
]));
409 /* We are awaiting a response */
410 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
411 if(mode
& DISORDER_POLL_READ
) read_input(c
);
412 /* There are a couple of reasons we might want to re-enter the state
413 * machine from the top. state_idle is obvious: there may be further
414 * commands to process. Re-entering on state_disconnected means that we
415 * immediately retry connection if a comms error occurs during a command.
416 * This is different to the case where a connection fails, where we await a
417 * spontaneous call to initiate the retry. */
419 case state_disconnected
: /* lost connection */
420 case state_idle
: /* completed a command */
422 disorder_eclient_polled(c
, 0);
429 /* Figure out what to set the mode to */
431 case state_disconnected
:
432 D(("state_disconnected (2)"));
433 /* Probably an error occurred. Await a retry. */
436 case state_connecting
:
437 D(("state_connecting (2)"));
438 /* Waiting for connect to complete */
439 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
441 case state_connected
:
442 D(("state_connected (2)"));
443 assert(!"should never be in state_connected here");
446 D(("state_idle (2)"));
447 /* Connected but nothing to do. */
450 case state_cmdresponse
:
453 D(("state_%s (2)", states
[c
->state
]));
454 /* Gathering a response. Wait for input. */
455 mode
= DISORDER_POLL_READ
;
456 /* Flush any pending output. */
457 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
460 D(("fd=%d new mode [%s %s]",
462 mode
& DISORDER_POLL_READ ?
"READ" : "",
463 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
464 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
467 /** @brief Called to start connecting */
468 static int start_connect(void *cc
,
469 const struct sockaddr
*sa
,
472 disorder_eclient
*c
= cc
;
474 D(("start_connect"));
475 c
->ident
= xstrdup(ident
);
480 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
481 return comms_error(c
, "socket: %s", strerror(errno
));
485 if(connect(c
->fd
, sa
, len
) < 0) {
489 c
->state
= state_connecting
;
490 /* We are called from _polled so the state machine will get to do its
494 /* Signal the error to the caller. */
495 return comms_error(c
, "connecting to %s: %s", ident
, strerror(errno
));
498 c
->state
= state_connected
;
502 /** @brief Called when poll triggers while waiting for a connection */
503 static void maybe_connected(disorder_eclient
*c
) {
504 /* We either connected, or got an error. */
506 socklen_t len
= sizeof err
;
508 D(("maybe_connected"));
509 /* Work around over-enthusiastic error slippage */
510 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
513 /* The connection failed */
514 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
515 /* sets state_disconnected */
519 /* The connection succeeded */
520 c
->state
= state_connected
;
521 byte_xasprintf(&r
, "connected to %s", c
->ident
);
522 c
->callbacks
->report(c
->u
, r
);
523 /* If this is a log client we expect to get a bunch of updates from the
524 * server straight away */
528 /* Authentication ************************************************************/
530 static void authbanner_opcallback(disorder_eclient
*c
,
531 struct operation
*op
) {
533 const unsigned char *nonce
;
537 const char *algo
= "SHA1";
539 D(("authbanner_opcallback"));
541 || !(rvec
= split(c
->line
+ 4, &nrvec
, SPLIT_QUOTES
, 0, 0))
543 /* Banner told us to go away, or was malformed. We cannot proceed. */
544 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
545 disorder_eclient_close(c
);
552 nonce
= unhex(rvec
[0], &nonce_len
);
553 res
= authhash(nonce
, nonce_len
, config
->password
, algo
);
555 protocol_error(c
, op
, c
->rc
, "%s: unknown authentication algorithm '%s'",
557 disorder_eclient_close(c
);
560 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
561 "user", quoteutf8(config
->username
), quoteutf8(res
),
565 static void authuser_opcallback(disorder_eclient
*c
,
566 struct operation
*op
) {
569 D(("authuser_opcallback"));
570 if(c
->rc
/ 100 != 2) {
571 /* Wrong password or something. We cannot proceed. */
572 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
573 disorder_eclient_close(c
);
576 /* OK, we're authenticated now. */
577 c
->authenticated
= 1;
578 byte_xasprintf(&r
, "authenticated with %s", c
->ident
);
579 c
->callbacks
->report(c
->u
, r
);
580 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
581 /* We are a log client, switch to logging mode */
582 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
586 /* Output ********************************************************************/
588 /* Chop N bytes off the front of a dynstr */
589 static void consume(struct dynstr
*d
, int n
) {
590 D(("consume %d", n
));
591 assert(d
->nvec
>= n
);
592 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
596 /* Write some bytes */
597 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
598 D(("put %d %.*s", c
->fd
, (int)n
, s
));
599 dynstr_append_bytes(&c
->output
, s
, n
);
602 /* Called when we can write to our FD, or at any other time */
603 static void send_output(disorder_eclient
*c
) {
606 D(("send_output %d bytes pending", c
->output
.nvec
));
607 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
608 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
615 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
619 consume(&c
->output
, n
);
623 /* Input *********************************************************************/
625 /* Called when c->fd might be readable, or at any other time */
626 static void read_input(disorder_eclient
*c
) {
631 D(("read_input in state %s", states
[c
->state
]));
632 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
633 /* read some more input */
634 n
= read(c
->fd
, buffer
, sizeof buffer
);
641 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
644 return; /* no new input to process */
646 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
647 dynstr_append_bytes(&c
->input
, buffer
, n
);
650 /* might have more than one line to process */
651 while(c
->state
> state_connecting
652 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
653 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
654 /* we might have disconnected along the way, which zogs the input buffer */
655 if(c
->state
> state_connecting
)
656 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
659 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
660 c
->authenticated
= 0;
664 /* called with a line that has just been read */
665 static void process_line(disorder_eclient
*c
, char *line
) {
666 D(("process_line %d [%s]", c
->fd
, line
));
668 case state_cmdresponse
:
669 /* This is the first line of a response */
670 if(!(line
[0] >= '0' && line
[0] <= '9'
671 && line
[1] >= '0' && line
[1] <= '9'
672 && line
[2] >= '0' && line
[2] <= '9'
674 fatal(0, "invalid response from server: %s", line
);
675 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
679 /* We need to collect the body. */
680 c
->state
= state_body
;
681 vector_init(&c
->vec
);
684 assert(c
->log_callbacks
!= 0);
685 if(c
->log_callbacks
->connected
)
686 c
->log_callbacks
->connected(c
->log_v
);
687 c
->state
= state_log
;
690 /* We've got the whole response. Go into the idle state so the state
691 * machine knows we're done and then call the operation callback. */
697 if(strcmp(line
, ".")) {
698 /* A line from the body */
699 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
701 /* End of the body. */
702 vector_terminate(&c
->vec
);
707 if(strcmp(line
, ".")) {
708 logline(c
, line
+ (line
[0] == '.'));
713 assert(!"wrong state for location");
718 /* Called when an operation completes */
719 static void complete(disorder_eclient
*c
) {
720 struct operation
*op
;
723 /* Pop the operation off the queue */
726 if(c
->opstail
== &op
->next
)
727 c
->opstail
= &c
->ops
;
728 /* If we've pipelined a command ahead then we go straight to cmdresponser.
729 * Otherwise we go to idle, which will arrange further sends. */
730 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
731 op
->opcallback(c
, op
);
732 /* Note that we always call the opcallback even on error, though command
733 * opcallbacks generally always do the same error handling, i.e. just call
734 * protocol_error(). It's the auth* opcallbacks that have different
738 /* Operation setup ***********************************************************/
740 static void stash_command_vector(disorder_eclient
*c
,
742 operation_callback
*opcallback
,
747 struct operation
*op
= xmalloc(sizeof *op
);
753 for(n
= 0; n
< ncmd
; ++n
) {
755 dynstr_append(&d
, ' ');
756 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
758 dynstr_append(&d
, '\n');
759 dynstr_terminate(&d
);
762 op
->cmd
= 0; /* usually, awaiting challenge */
763 op
->opcallback
= opcallback
;
764 op
->completed
= completed
;
768 assert(op
->sent
== 0);
770 /* Authentication operations jump the queue of useful commands */
773 if(c
->opstail
== &c
->ops
)
774 c
->opstail
= &op
->next
;
775 for(op
= c
->ops
; op
; op
= op
->next
)
779 c
->opstail
= &op
->next
;
783 static void vstash_command(disorder_eclient
*c
,
785 operation_callback
*opcallback
,
788 const char *cmd
, va_list ap
) {
792 D(("vstash_command %s", cmd ? cmd
: "NULL"));
795 vector_append(&vec
, (char *)cmd
);
796 while((arg
= va_arg(ap
, char *)))
797 vector_append(&vec
, arg
);
798 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
801 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
, 0, 0);
804 static void stash_command(disorder_eclient
*c
,
806 operation_callback
*opcallback
,
814 vstash_command(c
, queuejump
, opcallback
, completed
, v
, cmd
, ap
);
818 /* Command support ***********************************************************/
820 /* for commands with a simple string response */
821 static void string_response_opcallback(disorder_eclient
*c
,
822 struct operation
*op
) {
823 D(("string_response_callback"));
824 if(c
->rc
/ 100 == 2) {
826 ((disorder_eclient_string_response
*)op
->completed
)(op
->v
, c
->line
+ 4);
828 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
831 /* for commands with a simple integer response */
832 static void integer_response_opcallback(disorder_eclient
*c
,
833 struct operation
*op
) {
834 D(("string_response_callback"));
835 if(c
->rc
/ 100 == 2) {
837 ((disorder_eclient_integer_response
*)op
->completed
)
838 (op
->v
, strtol(c
->line
+ 4, 0, 10));
840 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
843 /* for commands with no response */
844 static void no_response_opcallback(disorder_eclient
*c
,
845 struct operation
*op
) {
846 D(("no_response_callback"));
847 if(c
->rc
/ 100 == 2) {
849 ((disorder_eclient_no_response
*)op
->completed
)(op
->v
);
851 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
854 /* error callback for queue_unmarshall */
855 static void eclient_queue_error(const char *msg
,
857 struct operation
*op
= u
;
859 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
862 /* for commands that expect a queue dump */
863 static void queue_response_opcallback(disorder_eclient
*c
,
864 struct operation
*op
) {
866 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
868 D(("queue_response_callback"));
869 if(c
->rc
/ 100 == 2) {
870 /* parse the queue */
871 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
872 q
= xmalloc(sizeof *q
);
873 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
874 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], eclient_queue_error
, op
)) {
882 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, qh
);
884 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
888 static void playing_response_opcallback(disorder_eclient
*c
,
889 struct operation
*op
) {
890 struct queue_entry
*q
;
892 D(("playing_response_callback"));
893 if(c
->rc
/ 100 == 2) {
896 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
897 eclient_queue_error
, c
))
904 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
908 ((disorder_eclient_queue_response
*)op
->completed
)(op
->v
, q
);
910 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
913 /* for commands that expect a list of some sort */
914 static void list_response_opcallback(disorder_eclient
*c
,
915 struct operation
*op
) {
916 D(("list_response_callback"));
917 if(c
->rc
/ 100 == 2) {
919 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
,
923 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
927 static void volume_response_opcallback(disorder_eclient
*c
,
928 struct operation
*op
) {
931 D(("volume_response_callback"));
932 if(c
->rc
/ 100 == 2) {
934 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
935 protocol_error(c
, op
, -1, "%s: invalid volume response: %s",
938 ((disorder_eclient_volume_response
*)op
->completed
)(op
->v
, l
, r
);
941 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
944 static int simple(disorder_eclient
*c
,
945 operation_callback
*opcallback
,
948 const char *cmd
, ...) {
952 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, cmd
, ap
);
954 /* Give the state machine a kick, since we might be in state_idle */
955 disorder_eclient_polled(c
, 0);
959 /* Commands ******************************************************************/
961 int disorder_eclient_version(disorder_eclient
*c
,
962 disorder_eclient_string_response
*completed
,
964 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
965 "version", (char *)0);
968 int disorder_eclient_namepart(disorder_eclient
*c
,
969 disorder_eclient_string_response
*completed
,
974 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
975 "part", track
, context
, part
, (char *)0);
978 int disorder_eclient_play(disorder_eclient
*c
,
980 disorder_eclient_no_response
*completed
,
982 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
983 "play", track
, (char *)0);
986 int disorder_eclient_pause(disorder_eclient
*c
,
987 disorder_eclient_no_response
*completed
,
989 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
993 int disorder_eclient_resume(disorder_eclient
*c
,
994 disorder_eclient_no_response
*completed
,
996 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
997 "resume", (char *)0);
1000 int disorder_eclient_scratch(disorder_eclient
*c
,
1002 disorder_eclient_no_response
*completed
,
1004 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1005 "scratch", id
, (char *)0);
1008 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
1009 disorder_eclient_no_response
*completed
,
1011 return disorder_eclient_scratch(c
, 0, completed
, v
);
1014 int disorder_eclient_remove(disorder_eclient
*c
,
1016 disorder_eclient_no_response
*completed
,
1018 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1019 "remove", id
, (char *)0);
1022 int disorder_eclient_moveafter(disorder_eclient
*c
,
1026 disorder_eclient_no_response
*completed
,
1032 vector_append(&vec
, (char *)"moveafter");
1033 vector_append(&vec
, (char *)target
);
1034 for(n
= 0; n
< nids
; ++n
)
1035 vector_append(&vec
, (char *)ids
[n
]);
1036 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
1038 disorder_eclient_polled(c
, 0);
1042 int disorder_eclient_recent(disorder_eclient
*c
,
1043 disorder_eclient_queue_response
*completed
,
1045 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1046 "recent", (char *)0);
1049 int disorder_eclient_queue(disorder_eclient
*c
,
1050 disorder_eclient_queue_response
*completed
,
1052 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1053 "queue", (char *)0);
1056 int disorder_eclient_files(disorder_eclient
*c
,
1057 disorder_eclient_list_response
*completed
,
1061 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1062 "files", dir
, re
, (char *)0);
1065 int disorder_eclient_dirs(disorder_eclient
*c
,
1066 disorder_eclient_list_response
*completed
,
1070 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1071 "dirs", dir
, re
, (char *)0);
1074 int disorder_eclient_playing(disorder_eclient
*c
,
1075 disorder_eclient_queue_response
*completed
,
1077 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1078 "playing", (char *)0);
1081 int disorder_eclient_length(disorder_eclient
*c
,
1082 disorder_eclient_integer_response
*completed
,
1085 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1086 "length", track
, (char *)0);
1089 int disorder_eclient_volume(disorder_eclient
*c
,
1090 disorder_eclient_volume_response
*completed
,
1093 char sl
[64], sr
[64];
1095 if(l
< 0 && r
< 0) {
1096 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1097 "volume", (char *)0);
1098 } else if(l
>= 0 && r
>= 0) {
1101 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1102 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1103 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1104 "volume", sl
, sr
, (char *)0);
1106 assert(!"invalid arguments to disorder_eclient_volume");
1107 return -1; /* gcc is being dim */
1111 int disorder_eclient_enable(disorder_eclient
*c
,
1112 disorder_eclient_no_response
*completed
,
1114 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1115 "enable", (char *)0);
1118 int disorder_eclient_disable(disorder_eclient
*c
,
1119 disorder_eclient_no_response
*completed
,
1121 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1122 "disable", (char *)0);
1125 int disorder_eclient_random_enable(disorder_eclient
*c
,
1126 disorder_eclient_no_response
*completed
,
1128 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1129 "random-enable", (char *)0);
1132 int disorder_eclient_random_disable(disorder_eclient
*c
,
1133 disorder_eclient_no_response
*completed
,
1135 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1136 "random-disable", (char *)0);
1139 int disorder_eclient_get(disorder_eclient
*c
,
1140 disorder_eclient_string_response
*completed
,
1141 const char *track
, const char *pref
,
1143 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1144 "get", track
, pref
, (char *)0);
1147 int disorder_eclient_set(disorder_eclient
*c
,
1148 disorder_eclient_no_response
*completed
,
1149 const char *track
, const char *pref
,
1152 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1153 "set", track
, pref
, value
, (char *)0);
1156 int disorder_eclient_unset(disorder_eclient
*c
,
1157 disorder_eclient_no_response
*completed
,
1158 const char *track
, const char *pref
,
1160 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1161 "unset", track
, pref
, (char *)0);
1164 int disorder_eclient_resolve(disorder_eclient
*c
,
1165 disorder_eclient_string_response
*completed
,
1168 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1169 "resolve", track
, (char *)0);
1172 int disorder_eclient_search(disorder_eclient
*c
,
1173 disorder_eclient_list_response
*completed
,
1176 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1177 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1178 "search", terms
, (char *)0);
1181 int disorder_eclient_nop(disorder_eclient
*c
,
1182 disorder_eclient_no_response
*completed
,
1184 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1188 /** @brief Get the last @p max added tracks
1190 * @param completed Called with list
1191 * @param max Number of tracks to get, 0 for all
1192 * @param v Passed to @p completed
1194 * The first track in the list is the most recently added.
1196 int disorder_eclient_new_tracks(disorder_eclient
*c
,
1197 disorder_eclient_list_response
*completed
,
1202 sprintf(limit
, "%d", max
);
1203 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1204 "new", limit
, (char *)0);
1207 static void rtp_response_opcallback(disorder_eclient
*c
,
1208 struct operation
*op
) {
1209 D(("rtp_response_opcallback"));
1210 if(c
->rc
/ 100 == 2) {
1213 char **vec
= split(c
->line
+ 4, &nvec
, SPLIT_QUOTES
, 0, 0);
1215 ((disorder_eclient_list_response
*)op
->completed
)(op
->v
, nvec
, vec
);
1218 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
1221 /** @brief Determine the RTP target address
1223 * @param completed Called with address details
1224 * @param v Passed to @p completed
1226 * The address details will be two elements, the first being the hostname and
1227 * the second the service (port).
1229 int disorder_eclient_rtp_address(disorder_eclient
*c
,
1230 disorder_eclient_list_response
*completed
,
1232 return simple(c
, rtp_response_opcallback
, (void (*)())completed
, v
,
1233 "rtp-address", (char *)0);
1236 /* Log clients ***************************************************************/
1238 /** @brief Monitor the server log
1240 * @param callbacks Functions to call when anything happens
1241 * @param v Passed to @p callbacks functions
1243 * Once a client is being used for logging it cannot be used for anything else.
1244 * There is magic in authuser_opcallback() to re-submit the @c log command
1245 * after reconnection.
1247 * NB that the @c state callback may be called from within this function,
1248 * i.e. not solely later on from the event loop callback.
1250 int disorder_eclient_log(disorder_eclient
*c
,
1251 const disorder_eclient_log_callbacks
*callbacks
,
1253 if(c
->log_callbacks
) return -1;
1254 c
->log_callbacks
= callbacks
;
1256 /* Repoort initial state */
1257 if(c
->log_callbacks
->state
)
1258 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1259 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1264 /* If we get here we've stopped being a log client */
1265 static void log_opcallback(disorder_eclient
*c
,
1266 struct operation
attribute((unused
)) *op
) {
1267 D(("log_opcallback"));
1268 c
->log_callbacks
= 0;
1272 /* error callback for log line parsing */
1273 static void logline_error(const char *msg
, void *u
) {
1274 disorder_eclient
*c
= u
;
1275 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1278 /* process a single log line */
1279 static void logline(disorder_eclient
*c
, const char *line
) {
1284 D(("logline [%s]", line
));
1285 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1286 if(nvec
< 2) return; /* probably an error, already
1288 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1289 /* probably the wrong side of a format change */
1290 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1293 /* TODO: do something with the time */
1294 n
= TABLE_FIND(logentry_handlers
, struct logentry_handler
, name
, vec
[1]);
1295 if(n
< 0) return; /* probably a future command */
1298 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
)
1300 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1303 static void logentry_completed(disorder_eclient
*c
,
1304 int attribute((unused
)) nvec
, char **vec
) {
1305 if(!c
->log_callbacks
->completed
) return;
1306 c
->statebits
&= ~DISORDER_PLAYING
;
1307 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1308 if(c
->log_callbacks
->state
)
1309 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1312 static void logentry_failed(disorder_eclient
*c
,
1313 int attribute((unused
)) nvec
, char **vec
) {
1314 if(!c
->log_callbacks
->failed
)return;
1315 c
->statebits
&= ~DISORDER_PLAYING
;
1316 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1317 if(c
->log_callbacks
->state
)
1318 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1321 static void logentry_moved(disorder_eclient
*c
,
1322 int attribute((unused
)) nvec
, char **vec
) {
1323 if(!c
->log_callbacks
->moved
) return;
1324 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1327 static void logentry_playing(disorder_eclient
*c
,
1328 int attribute((unused
)) nvec
, char **vec
) {
1329 if(!c
->log_callbacks
->playing
) return;
1330 c
->statebits
|= DISORDER_PLAYING
;
1331 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1332 if(c
->log_callbacks
->state
)
1333 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1336 static void logentry_queue(disorder_eclient
*c
,
1337 int attribute((unused
)) nvec
, char **vec
) {
1338 struct queue_entry
*q
;
1340 if(!c
->log_callbacks
->completed
) return;
1341 q
= xmalloc(sizeof *q
);
1342 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1344 c
->log_callbacks
->queue(c
->log_v
, q
);
1347 static void logentry_recent_added(disorder_eclient
*c
,
1348 int attribute((unused
)) nvec
, char **vec
) {
1349 struct queue_entry
*q
;
1351 if(!c
->log_callbacks
->recent_added
) return;
1352 q
= xmalloc(sizeof *q
);
1353 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1355 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1358 static void logentry_recent_removed(disorder_eclient
*c
,
1359 int attribute((unused
)) nvec
, char **vec
) {
1360 if(!c
->log_callbacks
->recent_removed
) return;
1361 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1364 static void logentry_removed(disorder_eclient
*c
,
1365 int attribute((unused
)) nvec
, char **vec
) {
1366 if(!c
->log_callbacks
->removed
) return;
1367 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1370 static void logentry_rescanned(disorder_eclient
*c
,
1371 int attribute((unused
)) nvec
,
1372 char attribute((unused
)) **vec
) {
1373 if(!c
->log_callbacks
->rescanned
) return;
1374 c
->log_callbacks
->rescanned(c
->log_v
);
1377 static void logentry_scratched(disorder_eclient
*c
,
1378 int attribute((unused
)) nvec
, char **vec
) {
1379 if(!c
->log_callbacks
->scratched
) return;
1380 c
->statebits
&= ~DISORDER_PLAYING
;
1381 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1382 if(c
->log_callbacks
->state
)
1383 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1386 static const struct {
1389 const char *disable
;
1390 } statestrings
[] = {
1391 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1392 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1393 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1394 { DISORDER_PLAYING
, "playing", "completed" },
1395 { DISORDER_PLAYING
, 0, "scratched" },
1396 { DISORDER_PLAYING
, 0, "failed" },
1398 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1400 static void logentry_state(disorder_eclient
*c
,
1401 int attribute((unused
)) nvec
, char **vec
) {
1404 for(n
= 0; n
< NSTATES
; ++n
)
1405 if(statestrings
[n
].enable
&& !strcmp(vec
[0], statestrings
[n
].enable
)) {
1406 c
->statebits
|= statestrings
[n
].bit
;
1408 } else if(statestrings
[n
].disable
&& !strcmp(vec
[0], statestrings
[n
].disable
)) {
1409 c
->statebits
&= ~statestrings
[n
].bit
;
1412 if(!c
->log_callbacks
->state
) return;
1413 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1416 static void logentry_volume(disorder_eclient
*c
,
1417 int attribute((unused
)) nvec
, char **vec
) {
1420 if(!c
->log_callbacks
->volume
) return;
1421 if(xstrtol(&l
, vec
[0], 0, 10)
1422 || xstrtol(&r
, vec
[1], 0, 10)
1423 || l
< 0 || l
> INT_MAX
1424 || r
< 0 || r
> INT_MAX
)
1426 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1429 /** @brief Convert @p statebits to a string */
1430 char *disorder_eclient_interpret_state(unsigned long statebits
) {
1434 static const struct {
1438 { DISORDER_PLAYING_ENABLED
, "playing_enabled" },
1439 { DISORDER_RANDOM_ENABLED
, "random_enabled" },
1440 { DISORDER_TRACK_PAUSED
, "track_paused" },
1441 { DISORDER_PLAYING
, "playing" },
1442 { DISORDER_CONNECTED
, "connected" },
1444 #define NBITS (sizeof bits / sizeof *bits)
1448 dynstr_append(d
, '0');
1449 for(n
= 0; n
< NBITS
; ++n
)
1450 if(statebits
& bits
[n
].bit
) {
1452 dynstr_append(d
, '|');
1453 dynstr_append_string(d
, bits
[n
].name
);
1454 statebits
^= bits
[n
].bit
;
1460 dynstr_append(d
, '|');
1461 sprintf(s
, "%#lx", statebits
);
1462 dynstr_append_string(d
, s
);
1464 dynstr_terminate(d
);
1473 indent-tabs-mode:nil