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. 5757462fc948


From: Rob Savoye
Subject: [Powerguru-commit] [SCM] powerguru branch, master, updated. 5757462fc948d67b22047c1d5542136a1aa5eeb5
Date: Mon, 7 Jan 2019 13:10:48 -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  5757462fc948d67b22047c1d5542136a1aa5eeb5 (commit)
       via  34f05d1999a605cd57ab0f797f103c10450e8737 (commit)
       via  6a58c4ea79dd84fa813477a7e72eaf6d4d6c11fe (commit)
       via  7e34bfad4d4cfa22690691717753b7341264fde3 (commit)
      from  e3ba267837b824dd5134358179f8378d398c4ff2 (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=5757462fc948d67b22047c1d5542136a1aa5eeb5


commit 5757462fc948d67b22047c1d5542136a1aa5eeb5
Author: Rob Savoye <address@hidden>
Date:   Mon Jan 7 11:10:35 2019 -0700

    Don't connect to the newtwork in main

diff --git a/daemon/main.cc b/daemon/main.cc
index 1b1fc44..ecc3717 100644
--- a/daemon/main.cc
+++ b/daemon/main.cc
@@ -97,7 +97,7 @@ main(int argc, char *argv[])
     retcode_t   ret;
     std::condition_variable alldone;
 
-    log_init("pgd");
+    log_init("/tmp/pgd");
     
     // scan for the two main standard GNU options
     for (c = 0; c < argc; c++) {
@@ -113,8 +113,8 @@ main(int argc, char *argv[])
 
     // Set the option flags to default values. We do it this way to
     // shut up GCC complaining they're not used.
-    daemon = true;
-    client = false;
+    //daemon = true;
+    //client = false;
     snmp = false;
     // Load the database config variable so they can be overridden by
     // the command line arguments.
@@ -140,26 +140,10 @@ main(int argc, char *argv[])
               // not using the default one.
               owserver = strdup(optarg);
               break;
-#if 0
-          case 'c':
-              client = true;
-              daemon = false;
-              break;
-
-          case 'o':
-              outbackmode = true;
-              break;
-
-          case 'r':
-              background = true;
-              break;
-#endif
 
 #ifdef USE_SNMP
           case 'j':
               snmp = true;
-              daemon = false;
-              client = false;
               break;
 #endif
               // Specify database host machine.
@@ -172,7 +156,7 @@ main(int argc, char *argv[])
           case 'v':
               // verbosity++;
               //dbglogfile.set_verbosity();
-              BOOST_LOG(lg) << "Verbose output turned on" << std::endl;
+              BOOST_LOG(lg) << "FIXME: Verbose output turned on";
               break;
        
           default:
@@ -188,18 +172,9 @@ main(int argc, char *argv[])
         agent.master(false);
     }
 #endif
-
-    BOOST_LOG(lg) << "PowerGuru - 1 Wire Mode" << std::endl;
-    Tcpip net;
-    if (net.createNetServer(DEFAULTPORT) == ERROR) {
-        BOOST_LOG_SEV(lg, severity_level::error)
-            << "ERROR: Couldn't create a network server!";
-        exit(-1);
-    }
-
     Onewire ow;
     std::thread onewire_thread (onewire_handler, std::ref(ow));
-
+    Tcpip net;
     std::thread client_thread (client_handler, std::ref(net));
 #ifdef BUILD_OWNET
     Ownet ownet(owserver);

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=34f05d1999a605cd57ab0f797f103c10450e8737


commit 34f05d1999a605cd57ab0f797f103c10450e8737
Author: Rob Savoye <address@hidden>
Date:   Mon Jan 7 11:10:08 2019 -0700

    Use boost::asio networking instead of the old crufty code

diff --git a/daemon/threads.cc b/daemon/threads.cc
index a928bdb..267f62b 100644
--- a/daemon/threads.cc
+++ b/daemon/threads.cc
@@ -48,9 +48,19 @@ extern char *optarg;
 #include "tcpip.h"
 #include "xml.h"
 #include "commands.h"
-#include "ownet.h"
+#include "onewire.h"
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/write.hpp>
+#include <boost/asio/buffer.hpp>
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio.hpp>
+#include <string>
+#include <ctime>
 
+using namespace boost::asio;
+using namespace boost::asio::ip;
 using namespace std::chrono_literals;
+using boost::asio::ip::tcp;
 
 // This queue is used to pass data between the threads.
 extern std::mutex queue_lock;
@@ -63,6 +73,9 @@ onewire_handler(Onewire &onewire)
     DEBUGLOG_REPORT_FUNCTION;
     BOOST_LOG(lg) << "PowerGuru - 1 Wire Mode";
 
+    std::map<std::string, family_t> table;
+    initTable(table);
+
     if (!onewire.isMounted()) {
         BOOST_LOG(lg) << "WARNING: Couldn't open 1wire file system!";
         return;        
@@ -81,30 +94,34 @@ onewire_handler(Onewire &onewire)
     query += "";
     query += ");";
 
-    std::map<std::string, boost::shared_ptr<temperature_t>>::iterator it;
+    std::map<std::string, boost::shared_ptr<onewire_t>> sensors = 
onewire.getSensors();
+    std::map<std::string, boost::shared_ptr<onewire_t>>::iterator it;
     while (onewire.getPollSleep() > 0) {
-        std::map<std::string, boost::shared_ptr<temperature_t>> temps = 
onewire.getTemperatures();
-        for (it = temps.begin(); it != temps.end(); it++) {
-            if ((it->second->family == "10") | (it->second->family == "28")) {
-#ifdef HAVE_LIBPQ
-                std::string stamp;
-                stamp = pdb.gettime(stamp);
-                std::string query = it->second->family;
-                query += ",\'" + it->second->id + "\'";
-                query += ", \'" + it->second->type + "\'";
-                query += ", \'" + stamp + "\'";
-                query += ", " + std::to_string(it->second->lowtemp);
-                query +=  ", " + std::to_string(it->second->hightemp);
-                query += ", " + std::to_string(it->second->temp) + ", \'";
-                query += it->second->scale;
-                query += "\'";
-                pdb.queryInsert(query);
-#endif
-                //ownet.dump();
+        std::string query;
+        for (it = sensors.begin(); it != sensors.end(); it++) {
+            if (it->second->type == TEMPERATURE) {
+                boost::shared_ptr<temperature_t> 
temp(onewire.getTemperature(it->first));
+                if (temp == 0) {
+                    continue;
+                }
+                if (temp) {
+                    pdb.formatQuery(temp, query);
+                    pdb.queryInsert(query, "temperature");
+                    continue;
+                }
+            }
+            if (it->second->type == BATTERY) {   
+                boost::shared_ptr<battery_t> 
batt(onewire.getBattery(it->first));
+                if (batt) {
+                    pdb.formatQuery(batt, query);
+                    pdb.queryInsert(query, "battery");
+                    continue;
+                }
             }
-            // Don't eat up all the cpu cycles!
-            
std::this_thread::sleep_for(std::chrono::seconds(onewire.getPollSleep()));
         }
+            
+        // Don't eat up all the cpu cycles!
+        
std::this_thread::sleep_for(std::chrono::seconds(onewire.getPollSleep()));
     }
 }
 
@@ -115,71 +132,51 @@ client_handler(Tcpip &net)
 
     retcode_t ret;
     Commands cmd;
-    int retries = 10;
+    int retries = 3;
     std::string hostname;
     std::string user;
+    io_service ioservice;
+    tcp::endpoint tcp_endpoint{tcp::v4(), 7654};
+    tcp::acceptor tcp_acceptor{ioservice, tcp_endpoint};
+    tcp::socket tcp_socket{ioservice};
+    std::string data;
+    tcp::resolver resolv{ioservice};
+    //tcp::socket tcp_socket{ioservice};
+    std::array<char, 4096> bytes;
 
-    while (retries-- <= 10) {
-        if (net.newNetConnection(true) == ERROR) {
-            std::cerr << "ERROR: new connection failed!" << std::endl;
-            return;
-        }
-
-        bool loop = true;
-        std::vector<unsigned char> data;
-        while (loop) {
-            data.clear();
-            size_t pos = net.readNet(data).size();
-            if (pos < 0) {
-                BOOST_LOG(lg) << "ERROR: Got error from socket ";
-                //loop = false;
+    tcp_acceptor.listen();
+    //tcp_acceptor.async_accept(tcp_socket, accept_handler);
+    tcp_acceptor.accept(tcp_socket);
+    ioservice.run();
+    //std::array<char, 4096> bytes;
+    //tcp_socket.async_read_some(buffer(bytes), read_handler);
+    bool loop = true;
+    while (loop) {
+        boost::system::error_code error;
+        boost::asio::write(tcp_socket, buffer("Hello World!\n"), error);
+        tcp_socket.read_some(buffer(bytes), error);
+        std::cerr << bytes.data();
+        // Client dropped connection
+        if (error == boost::asio::error::eof)
+            break;
+        // if the first character is a <, assume it's in XML formst.
+        XML xml;
+        if (bytes[0] == '<') {
+            std::string str(std::begin(bytes), std::end(bytes));
+            xml.parseMem(str);
+            if (xml[0]->nameGet() == "helo") {
+                hostname = xml[0]->childGet(0)->valueGet();
+                user = "foo";// xml[data]->childGet(1)->valueGet();
+                BOOST_LOG(lg) << "Incoming connection from user " << user
+                              << " on host " << hostname;
             } else {
-                if (data.data() == 0) {
-                    continue;  
-                }
-                std::string buffer = (char *)data.data();
-                if (data.size() == 1 && buffer[0] == 0) {
-                    net.closeConnection();
-                    // loop = false;
-                    break;
-                }
-                if (data.size() == 0) {
-                    sleep(1);
-                    continue;
-                }
-                // 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();
-                    loop = false;
-                    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.parseMem(buffer);
-                    if (xml[0]->nameGet() == "helo") {
-                        hostname = xml[0]->childGet(0)->valueGet();
-                        user =  xml[0]->childGet(1)->valueGet();
-                        BOOST_LOG(lg) << "Incoming connection from user " << 
user
-                                   << " on host " << hostname;
-                    } else {
-                        cmd.execCommand(xml, str);
-                        std::lock_guard<std::mutex> guard(queue_lock);
-                        tqueue.push(xml);
-                        queue_cond.notify_one();
-                    }
-                }
-                buffer.clear();
+                cmd.execCommand(xml, str);
+                std::lock_guard<std::mutex> guard(queue_lock);
+                tqueue.push(xml);
+                queue_cond.notify_one();
             }
         }
     }
-
-    net.closeConnection();
 }
 
 #ifdef BUILD_OWNET
@@ -201,42 +198,42 @@ ownet_handler(Ownet &ownet)
         return;
     }
 #endif
+    std::map<std::string, family_t> table;
+    initTable(table);
 
     // Open the network connection to the database.
     std::string query = "INSERT INTO onewire VALUES(";
     query += "";
     query += ");";
 
-    std::map<std::string, boost::shared_ptr<ownet_t>> 
sensors(ownet.getSensors());
-    std::map<std::string, boost::shared_ptr<ownet_t>>::iterator it;
+    std::map<std::string, boost::shared_ptr<onewire_t>> sensors = 
ownet.getSensors();
+    std::map<std::string, boost::shared_ptr<onewire_t>>::iterator it;
     while (ownet.getPollSleep() > 0) {
+        std::string query;
         for (it = sensors.begin(); it != sensors.end(); it++) {
-            if ((it->second->family == "10") | (it->second->family == "28")) {
-                boost::shared_ptr<temperature_t> 
temp(ownet.getTemperature(it->first.c_str()));
+            if (it->second->type == TEMPERATURE) {
+                boost::shared_ptr<temperature_t> 
temp(ownet.getTemperature(it->first));
                 if (temp == 0) {
-                    BOOST_LOG(lg) << "ZERO!!!!";
                     continue;
                 }
-#ifdef HAVE_LIBPQ
-                std::string stamp;
-                stamp = pdb.gettime(stamp);
-                std::string query = temp->family;
-                query += ",\'" + temp->id + "\'";
-                query += ", \'" + temp->type + "\'";
-                query += ", \'" + stamp + "\'";
-                query += ", " + std::to_string(temp->lowtemp);
-                query +=  ", " + std::to_string(temp->hightemp);
-                query += ", " + std::to_string(temp->temp) + ", \'";
-                query += temp->scale;
-                query += "\'";
-                pdb.queryInsert(query);
-#endif
-                //ownet.dump();
+                if (temp) {
+                    pdb.formatQuery(temp, query);
+                    pdb.queryInsert(query, "temperature");
+                    continue;
+                }
+            }
+            if (it->second->type == BATTERY) {   
+                boost::shared_ptr<battery_t> batt(ownet.getBattery(it->first));
+                if (batt) {
+                    pdb.formatQuery(batt, query);
+                    pdb.queryInsert(query, "battery");
+                    continue;
+                }
             }
-        
-            // Don't eat up all the cpu cycles!
-            
std::this_thread::sleep_for(std::chrono::seconds(ownet.getPollSleep()));
         }
+            
+        // Don't eat up all the cpu cycles!
+        
std::this_thread::sleep_for(std::chrono::seconds(ownet.getPollSleep()));
     }
 }
 #endif

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=6a58c4ea79dd84fa813477a7e72eaf6d4d6c11fe


commit 6a58c4ea79dd84fa813477a7e72eaf6d4d6c11fe
Author: Rob Savoye <address@hidden>
Date:   Mon Jan 7 11:10:02 2019 -0700

    Use boost::asio networking instead of the old crufty code

diff --git a/client/threads.cc b/client/threads.cc
index 5485e3c..aa1b875 100644
--- a/client/threads.cc
+++ b/client/threads.cc
@@ -32,14 +32,10 @@
 #include <condition_variable>
 #include <mutex>
 #include <chrono>
-
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#else
-extern int optind;
-extern char *optarg;
-#endif
-
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/write.hpp>
+#include <boost/asio/buffer.hpp>
+#include <boost/asio/ip/tcp.hpp>
 #include "log.h"
 #include "ownet.h"
 #include "console.h"
@@ -49,6 +45,8 @@ extern char *optarg;
 #include "commands.h"
 
 using namespace std::chrono_literals;
+using namespace boost::asio;
+using namespace boost::asio::ip;
 
 void
 console_handler(Tcpip &net)
@@ -69,7 +67,7 @@ console_handler(Tcpip &net)
     args += " ";
     args += std::getenv("USER");
     cmd.createCommand(Commands::HELO, args, str);
-    net.writeNet(str);
+    // net.writeNet(str);
     
     while(loop) {
         // Don't eat up all the cpu cycles!
@@ -87,7 +85,7 @@ console_handler(Tcpip &net)
                 action = line;
             }
             cmd.createCommand(cmd.convertAction(action), args, str);
-            net.writeNet(str);
+            //net.writeNet(str);
         }
     }
 }
