Skip to content

Commit

Permalink
Implement dequeue function and send then in serial line. #1 #2
Browse files Browse the repository at this point in the history
Shortcomings: This seems to block on long blocks and transactions and
it sends string "empty" on wire even if there is nothing to
send. Also, no synchronization or block compaction is performed.
  • Loading branch information
zouppen committed Feb 26, 2014
1 parent 0dae39e commit b173b75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/bitcoin.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ bool bitcoin_inv_insert(struct bitcoin_storage const *st, struct msg *const m)
return true;
}

const struct msg *bitcoin_dequeue(struct bitcoin_storage const *st)
{
// Fetch and dequeue key
GSequenceIter *it = g_sequence_get_begin_iter(st->send_queue);
guchar *key = g_sequence_get(it);
g_sequence_remove(it);

// Fetch value (or NULL in case of failure)
return g_hash_table_lookup(st->inv,key);
}

/**
* Calculates a hash for use internally in Hash Table storage. This
* hash should not be used outside g_hash_table.
Expand Down
5 changes: 5 additions & 0 deletions src/bitcoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ struct bitcoin_storage bitcoin_new_storage();
*/
bool bitcoin_inv_insert(struct bitcoin_storage const *st, struct msg *const m);

/**
* Fetches unsent message with highest sending priority.
*/
const struct msg *bitcoin_dequeue(struct bitcoin_storage const *st);

/**
* Returns message type of given wire message.
*/
Expand Down
38 changes: 24 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "bitcoin.h"

// Prototypes
void serial(const int fd);
void serial(const int devfd, struct bitcoin_storage *const st);

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -73,21 +73,31 @@ int main(int argc, char *argv[])
if (ret < 1) err(5,"Error while polling");

// Always serve slow serial first
if (fds[0].revents & POLLOUT) serial(dev_fd);
if (fds[0].revents & POLLOUT) serial(dev_fd,&st);
if (fds[1].revents & POLLIN) incoming_node_data(node_fd,&st);
}
}

void serial(const int devfd) {
// Just dummy writer for generating much traffic to serial
static guint8 i=0;
char instanssi[43];
snprintf(instanssi,sizeof(instanssi),
"Kissa kissa kissa kissa... Instanssi! %3hhu\n",
i++);

// Write the output. Do not care if some output is ignored.
const int ret = write(devfd,instanssi,sizeof(instanssi)-1);
if (ret < 1) err(4,"Unable to write to serial port");
printf("sent %d bytes to serial port\n",ret);
void serial(const int devfd, struct bitcoin_storage *const st)
{
gint size = g_sequence_get_length(st->send_queue);

if (size==0) {
const char buf[] = "empty ";
const int ret = write(devfd,buf,sizeof(buf)-1);
if (ret < 1) err(4,"Unable to write to serial port");
} else {
const struct msg *m = bitcoin_dequeue(st);

printf("Sending %s %s, queue size is %d\n",
bitcoin_type_str(m),
hex256(bitcoin_inv_hash(m)),
size);

// FIXME no two writes inside single call!
const int ret1 = write(devfd,&m->type,1); // FIXME byte ordering issue
if (ret1 < 1) err(4,"Unable to write to serial port");
const int ret2 = write(devfd,m->payload,m->length);
if (ret2 < 1) err(4,"Unable to write to serial port");
}
}

0 comments on commit b173b75

Please sign in to comment.