Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

more C++ updates

  • Loading branch information...
commit e40aae225d878806dc874d13a5a79724252b6232 1 parent 47037ed
Michael Moon authored June 18, 2012
36  controller/C++/TCPClient.cpp
... ...
@@ -1,10 +1,15 @@
1 1
 #include "TCPClient.hpp"
2 2
 
  3
+namespace C {
  4
+	extern "C" int printf(const char *format, ...);
  5
+}
  6
+
3 7
 TCPClient::Command TCPClient::commands[] = {
4 8
 	{ "list printers",	&TCPClient::cmd_list_printers },
5 9
 	{ "add printer",	&TCPClient::cmd_add_printer },
6 10
 	{ "exit",			&TCPClient::cmd_exit },
7 11
 	{ "shutdown",		&TCPClient::cmd_shutdown },
  12
+	{ "use printer",	&TCPClient::cmd_use_printer },
8 13
 	{ NULL,				NULL }
9 14
 };
10 15
 
@@ -58,7 +63,7 @@ void TCPClient::onread(struct SelectFd *selected) {
58 63
 					bodyrmn = 0;
59 64
 					bodycomplete = 0;
60 65
 
61  
-					printf("method: %s\nuri: %s\nprotocol: %s\n", httpdata["method"].c_str(), httpdata["uri"].c_str(), httpdata["protocol"].c_str());
  66
+					C::printf("method: %s\nuri: %s\nprotocol: %s\n", httpdata["method"].c_str(), httpdata["uri"].c_str(), httpdata["protocol"].c_str());
62 67
 				}
63 68
 				else {
64 69
 					process_netrap_request(linebuf, l);
@@ -133,16 +138,16 @@ void TCPClient::onerror(struct SelectFd *selected) {
133 138
 }
134 139
 
135 140
 void TCPClient::process_http_request() {
136  
-	printf("Processing %s request for %s\n", httpdata["method"].c_str(), httpdata["uri"].c_str());
  141
+	C::printf("Processing %s request for %s\n", httpdata["method"].c_str(), httpdata["uri"].c_str());
137 142
 	char writebuf[256];
138 143
 	char *wp = writebuf;
139 144
 	wp += snprintf(wp, (writebuf + 256 - wp), "%s 200 OK\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\n", httpdata["protocol"].c_str());
140 145
 	write(string(writebuf)); wp = writebuf;
141  
-	printf("Headers:\n");
  146
+	C::printf("Headers:\n");
142 147
 	write(string("Headers:\n"));
143 148
 	std::map<string, string>::iterator i;
144 149
 	for (i=httpdata.begin(); i != httpdata.end(); i++) {
145  
-		printf("\t%s:\t%s\n", (*i).first.c_str(), (*i).second.c_str());
  150
+		C::printf("\t%s:\t%s\n", (*i).first.c_str(), (*i).second.c_str());
146 151
 		wp += snprintf(wp, (writebuf + 256 - wp), "\t%s:\t%s\n", (*i).first.c_str(), (*i).second.c_str());
147 152
 		write(string(writebuf)); wp = writebuf;
148 153
 	}
@@ -163,6 +168,10 @@ void TCPClient::process_netrap_request(const char *line, int len) {
163 168
 			return;
164 169
 		}
165 170
 	}
  171
+	// not a command- check if we have a printer
  172
+	if (printer != NULL) {
  173
+		printer->write(this, line, len);
  174
+	}
166 175
 }
167 176
 
