powerguru-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Powerguru-commit] [SCM] powerguru branch, master, updated. 64e6a6773c13


From: Rob Savoye
Subject: [Powerguru-commit] [SCM] powerguru branch, master, updated. 64e6a6773c13534e53cfeefa2ad048eea1e9b42e
Date: Tue, 11 Dec 2018 19:51:04 -0500 (EST)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "powerguru".

The branch, master has been updated
       via  64e6a6773c13534e53cfeefa2ad048eea1e9b42e (commit)
      from  5ff742be5a8f81b582154e82ad9aafb7a8c159d0 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=64e6a6773c13534e53cfeefa2ad048eea1e9b42e


commit 64e6a6773c13534e53cfeefa2ad048eea1e9b42e
Author: Rob Savoye <address@hidden>
Date:   Tue Dec 11 17:51:00 2018 -0700

    Add command processor

diff --git a/daemon/main.cc b/daemon/main.cc
index d89fad6..c5ff253 100644
--- a/daemon/main.cc
+++ b/daemon/main.cc
@@ -31,6 +31,7 @@
 #include <thread>
 #include <mutex>
 #include <condition_variable>
+#include <queue>
 
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
@@ -59,6 +60,7 @@ include "xantrex-trace.h"
 #include "tcpip.h"
 #include "xml.h"
 #include "serial.h"
+#include "commands.h"
 
 using namespace std;
 using namespace rcinit;
@@ -77,6 +79,10 @@ extern void ownet_handler(pdev::Ownet &);
 extern void outback_handler(pdev::Ownet &);
 extern void xantrex_handler(pdev::Ownet &);
 
+std::mutex queue_lock;
+std::queue <XML> tqueue;
+std::condition_variable queue_cond;
+
 int
 main(int argc, char *argv[])
 {
@@ -223,15 +229,6 @@ main(int argc, char *argv[])
         }
     }
 
