#include "tcpclient.hpp"
#include "ctype.h"
+enum AnswerTask {
+ atUndef,
+ atGetBuffer,
+};
+typedef struct {
+ AnswerTask m_task;
+ DynBuffer m_name;
+} Parameters;
+
+void handleAnswer(Parameters& params, DynBuffer& buffer){
+ switch(params.m_task){
+ case atGetBuffer:
+ {
+ DynBuffer name(params.m_name);
+ name.append(".dat");
+ FILE* fp = fopen(name.str(), "wb");
+ fwrite(buffer.str() + 4, 1, buffer.length() - 4, fp);
+ fclose(fp);
+ break;
+ }
+ default:
+ break;
+ }
+}
int toNumber(const char* value, int &length){
int number = 0;
length = 0;
buffer.appendAsLE(count, 4);
buffer.appendAsLE(delay, 4);
}
-static void prepareGetBuffer(const char* bufferName, DynBuffer& buffer){
+static void prepareGetBuffer(const char* bufferName,
+ DynBuffer& buffer, Parameters& params){
printf("getbuffer: %s\n", bufferName);
+ buffer.ensureSize(64*1024);
buffer.clear().append("GETB");
buffer.append(bufferName);
+ params.m_name = bufferName;
+ params.m_task = atGetBuffer;
}
void usage(const char* msg, const char* argument){
printf("Usage: client <opts> <command> [<args>]\n"
"client --port=21222 --host=192.168.178.3 blink rpi2_11 99 500 250\n"
"client melody rpi2_12\n"
"client trace rpi2_21 100 1000\n"
- "client getbuffer #A t\n"
+ "client getbuffer '#A' t\n"
"+++ %s%s%s\n",
msg, argument == NULL ? "" : " ", argument == NULL ? "" : argument);
exit(1);
int main (int argc, char **argv) {
const char* host = "127.0.0.1";
int port = 15000;
+ Parameters params;
+ params.m_task = atUndef;
while(argc > 1 && argv[1][0] == '-'){
const char* arg = argv[1];
if (strncmp(arg, "-p", 2) == 0 || strncmp(arg, "--port=", 7) == 0){
} else if (strcmp(command, "getbuffer") == 0){
if (argc < 3)
usage("missing buffername", NULL);
- prepareGetBuffer(argv[2], buffer);
+ prepareGetBuffer(argv[2], buffer, params);
} else {
usage("unknown command:", command);
}
if (client.connect(host, port)){
client.sendAndReceive(buffer);
printf("Received: %s\n", buffer.str());
+ handleAnswer(params, buffer);
}
client.disconnect();
return 0;
*/
void GPIOProcessor::getBuffer(DynBuffer& buffer){
int name;
- if (buffer.length() != 4 + 2 || buffer.at(4) != '#' || buffer.at(5) < 'A' || (name = buffer.at(5)) > 'Z'){
- DynBuffer name(buffer.str() + 4);
+ int ix;
+ if (buffer.length() != 4 + 2 || buffer.at(4) != '#' || (name = buffer.at(5)) < 'A' || name > 'Z'){
buffer.set("ERROR wrong buffername: ").append(name);
- } else if(m_buffers[name] == NULL) {
+ } else if(m_buffers[ix = name - 'A'] == NULL) {
buffer.set("ERROR unknown buffername: #").append(name);
} else {
- int ix = name - 'A';
buffer.set("OK ").append(*m_buffers[ix]);
- delete m_buffers[ix];
- m_buffers[ix] = NULL;
+ if (m_buffers[ix]->length() >= m_buffers[ix]->size()){
+ delete m_buffers[ix];
+ m_buffers[ix] = NULL;
+ }
}
}
/**
buffer.set("ERR too few buffers: 26");
} else {
m_buffers[ix] = new DynBuffer(count);
- new TraceInputTimer(pin, delay, count, m_buffers[ix], this, m_logger, &m_pool);
+ TraceInputTimer* timer = new TraceInputTimer(pin, delay,
+ count, m_buffers[ix], this, m_logger, &m_pool);
+ timer->start();
buffer.set("OK #").append(name);
}
}
m_logger->say(LOG_ERROR, m_buffer.str());
}
if (m_buffer.length() > 0){
- ::send (connectionSocket, m_buffer.str(), m_buffer.length(), 0);
+ int offset = 0;
+ int sent;
+ while ( (sent = ::send (connectionSocket, m_buffer.str() + offset,
+ m_buffer.length() - offset, 0)) > 0){
+ m_logger->sayf(LOG_DEBUG, "sent: %d / %d", sent,
+ m_buffer.length() - offset);
+ if (offset + sent >= m_buffer.length())
+ break;
+ offset += sent;
+ }
}
} while (state != stStop);
close (connectionSocket);