@@ -98,71 +96,51 @@ daemon_handler(Tcpip &net)
     DEBUGLOG_REPORT_FUNCTION;
 
     retcode_t ret;
-    int retries = 10;
+    boost::asio::io_service ioservice;
+    tcp::resolver       resolv{ioservice};
+    tcp::socket         tcp_socket{ioservice};
+    std::array<char, 1024> bytes;
+    std::memset(bytes.data(), 0, bytes.size());
+    //tcp::endpoint       tcp_endpoint{tcp::v4(), 2014};
+    tcp::endpoint       
tcp_endpoint(boost::asio::ip::address::from_string("192.168.0.50"), 7654);
 
+    bool loop = true;
+    int retries = 10;
     while (retries-- > 0) {
-        bool loop = true;
-        std::vector<unsigned char> data;
+        //std::vector<unsigned char> data;
+        boost::system::error_code error;
+        tcp::resolver::query q{"pi", "7654"};
+        resolv.resolve(q);
+        tcp_socket.connect(tcp_endpoint);
+        ioservice.run();
+        //boost::asio::write(tcp_socket, buffer("Hello World!\n"), error);
         while (loop) {
-            data.clear();
-            if (net.readNet(data).size() < 0) {
-                BOOST_LOG_SEV(lg, severity_level::error) << "ERROR: Got error 
from socket ";
-                loop = false;
-            } else {
-                if (data.data() == 0) {
-                    continue;
-                }
-                // Store the pointer to the data to make the code easier to 
read.
-                std::string buffer = (char *)data.data();
-                // Check to see if the socket was closed, so the read failed.
-                if (buffer[0] == 255) {
-                    //std::cerr << "Done!!!!" << std::endl;
-                    loop = false;
-                    retries = 0;
-                    break;
-                }                
-                //if (buffer.size() == 1 && *data.data() == 0) {
-                if (data.size() == 1 && buffer[0] == 0) {
-                    net.closeConnection();
-                    // loop = false;
-                    break;
-                }
-                if (data.size() == 0) {
-                    sleep(1);
-                    continue;
-                }
-                size_t pos = buffer.find('\n');
-                if (pos == 0 || pos == std::string::npos) {
-                    data.clear();
-                    buffer.clear();
-                    loop = false;
-                    continue;
-                }
-                buffer.erase(pos);
-                // if the first character is a <, assume it's in XML formst.
-                if (buffer[0] == '<') {
-                    XML xml;
-                    xml.parseMem(buffer);
-                    BOOST_LOG(lg) << "FIXME1: \"" << xml.nameGet() << "\"";
-                    if (xml.nameGet() == "command") {
-                        std::cerr << "FIXME2: 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;
-                    } else {
-                        std::cerr << "FIXME: JUNK: " << xml.valueGet() << 
std::endl;
+            tcp_socket.read_some(buffer(bytes), error);
+            // if the first character is a <, assume it's in XML formst.
+            std::string str = bytes.data();
+            if (bytes[0] == '<') {
+                XML xml;
+                // the data buffer is padded with zeros, so we can safely 
convert
+                // it to a string as it'll be null terminated. Since the 
network
+                // protocol is XML based, we know it'll always be ASCII.
+                xml.parseMem(str);
+                //BOOST_LOG(lg) << "FIXME1: \"" << xml.nameGet() << "\"";
+                if (xml.nameGet() == "command") {
+                    //std::cerr << "FIXME2: Command: " << xml.valueGet() << 
std::endl;
+                    if (xml.valueGet() == "help") {
+                        boost::asio::write(tcp_socket, buffer("Hello World 
Again!\n"), error);
                     }
+                } else if (xml.nameGet() == "data") {
+                    std::cerr << "FIXME: DATA: " << xml.valueGet() << 
std::endl;
                 } else {
-                    std::cerr << buffer << std::endl;
+                    std::cerr << "FIXME: JUNK: " << xml.valueGet() << 
std::endl;
                 }
+                //xml.dump();
+            } else {
+                std::cerr << str << std::endl;
             }
         }
     }
-
-    net.closeNet();
 }
 
 // local Variables:

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=7e34bfad4d4cfa22690691717753b7341264fde3


commit 7e34bfad4d4cfa22690691717753b7341264fde3
Author: Rob Savoye <address@hidden>
Date:   Mon Jan 7 11:09:24 2019 -0700

    Don't connect to the newtwork in main

diff --git a/client/cmd.cc b/client/cmd.cc
index 19b4784..182a6f2 100644
--- a/client/cmd.cc
+++ b/client/cmd.cc
@@ -21,18 +21,6 @@
 # include "config.h"
 #endif
 
-#include <stdarg.h>
-#include <signal.h>
-#include <netinet/in.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <cstdio>
-#include <iostream>
 #include <thread>
 #include <mutex>
 #include <condition_variable>
@@ -54,7 +42,7 @@ main(int argc, char *argv[])
 {
     int         c;
     std::string dbhost = "localhost";
-    std::string pserver = "localhost:" + DEFAULTPORT;
+    std::string pserver = "localhost:" + 7654;
     retcode_t   ret;
 
     log_init("pguru");
@@ -112,16 +100,17 @@ main(int argc, char *argv[])
     }
 #endif
     Tcpip net;
-    if (net.createNetClient(pserver) == ERROR) {
-        std::cerr << "ERROR: Can't connect to Powerguru daemon!" << std::endl;
-        exit(-1);
-    }
+    //if (net.createNetClient(pserver) == ERROR) {
+    //    std::cerr << "ERROR: Can't connect to Powerguru daemon!" << 
std::endl;
+    //    exit(-1);
+    //}
 
-    std::thread first (daemon_handler, std::ref(net));
-    std::thread second (console_handler, std::ref(net));
+    daemon_handler(std::ref(net));
+    //std::thread daemon_thread (daemon_handler, std::ref(net));
+    //std::thread console_thread (console_handler, std::ref(net));
 
-    first.join();                // pauses until second finishes
-    second.join();                // pauses until second finishes
+    //daemon_thread.join();                // pauses until second finishes
+    //console_thread.join();                // pauses until second finishes
 
 #ifdef BUILD_OWNET
     //third.join();                // pauses until second finishes

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

Summary of changes:
 client/cmd.cc     |  31 +++------
 client/threads.cc | 110 ++++++++++++-----------------
 daemon/main.cc    |  35 ++--------
 daemon/threads.cc | 203 +++++++++++++++++++++++++++---------------------------
 4 files changed, 159 insertions(+), 220 deletions(-)


hooks/post-receive
-- 
powerguru



reply via email to

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