]> gitweb.hamatoma.de Git - cpidjinn/commitdiff
getbuffer works, data written in file
authorhama <hama@siduction.net>
Mon, 20 Jun 2016 22:49:30 +0000 (00:49 +0200)
committerhama <hama@siduction.net>
Mon, 20 Jun 2016 22:49:30 +0000 (00:49 +0200)
Server/client.cpp
Server/gpioprocessor.cpp
Server/tcpclient.cpp
Server/tcpserver.cpp
util/timer.cpp

index 8450d75d49a04a6218bfaadc7d32f8ccd48b476c..74b92bdb22475fc65e469f1f4ed5156d56c18392 100644 (file)
@@ -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 <opts> <command> [<args>]\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;
index a9e43eca70245481fd97684adee0b9b0a40c5d50..e39dfb03f3db76eb5e43a9ccaa5629e28b114f84 100644 (file)
@@ -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);
                }
        }
index afb095c868a629790f093af0b6973b3c166790d3..06018d008ac3bbff59f80c828f02e1a18cb0bf58 100644 (file)
@@ -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);
                        }
                }
        }
index 1433bb8a97f936a13ab444ca9a52d458df3b59b7..c5b4cc1d1c2312dc83394590ee5c2be31f24c85a 100644 (file)
@@ -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);
index 5b2b0397d1fbab65d69ef296625ecfc0c0535074..4ff3fafa3bfe02e17e9f2784fb1d4a5094c3a142 100644 (file)
@@ -1,5 +1,5 @@
 #include "util.hpp"
-#define TRACE_ON
+//#define TRACE_ON
 #include "trace.hpp"
 /**
  * Constructor.