-//     // Open the network connection to the database.
-// #if defined(HAVE_MARIADB) && defined(HAVE_POSTGRESQL)
-//     if (use_db) {
-//         if (!pdb.openDB()) {
-//             dbglogfile << "ERROR: Couldn't open database!" << endl;
-//             exit(1);
-//         }
-//     }
-// #endif
     // Start the SNMP daemon support.
 #ifdef USE_SNMP
     if (snmp) {
@@ -253,13 +250,11 @@ main(int argc, char *argv[])
 #endif
 #ifdef BUILD_OUTBACK
     std::thread fourth(outback_handler, std::ref(ownet));
-#endif
-//    std::thread forth (console_handler, std::ref(con));
 //    std::thread forth (msg_handler, std::ref(pdb));
+#endif
     
 #ifdef BUILD_OUTBACK
-
-// Network daemon/client mode. Normally we're a network daemon that
+    // Network daemon/client mode. Normally we're a network daemon that
     // responses to requests by a remote client. Many house networks
     // are behind a firewall, so the daemon can also connect to a
     // publically accessible host to establish the connection the
@@ -290,9 +285,7 @@ main(int argc, char *argv[])
         XML xml;
         unsigned int i;
 
-        vector<const xmlChar *> messages;
-        //const xmlChar *messages[200];
-        
+        vector<const xmlChar *> messages;        
         bool loop = true;
         while (loop) {
             ret = msg.anydata(messages);
@@ -355,10 +348,28 @@ main(int argc, char *argv[])
 
     con.resetCon();
 #endif
+
+    // Commands from the client via the client_handler get processed here
+    // so messages can be passed between threads.
+    while (true) {
+        std::unique_lock<std::mutex> guard(queue_lock);
+        queue_cond.wait(guard, [] { return !tqueue.empty(); });
+        XML xml = tqueue.front();
+        tqueue.pop();
+        // if (xml[0]->nameGet() == "list") {
+            std::vector<std::string> devs;
+            ownet.listDevices(devs);
+            std::vector<std::string>::iterator sit;
+            for (sit = devs.begin(); sit != devs.end(); sit++) {
+                std::cerr << "FIXME: " << *sit <<std::endl;
+            }
+                    //}
+    }
+
     // synchronize threads:
     dbglogfile << "Killing all threads..." << std::endl;
     first.join();                // pauses until first finishes
-#ifdef BUILD_OWNET_XXX
+#ifdef BUILD_OWNET
     second.join();                // pauses until second finishes
 #endif
 #ifdef BUILD_OUTBACK
diff --git a/daemon/threads.cc b/daemon/threads.cc
index 6bd9c48..f375b50 100644
--- a/daemon/threads.cc
+++ b/daemon/threads.cc
@@ -32,6 +32,7 @@
 #include <condition_variable>
 #include <mutex>
 #include <chrono>
+#include <queue>
 
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
@@ -46,19 +47,26 @@ extern char *optarg;
 #include "database.h"
 #include "tcpip.h"
 #include "xml.h"
+#include "commands.h"
 
 extern LogFile dbglogfile;
 
 using namespace std::chrono_literals;
 
+extern std::mutex queue_lock;
+extern std::queue <XML> tqueue;
+extern std::condition_variable queue_cond;
+
 void
 client_handler(Tcpip &net)
 {
     DEBUGLOG_REPORT_FUNCTION;
 
     retcode_t ret;
-    //Tcpip net;
+    Commands cmd;
     int retries = 10;
+    std::string hostname;
+    std::string user;
 
     while (retries-- <= 10) {
         net.newNetConnection(true);
@@ -67,15 +75,15 @@ client_handler(Tcpip &net)
         std::vector<unsigned char> data;
         while (loop) {
             data.clear();
-            if (net.readNet(data).size() < 0) {
+            size_t pos = net.readNet(data).size();
+            if (pos < 0) {
                 dbglogfile << "ERROR: Got error from socket " << std::endl;
-                loop = false;
+                //loop = false;
             } else {
                 if (data.data() == 0) {
                     continue;  
                 }
                 std::string buffer = (char *)data.data();
-                //if (buffer.size() == 1 && *data.data() == 0) {
                 if (data.size() == 1 && buffer[0] == 0) {
                     net.closeConnection();
                     // loop = false;
@@ -85,8 +93,9 @@ client_handler(Tcpip &net)
                     sleep(1);
                     continue;
                 }
-                size_t pos = buffer.find('\n')-1;
-                //std::cerr << "FOO: " << pos << " | " << std::string::npos << 
std::endl;
+                // this assumes all packets from the client are terminated
+                // with a newline, which text input from the console is.
+                size_t pos = buffer.find('\n');
                 if (pos == 0 || pos == std::string::npos) {
                     data.clear();
                     buffer.clear();
@@ -94,31 +103,28 @@ client_handler(Tcpip &net)
                     continue;
                 }
                 buffer.erase(pos);
+                std::string str;
                 // if the first character is a <, assume it's in XML formst.
+                XML xml;
                 if (buffer[0] == '<') {
-                    XML xml;
                     xml.parseMem(buffer);
-                    dbglogfile << "FIXME:: \"" << xml.nameGet() << "\"" << 
std::endl;
-                    if (xml.nameGet() == "command") {
-                        std::cerr << "FIXME: Command: " << xml.valueGet() << 
std::endl;
-                        if (xml.valueGet() == "help") {
-                            net.writeNet("Hello World!\n");
-                        }
-                        
-                    } else if (xml.nameGet() == "data") {
-                        std::cerr << "FIXME: DATA: " << xml.valueGet() << 
std::endl;
+                    if (xml[0]->nameGet() == "helo") {
+                        hostname = xml[0]->childGet(0)->valueGet();
+                        user =  xml[0]->childGet(1)->valueGet();
+                        dbglogfile << "Incoming connection from user " << user
+                                   << " on host " << hostname << std::endl;
                     } else {
-                        std::cerr << "FIXME: JUNK: " << xml.valueGet() << 
std::endl;
+                        cmd.execCommand(xml, str);
+                        std::lock_guard<std::mutex> guard(queue_lock);
+                        tqueue.push(xml);
+                        queue_cond.notify_one();
                     }
-                } else {
-                    std::cerr << buffer << std::endl;
                 }
                 buffer.clear();
             }
         }
     }
 
-    //next.join();
     net.closeConnection();
 }
 
@@ -143,7 +149,7 @@ ownet_handler(pdev::Ownet &ownet)
                 ownet.getTemperature(it->first.c_str());
             }
         }
-        ownet.dump();
+        //ownet.dump();
         
         // Don't eat up all the cpu cycles!
         
std::this_thread::sleep_for(std::chrono::seconds(ownet.getPollSleep()));

-----------------------------------------------------------------------

Summary of changes:
 daemon/main.cc    | 45 ++++++++++++++++++++++++++++-----------------
 daemon/threads.cc | 48 +++++++++++++++++++++++++++---------------------
 2 files changed, 55 insertions(+), 38 deletions(-)


hooks/post-receive
-- 
powerguru



reply via email to

[Prev in Thread] Current Thread [Next in Thread]