Merge branch 'switch' into edge
[clinton/Smoothieware.git] / src / libs / Network / uip / CommandQueue.cpp
CommitLineData
d4ee6ee2
JM
1#include "CommandQueue.h"
2
3#include "stdio.h"
4#include "string.h"
5#include "stdlib.h"
6
7#include "Kernel.h"
8#include "libs/SerialMessage.h"
9#include "CallbackStream.h"
10
11static CommandQueue *command_queue_instance;
12CommandQueue *CommandQueue::instance = NULL;
13
14
15CommandQueue::CommandQueue()
16{
17 command_queue_instance = this;
18 null_stream= &(StreamOutput::NullStream);
19}
20
21CommandQueue* CommandQueue::getInstance()
22{
23 if(instance == 0) instance= new CommandQueue();
24 return instance;
25}
26
27extern "C" {
28 int network_add_command(const char *cmd, void *pstream)
29 {
30 return command_queue_instance->add(cmd, (StreamOutput*)pstream);
31 }
32}
33
34int CommandQueue::add(const char *cmd, StreamOutput *pstream)
35{
36 cmd_t c= {strdup(cmd), pstream==NULL?null_stream:pstream};
37 q.push(c);
38 if(pstream != NULL) {
39 // count how many times this is on the queue
40 CallbackStream *s= static_cast<CallbackStream *>(pstream);
41 s->inc();
42 }
43 return q.size();
44}
45
46// pops the next command off the queue and submits it.
47bool CommandQueue::pop()
48{
49 if (q.size() == 0) return false;
50
51 cmd_t c= q.pop();
52 char *cmd= c.str;
53
54 struct SerialMessage message;
55 message.message = cmd;
56 message.stream = c.pstream;
57
58 free(cmd);
59 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
60
61 if(message.stream != null_stream) {
62 message.stream->puts(NULL); // indicates command is done
63 // decrement usage count
64 CallbackStream *s= static_cast<CallbackStream *>(message.stream);
65 s->dec();
66 }
67 return true;
68}