Browse Source

Add Update packet and signal when initial route updates are done.

master
Jeroen Vreeken 10 months ago
parent
commit
64baec472b
7 changed files with 103 additions and 106 deletions
  1. +4
    -0
      dml/dml_host.h
  2. +6
    -0
      dml/dml_packet.h
  3. +27
    -0
      dml_host.c
  4. +41
    -106
      dml_list.c
  5. +23
    -0
      dml_packet.c
  6. +1
    -0
      dml_stream.c
  7. +1
    -0
      dmld.c

+ 4
- 0
dml/dml_host.h View File

@ -19,6 +19,7 @@
#define _INCLUDE_DML_HOST_H_
#include "dml_stream.h"
#include <dml/dml_packet.h>
struct dml_host;
@ -28,6 +29,8 @@ struct dml_connection *dml_host_connection_get(struct dml_host *host);
int dml_host_connection_closed_cb_set(struct dml_host *host,
void(*cb)(struct dml_host *host, void *arg), void *arg);
int dml_host_update_cb_set(struct dml_host *host,
void(*cb)(struct dml_host *host, uint32_t flags, void *arg), void *arg);
int dml_host_stream_added_cb_set(struct dml_host *host,
void(*cb)(struct dml_host *host, struct dml_stream *ds, void *arg), void *arg);
int dml_host_stream_removed_cb_set(struct dml_host *host,
@ -41,6 +44,7 @@ int dml_host_stream_req_reverse_connect_cb_set(struct dml_host *host,
int dml_host_stream_req_reverse_disconnect_cb_set(struct dml_host *host,
void (*cb)(struct dml_host *host, struct dml_stream *ds, struct dml_stream *ds_rev, int status, void *arg), void *arg);
/* Filter on mime types: use nr=0, mimetypes = NULL to pass all */
int dml_host_mime_filter_set(struct dml_host *host, int nr, char **mimetypes);
bool dml_host_mime_filter(struct dml_host *host, struct dml_stream *ds);


+ 6
- 0
dml/dml_packet.h View File

@ -31,6 +31,8 @@
#define DML_PACKET_HELLO_LEAF 1
#define DML_PACKET_HELLO_UPDATES 2
#define DML_PACKET_UPDATE_INITIAL_DONE 1
#define DML_PACKET_DISC_UNROUTABLE 1
#define DML_PACKET_DISC_REQUESTED 2
@ -47,6 +49,7 @@ enum dml_packet_id {
DML_PACKET_HEADER = 4,
DML_PACKET_CONNECT = 5,
DML_PACKET_DISC = 6,
DML_PACKET_UPDATE = 7,
DML_PACKET_REQ_DESCRIPTION = 34,
DML_PACKET_REQ_CERTIFICATE = 35,
@ -61,6 +64,9 @@ enum dml_packet_id {
int dml_packet_send_hello(struct dml_connection *dc, uint32_t flags, char *ident);
int dml_packet_parse_hello(uint8_t *data, uint16_t len, uint32_t *flags, char **ident);
int dml_packet_send_update(struct dml_connection *dc, uint32_t flags);
int dml_packet_parse_update(uint8_t *data, uint16_t len, uint32_t *flags);
int dml_packet_send_description(struct dml_connection *dc,
uint8_t id[DML_ID_SIZE], uint8_t version, uint32_t bps, char *mime,
char *name, char *alias, char *description);


+ 27
- 0
dml_host.c View File

@ -36,6 +36,9 @@ struct dml_host {
void (*connection_closed_cb)(struct dml_host *host, void *arg);
void *connection_closed_cb_arg;
void (*update_cb)(struct dml_host *host, uint32_t flags, void *arg);
void *update_cb_arg;
void (*stream_added_cb)(struct dml_host *host, struct dml_stream *ds, void *arg);
void *stream_added_cb_arg;
@ -60,6 +63,10 @@ bool dml_host_mime_filter(struct dml_host *host, struct dml_stream *ds)
char *dmime = dml_stream_mime_get(ds);
int i;
/* exception: no filter, pass all */
if (!host->mime_filter_nr)
return true;
for (i = 0; i < host->mime_filter_nr; i++) {
if (!strcmp(host->mime_filter[i], dmime)) {
return true;
@ -74,6 +81,17 @@ static void rx_packet(struct dml_connection *dc, void *arg,
struct dml_host *host = arg;
switch(id) {
case DML_PACKET_UPDATE: {
uint32_t flags;
if (dml_packet_parse_update(data, len, &flags))
break;
if (host->update_cb)
host->update_cb(host, flags, host->update_cb_arg);
break;
}
case DML_PACKET_ROUTE: {
uint8_t hops;
uint8_t rid[DML_ID_SIZE];
@ -435,6 +453,15 @@ int dml_host_mime_filter_set(struct dml_host *host, int nr, char **mimetypes)
return 0;
}
int dml_host_update_cb_set(struct dml_host *host,
void(*cb)(struct dml_host *host, uint32_t flags, void *arg), void *arg)
{
host->update_cb = cb;
host->update_cb_arg = arg;
return 0;
}
int dml_host_stream_added_cb_set(struct dml_host *host,
void(*cb)(struct dml_host *host, struct dml_stream *ds, void *arg), void *arg)
{


+ 41
- 106
dml_list.c View File

@ -15,9 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dml/dml_client.h>
#include <dml/dml_connection.h>
#include <dml/dml_packet.h>
#include <dml/dml_host.h>
#include <dml/dml.h>
#include <dml/dml_id.h>
@ -26,129 +24,66 @@
#include <inttypes.h>
#include <string.h>
struct info {
uint8_t id[DML_ID_SIZE];
struct info *next;
};
struct info *info_list;
static GMainLoop *loop;
static int nr_ids = 0;
static int nr_added = 0;
int info_add(uint8_t id[DML_ID_SIZE])
static void stream_added_cb(struct dml_host *host, struct dml_stream *ds, void *arg)
{
struct info *entry;
char *idstr = dml_id_str(dml_stream_id_get(ds));
char *mime, *name, *alias, *description;
uint32_t bps;
for (entry = info_list; entry; entry = entry->next) {
if (!memcmp(entry->id, id, DML_ID_SIZE))
return -1;
}
mime = dml_stream_mime_get(ds);
name = dml_stream_name_get(ds);
alias = dml_stream_alias_get(ds);
description = dml_stream_description_get(ds);
bps = dml_stream_bps_get(ds);
entry = calloc(1, sizeof(struct info));
memcpy(entry->id, id, DML_ID_SIZE);
entry->next = info_list;
info_list = entry;
printf("id: %s\n\tmime: '%s'\n\tbps: %d\n\tname: '%s'\n"
"\talias: '%s'\n\tdescription: '%s'\n",
idstr, mime, bps, name, alias, description);
return 0;
}
void rx_packet(struct dml_connection *dc, void *arg,
uint16_t id, uint16_t len, uint8_t *data)
{
switch (id) {
case DML_PACKET_HELLO: {
char *ident;
uint32_t flags;
dml_packet_parse_hello(data, len, &flags, &ident);
printf("ident: '%s' flags: %08"PRIx32"\n", ident, flags);
free(ident);
break;
}
case DML_PACKET_ROUTE: {
uint8_t id[DML_ID_SIZE];
uint8_t hops;
dml_packet_parse_route(data, len, id, &hops);
char *idstr = dml_id_str(id);
printf("id: %s hops: %d\n", idstr, hops);
free(idstr);
if (!info_add(id)) {
dml_packet_send_req_description(dc, id);
}
break;
}
case DML_PACKET_DESCRIPTION: {
uint8_t desc_id[DML_ID_SIZE];
uint8_t version;
uint32_t bps;
char *mime, *name, *alias, *description;
dml_packet_parse_description(data, len, desc_id, &version,
&bps, &mime, &name, &alias, &description);
char *idstr = dml_id_str(desc_id);
uint8_t hash_id[DML_ID_SIZE];
dml_id_gen(hash_id, version, bps, mime, name, alias,
description);
bool hash_match = !memcmp(hash_id, desc_id, DML_ID_SIZE);
printf("id: %s\n\tmime: '%s'\n\tbps: %d\n\tname: '%s'\n"
"\talias: '%s'\n\tdescription: '%s'\n"
"\thash ok: %d\n",
idstr, mime, bps, name, alias, description,
hash_match);
free(idstr);
free(mime);
free(name);
free(alias);
free(description);
}
default:
break;
free(idstr);
nr_added++;
if (nr_added == nr_ids) {
g_main_loop_quit(loop);
}
return;
}
int client_connection_close(struct dml_connection *dc, void *arg)
{
//TODO timeout and reconnect!
return dml_connection_destroy(dc);
}
void client_connect(struct dml_client *client, void *arg)
void update_cb(struct dml_host *host, uint32_t flags, void *arg)
{
struct dml_connection *dc;
int fd;
fd = dml_client_fd_get(client);
if (flags == DML_PACKET_UPDATE_INITIAL_DONE) {
struct dml_stream *entry;
for (entry = NULL; (entry = dml_stream_iterate(entry)); ) {
nr_ids++;
}
dc = dml_connection_create(fd, client, rx_packet, client_connection_close);
dml_packet_send_hello(dc, DML_PACKET_HELLO_UPDATES, "dml_list " DML_VERSION);
printf("Received %d IDs, retrieving stream descriptions\n", nr_ids);
}
}
int main(int argc, char **argv)
{
struct dml_client *dc;
struct dml_host *host;
char *host = "localhost";
unsigned short port = 0;
char *server = "localhost";
if (argc > 1)
host = argv[1];
if (argc > 2)
port = atoi(argv[2]);
server = argv[1];
dc = dml_client_create(host, port, client_connect, NULL);
if (dml_client_connect(dc)) {
perror("Could not connect to server");
host = dml_host_create(server);
if (!host) {
printf("Could not create host\n");
return -1;
}
g_main_loop_run(g_main_loop_new(NULL, false));
dml_host_update_cb_set(host, update_cb, NULL);
dml_host_stream_added_cb_set(host, stream_added_cb, NULL);
loop = g_main_loop_new(NULL, false);
g_main_loop_run(loop);
return 0;
}

+ 23
- 0
dml_packet.c View File

@ -54,6 +54,29 @@ int dml_packet_parse_hello(uint8_t *data, uint16_t len, uint32_t *flags, char **
return 0;
}
int dml_packet_send_update(struct dml_connection *dc, uint32_t flags)
{
uint8_t data[4];
data[0] = (flags >> 24) & 0xff;
data[1] = (flags >> 16) & 0xff;
data[2] = (flags >> 8) & 0xff;
data[3] = (flags >> 0) & 0xff;
return dml_connection_send(dc, data, DML_PACKET_UPDATE, 4);
}
int dml_packet_parse_update(uint8_t *data, uint16_t len, uint32_t *flags)
{
if (len < 4)
return -1;
if (flags)
*flags = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
return 0;
}
int dml_packet_send_description(struct dml_connection *dc,
uint8_t id[DML_ID_SIZE], uint8_t version, uint32_t bps, char *mime,
char *name, char *alias, char *description)


+ 1
- 0
dml_stream.c View File

@ -172,6 +172,7 @@ struct dml_stream *dml_stream_update_description(uint8_t *data, uint16_t len, bo
stream->alias = alias;
stream->description = description;
stream->mime = mime;
stream->bps = bps;
return stream;


+ 1
- 0
dmld.c View File

@ -421,6 +421,7 @@ gboolean update_all(void *arg)
// printf("r: %d\n", r);
if (r) {
// printf("switch to regular updates %p\n", con);
dml_packet_send_update(con->dc, DML_PACKET_UPDATE_INITIAL_DONE);
g_timeout_add_seconds(1, update, con);
return 0;
}


Loading…
Cancel
Save