168 177
 void TCPClient::process_gcode_request(const char *line, int len) {
@@ -203,6 +212,25 @@ void TCPClient::cmd_add_printer(const char *line, int len) {
203 212
 	printf("Printer \"%s\" created\n", p->name());
204 213
 }
205 214
 
  215
+void TCPClient::cmd_use_printer(const char *line, int len) {
  216
+	const char *nameptr = line + 12;
  217
+	int l;
  218
+	for (l = 0; nameptr[l] > 32 && l < len; l++);
  219
+	char *name = (char *) malloc(l + 1);
  220
+	memcpy(name, nameptr, l);
  221
+	name[l] = 0;
  222
+// 	printf("looking for printer called \"%s\"\n", name);
  223
+	std::list<Printer *>::iterator i = Printer::allprinters.begin();
  224
+	for (; i != Printer::allprinters.end(); i++) {
  225
+// 		printf("Checking \"%s\"\n", (*i)->name());
  226
+		if (strncmp(name, (*i)->name(), l) == 0) {
  227
+// 			printf("Match!\n");
  228
+			printer = (*i);
  229
+			return;
  230
+		}
  231
+	}
  232
+}
  233
+
206 234
 void TCPClient::cmd_exit(const char *line, int len) {
207 235
 	state = TCPCLIENT_STATE_CLOSING;
208 236
 	write("Goodbye\n");
7  controller/C++/TCPClient.hpp
@@ -21,7 +21,7 @@ class TCPClient : public TCPSocket {
21 21
 	void onerror(struct SelectFd *selected);
22 22
 
23 23
 	std::map<string, string> httpdata;
24  
-	
  24
+
25 25
 #define TCPCLIENT_STATE_CLASSIFY 0
26 26
 #define TCPCLIENT_STATE_CLOSING 1
27 27
 #define TCPCLIENT_STATE_HTTPHEADER 2
@@ -39,18 +39,19 @@ class TCPClient : public TCPSocket {
39 39
 	void process_http_request();
40 40
 	void process_netrap_request(const char *line, int len);
41 41
 	void process_gcode_request(const char *line, int len);
42  
-	
  42
+
43 43
 	int printl(const char *str);
44 44
 
45 45
 	struct Command {
46 46
 		const char *command;
47 47
 		void (TCPClient::*func)(const char *line, int len);
48 48
 	};
49  
-	
  49
+
50 50
 	static Command commands[];
51 51
 
52 52
 	void cmd_list_printers(const char *line, int len);
53 53
 	void cmd_add_printer(const char *line, int len);
  54
+	void cmd_use_printer(const char *line, int len);
54 55
 	void cmd_exit(const char *line, int len);
55 56
 	void cmd_shutdown(const char *line, int len);
56 57
 };
17  controller/C++/printer.cpp
@@ -12,6 +12,8 @@ namespace C {
12 12
 	extern "C" int printf(const char *format, ...);
13 13
 }
14 14
 
  15
+#include "gcode.hpp"
  16
+
15 17
 std::list<Printer *> Printer::allprinters;
16 18
 int Printer::allprinters_count;
17 19
 
@@ -58,7 +60,7 @@ int Printer::open(char *port, int baud) {
58 60
 void Printer::init() {
59 61
 	allprinters.push_back(this);
60 62
 	allprinters_count++;
61  
-	
  63
+
62 64
 	_name = (char *) malloc(sizeof(void *) * 2 + 3);
63 65
 	C::printf("%d chars in name %s\n", snprintf(_name, sizeof(void *) * 2 + 3, "%p", this), this->name());
64 66
 
@@ -95,16 +97,27 @@ int Printer::write(string str) {
95 97
 }
96 98
 
97 99
 int Printer::write(const char *str, int len) {
  100
+	float words[32];
  101
+	uint32_t seen;
98 102
 	// TODO: extract target properties from outgoing commands
  103
+	seen = Gcode::parse(str, len, words);
99 104
 	return Socket::write(str, len);
100 105
 }
101 106
 
  107
+int Printer::write(Socket *respondent, const char *str, int len) {
  108
+	this->respondent = respondent;
  109
+	return write(str, len);
  110
+}
  111
+
102 112
 int Printer::read(char *buf, int buflen) {
103 113
 	int r = Socket::read(buf, buflen);
104 114
 	// TODO: extract properties from replies
  115
+	if (respondent) {
  116
+		respondent->write(buf, buflen);
  117
+	}
105 118
 	return r;
106 119
 }
107 120
 
108 121
 int Printer::printercount() {
109 122
 	return allprinters_count;
110  
-}
  123
+}
8  controller/C++/printer.hpp
@@ -13,10 +13,10 @@ class Printer : public Socket {
13 13
 	Printer(int fd);
14 14
 	Printer(char *port, int baud);
15 15
 	~Printer(void);
16  
-	
  16
+
17 17
 	static std::list<Printer *> allprinters;
18 18
 	static int printercount();
19  
-	
  19
+
20 20
 	char *name();
21 21
 	void setname(char *newname);
22 22
 
@@ -33,6 +33,8 @@ class Printer : public Socket {
33 33
 	int write(string str);
34 34
 	int write(const char *str, int len);
35 35
 
  36
+	int write(Socket *respondent, const char *str, int len);
  37
+
36 38
 	int read(char *buffer, int buflen);
37 39
 protected:
38 40
 	char *_name;
@@ -41,6 +43,8 @@ class Printer : public Socket {
41 43
 	map<string, string> properties;
42 44
 	map<string, string> capabilities;
43 45
 
  46
+	Socket *respondent;
  47
+
44 48
 	static int allprinters_count;
45 49
 private:
46 50
 };
20  controller/C++/queuemanager.cpp
@@ -10,6 +10,22 @@ QueueManager::QueueManager(Socket *drain) {
10 10
 QueueManager::~QueueManager() {
11 11
 }
12 12
 
13  
-void QueueManager::setDrain(Socket *drain) {
14  
-	QueueManager::drain = drain;
  13
+void QueueManager::setBehaviours(int behaviours) {
  14
+	behaviour = behaviours;
  15
+}
  16
+
  17
+void QueueManager::addDrain(Socket *drain) {
  18
+	drains.push_back(drain);
  19
+}
  20
+
  21
+void QueueManager::delDrain(Socket *drain) {
  22
+	drains.remove(drain);
  23
+}
  24
+
  25
+void addSocket(Socket *s) {
  26
+	sources.push_back(s);
  27
+}
  28
+
  29
+void delSocket(Socket *s) {
  30
+	sources.remove(s);
15 31
 }
16  controller/C++/queuemanager.hpp
@@ -14,11 +14,19 @@ class QueueManager {
14 14
 	QueueManager();
15 15
 	QueueManager(Socket *drain);
16 16
 	~QueueManager();
17  
-	void setDrain(Socket *drain);
18  
-	void addSocket(Socket *s);
19  
-	void delSocket(Socket *s);
  17
+
  18
+#define BEHAVIOUR_SOURCEPAUSE 1;
  19
+#define BEHAVIOUR_DRAINDROP   2;
  20
+	void setBehaviours(int behaviours);
  21
+
  22
+	void addDrain(Socket *drain);
  23
+	void delDrain(Socket *drain);
  24
+
  25
+	void addSource(Socket *s);
  26
+	void delSource(Socket *s);
20 27
 private:
21  
-	Socket *drain;
  28
+	int behaviour;
  29
+	list<Socket *> drains;
22 30
 	list<Socket *> sources;
23 31
 };
24 32
 
14  controller/C++/socket.cpp
@@ -68,6 +68,20 @@ int Socket::fd() {
68 68
 	return _fd;
69 69
 }
70 70
 
  71
+void Socket::stall(void) {
  72
+	stalled = 1;
  73
+	selector.remove(_fd);
  74
+}
  75
+
  76
+void Socket::unstall(void) {
  77
+	stalled = 0;
  78
+	selector.add(_fd, this);
  79
+}
  80
+
  81
+int Socket::is_stalled(void) {
  82
+	return stalled;
  83
+}
  84
+
71 85
 void Socket::onread(struct SelectFd *selected) {
72 86
 // 	printf("trying to read %d bytes\n", rxbuf->canwrite());
73 87
 	int r = rxbuf->writefromfd(selected->fd, rxbuf->canwrite());
5  controller/C++/socket.hpp
@@ -26,11 +26,16 @@ class Socket : public SelectorEventReceiver {
26 26
 
27 27
 	int read(char *buf, int buflen);
28 28
 
  29
+	void stall(void);
  30
+	void unstall(void);
  31
+	int is_stalled(void);
  32
+
29 33
 	int fd();
30 34
 
31 35
 	const char *toString();
32 36
 protected:
33 37
 	int _fd;
  38
+	int stalled;
34 39
 	timeval opentime;
35 40
 
36 41
 	Selector selector;

0 notes on commit e40aae2

Please sign in to comment.
Something went wrong with that request. Please try again.