From: hama Date: Mon, 20 Jun 2016 22:49:30 +0000 (+0200) Subject: getbuffer works, data written in file X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=2c59a46eaa15b2f95528ff01fd5fc936568c19bd;p=cpidjinn getbuffer works, data written in file --- diff --git a/Server/client.cpp b/Server/client.cpp index 8450d75..74b92bd 100644 --- a/Server/client.cpp +++ b/Server/client.cpp @@ -2,6 +2,30 @@ #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; @@ -48,10 +72,14 @@ static void prepareTrace(PinNumber pin, int count, int delay, DynBuffer& buffer) 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 []\n" @@ -79,7 +107,7 @@ void usage(const char* msg, const char* argument){ "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); @@ -88,6 +116,8 @@ void usage(const char* msg, const char* argument){ 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){ @@ -154,7 +184,7 @@ int main (int argc, char **argv) { } 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); } @@ -163,6 +193,7 @@ int main (int argc, char **argv) { if (client.connect(host, port)){ client.sendAndReceive(buffer); printf("Received: %s\n", buffer.str()); + handleAnswer(params, buffer); } client.disconnect(); return 0; diff --git a/Server/gpioprocessor.cpp b/Server/gpioprocessor.cpp index a9e43ec..e39dfb0 100644 --- a/Server/gpioprocessor.cpp +++ b/Server/gpioprocessor.cpp @@ -58,16 +58,17 @@ void GPIOProcessor::blink(DynBuffer& buffer){ */ 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; + } } } /** @@ -184,7 +185,9 @@ void GPIOProcessor::trace(DynBuffer& buffer){ 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); } } diff --git a/Server/tcpclient.cpp b/Server/tcpclient.cpp index afb095c..06018d0 100644 --- a/Server/tcpclient.cpp +++ b/Server/tcpclient.cpp @@ -81,8 +81,10 @@ void TcpClient::sendAndReceive(DynBuffer& buffer){ buffer.clear(); } else { buffer.setLength(length); - if (buffer.startsWith("ERRO", 4)){ + if (buffer.startsWith("ERR", 3)){ m_logger->say(LOG_ERROR, buffer.str()); + } else { + m_logger->sayf(LOG_DEBUG, "received: %d", length); } } } diff --git a/Server/tcpserver.cpp b/Server/tcpserver.cpp index 1433bb8..c5b4cc1 100644 --- a/Server/tcpserver.cpp +++ b/Server/tcpserver.cpp @@ -174,7 +174,16 @@ void TcpServer::listen(){ 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); diff --git a/util/timer.cpp b/util/timer.cpp index 5b2b039..4ff3faf 100644 --- a/util/timer.cpp +++ b/util/timer.cpp @@ -1,5 +1,5 @@ #include "util.hpp" -#define TRACE_ON +//#define TRACE_ON #include "trace.hpp" /** * Constructor.