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. d8857803a300


From: Rob Savoye
Subject: [Powerguru-commit] [SCM] powerguru branch, master, updated. d8857803a3001ec7b1fc78edc0681191ac22b461
Date: Sat, 8 Dec 2018 19:25:19 -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  d8857803a3001ec7b1fc78edc0681191ac22b461 (commit)
       via  f23aa7ad21221c529f7ddc4be97358a64a0270de (commit)
       via  d95e57fcd93186b51e9e3054dfedfead000558da (commit)
      from  4c565a0f8ab38f0a57d6189edded056e48d2fd6b (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=d8857803a3001ec7b1fc78edc0681191ac22b461


commit d8857803a3001ec7b1fc78edc0681191ac22b461
Author: Rob Savoye <address@hidden>
Date:   Sat Dec 8 17:25:02 2018 -0700

    Move console from thread to main

diff --git a/daemon/main.cc b/daemon/main.cc
index 1a51924..d89fad6 100644
--- a/daemon/main.cc
+++ b/daemon/main.cc
@@ -52,6 +52,7 @@ include "xantrex-trace.h"
 #include "ownet.h"
 #endif
 #include "menuitem.h"
+#include "console.h"
 #include "database.h"
 #include "snmp.h"
 #include "rc.h"
@@ -71,7 +72,7 @@ alarm_handler2 (int sig);
 
 int curses = 0;
 
-extern void client_handler(void);
+extern void client_handler(Tcpip &net);
 extern void ownet_handler(pdev::Ownet &);
 extern void outback_handler(pdev::Ownet &);
 extern void xantrex_handler(pdev::Ownet &);
@@ -240,10 +241,12 @@ main(int argc, char *argv[])
 #endif
 
     dbglogfile << "PowerGuru - 1 Wire Mode" << std::endl;
-    pdev::Ownet ownet;
-    std::thread first (client_handler);
+    Tcpip net;
+    net.createNetServer(DEFAULTPORT);
+    std::thread first (client_handler, std::ref(net));
 #ifdef BUILD_OWNET
-    //std::thread second (ownet_handler, std::ref(ownet));
+    pdev::Ownet ownet;
+    std::thread second (ownet_handler, std::ref(ownet));
 #endif
 #ifdef BUILD_XANTREX
     std::thread third (xantrex_handler, std::ref(ownet));
@@ -322,12 +325,40 @@ main(int argc, char *argv[])
             messages.clear();
         }
     }
-    // con.Reset();
-    // con.Close();
+#endif
+
+#if 0
+    // Open a console for user input
+    Console con;
+    con.openCon();
+    con.makeRaw();
+    int ch = 0;
+    while ((ch = con.getcCon()) != 'q') {
+        if (ch > 0) {                // If we have something, process it
+            con.putcCon (ch);          // echo inputted character to screen
+            switch (ch) {
+              case 'Q':
+              case 'q':
+                  dbglogfile << "Qutting PowerGuru due to user input!" << 
std::endl;
+                  break;
+              case '?':
+                  con.putsCon("PowerGuru client\n");
+                  con.putsCon("\t? - help\r\n");
+                  con.putsCon("\tq - Quit\r\n");
+                  con.putsCon("\tQ - Quit\r\n");
+                  sleep(1);
+              default:
+                  break;
+            };
+        }
+    }
+
+    con.resetCon();
 #endif
     // synchronize threads:
+    dbglogfile << "Killing all threads..." << std::endl;
     first.join();                // pauses until first finishes
-#ifdef OWNET
+#ifdef BUILD_OWNET_XXX
     second.join();                // pauses until second finishes
 #endif
 #ifdef BUILD_OUTBACK
diff --git a/daemon/threads.cc b/daemon/threads.cc
index f6df7b6..6bd9c48 100644
--- a/daemon/threads.cc
+++ b/daemon/threads.cc
@@ -52,57 +52,28 @@ extern LogFile dbglogfile;
 using namespace std::chrono_literals;
 
 void
-console_handler(Console &con)
-{
-    DEBUGLOG_REPORT_FUNCTION;
-    
-    // Open a console for user input
-    con.Open();
-    int ch = 0;
-    while ((ch = con.Getc()) != 'q') {
-        if (ch > 0) {                // If we have something, process it
-            con.Putc (ch);          // echo inputted character to screen
-            switch (ch) {
-              case 'Q':
-              case 'q':
-                  dbglogfile <<"Qutting PowerGuru due to user input!\n" << 
std::endl;
-                  con.Reset();
-                  exit(0);
-                  break;
-              case '?':
-                  con.Puts("PowerGuru client\n");
-                  con.Puts("\t? - help\r\n");
-                  con.Puts("\tq - Quit\r\n");
-                  con.Puts("\tQ - Quit\r\n");
-                  sleep(2);
-              default:
-                  break;
-            };
-        }
-    }
-}
-
-void
-client_handler(void)
+client_handler(Tcpip &net)
 {
     DEBUGLOG_REPORT_FUNCTION;
 
     retcode_t ret;
-    Tcpip net;
-    int retries = 0;
-
-    net.createNetServer(DEFAULTPORT);
+    //Tcpip net;
+    int retries = 10;
 
-    while (retries-- < 10) {
+    while (retries-- <= 10) {
         net.newNetConnection(true);
 
         bool loop = true;
         std::vector<unsigned char> data;
         while (loop) {
+            data.clear();
             if (net.readNet(data).size() < 0) {
                 dbglogfile << "ERROR: Got error from socket " << std::endl;
                 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) {
@@ -114,7 +85,15 @@ client_handler(void)
                     sleep(1);
                     continue;
                 }
-                buffer.erase(buffer.find('\n')-1);
+                size_t pos = buffer.find('\n')-1;
+                //std::cerr << "FOO: " << pos << " | " << std::string::npos << 
std::endl;
+                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;
@@ -134,11 +113,13 @@ client_handler(void)
                 } else {
                     std::cerr << buffer << std::endl;
                 }
+                buffer.clear();
             }
         }
     }
 
-    net.closeNet();
+    //next.join();
+    net.closeConnection();
 }
 
 #ifdef BUILD_OWNET

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


commit f23aa7ad21221c529f7ddc4be97358a64a0270de
Author: Rob Savoye <address@hidden>
Date:   Sat Dec 8 17:22:29 2018 -0700

    Use more protected variables from the base class.

diff --git a/lib/tcpip.cc b/lib/tcpip.cc
index 496bada..acf749b 100644
--- a/lib/tcpip.cc
+++ b/lib/tcpip.cc
@@ -63,7 +63,14 @@ Tcpip::Tcpip(void)
 }
 
 Tcpip::~Tcpip(void)
-{    
+{
+    std::vector<int>::iterator it;
+//    for (it = _connection.begin(); it != _connections.end(); it++) {
+//        closeConnection(it);
+//    }
+
+    //next.join();
+    closeNet();
 }
 
 // Description: Create a tcp/ip network server. This creates a server
@@ -75,26 +82,14 @@ Tcpip::createNetServer(const std::string &service, const 
std::string &proto)
 {
     DEBUGLOG_REPORT_FUNCTION;
 
-    const struct servent  *serv;
-
-    serv = lookupService(service);
-  
-    // See if we got a service data structure
-    if (serv == 0) {
-        dbglogfile << "ERROR: unable to get " << service
-                   << " service entry" << std::endl;
-        _port = 0;
-        return ERROR;
-    }
-
-    dbglogfile << "Port number is " << serv->s_port
-               << ", byte swapped is " << htons(serv->s_port) << std::endl;
+    dbglogfile << "Port number is " << _service->s_port
+               << ", byte swapped is " << htons(_service->s_port) << std::endl;
 
     // Store the port number
-    _port = serv->s_port;
+    _port = _service->s_port;
     //_proto = serv->s_proto;
   
-    return createNetServer(serv->s_port, proto);
+    return createNetServer(_service->s_port, proto);
 }
 
 retcode_t
@@ -219,9 +214,10 @@ Tcpip::createNetServer(short port, const std::string 
&protocol)
 #if 0
         dbglogfile << "Listening for net traffic on fd #\n" << _sockfd << 
std::endl;
 #endif
-    
+
         return SUCCESS;
     }
+
     return ERROR;
 }
 
@@ -308,13 +304,14 @@ Tcpip::newNetConnection(bool block)
     if (_sockfd < 0) {
         dbglogfile << "unable to accept : " << strerror(errno) << std::endl;
         return ERROR;
+    } else {
+        dbglogfile << "Accepting tcp/ip connection on fd #"
+                   << _sockfd << std::endl;
     }
-  
-    dbglogfile << "Accepting tcp/ip connection on fd #"
-               << _sockfd << std::endl;
 
-    memcpy(&_client, &fsin, sizeof(struct sockaddr));
+    std::memcpy(&_client, &fsin, sizeof(struct sockaddr));
   
+    _connections.push_back(_sockfd);
     return SUCCESS;
 }
 
@@ -334,27 +331,12 @@ Tcpip::createNetClient(const std::string &hostname, short 
port,
     DEBUGLOG_REPORT_FUNCTION;
     struct sockaddr_in sock_in;
     int                type;
-    char                thishostname[MAXHOSTNAMELEN];
     fd_set              fdset;
     struct timeval      tval;
     int                 ret;
     int                 retries;
   
     memset(&sock_in, 0, sizeof(struct sockaddr_in));
-    memset(&thishostname, 0, MAXHOSTNAMELEN);
-  
-    // If there is no supplied hostname, assume it's a local process
-    //  if ((hostname.size() == 0) || (hostname == "localhost")) {
-    if (hostname.size() == 0) {
-        if (gethostname(thishostname, MAXHOSTNAMELEN) == 0) {
-            dbglogfile << "The hostname for this machine is "
-                       << thishostname;
-        } else {
-            dbglogfile << "WARNING: Couldn't get the hostname for this 
machine!" << std::endl;
-        }
-    } else {
-        strcpy(thishostname, hostname.c_str());
-    }
   
     // const struct hostent *hent = hostByNameGet(thishostname);
     // memcpy(&sock_in.sin_addr, hent->h_addr, hent->h_length);
@@ -376,7 +358,7 @@ Tcpip::createNetClient(const std::string &hostname, short 
port,
         type = SOCK_STREAM;
     }
   
-    _sockfd = socket(PF_INET, type, _proto->p_proto);
+    _sockfd = socket(PF_INET, type, _addrinfo->ai_protocol);
   
     if (_sockfd < 0) {
         dbglogfile << "WARNING: unable to create socket: "
@@ -426,7 +408,7 @@ Tcpip::createNetClient(const std::string &hostname, short 
port,
                     reinterpret_cast<struct sockaddr *>(&sock_in),
                     sizeof(sock_in)) < 0) {
             dbglogfile << "unable to connect to "
-                       << thishostname
+                       << _hostname
                        << ", port " << port
                        << ": " << strerror(errno) << std::endl;
             close(_sockfd);
@@ -453,11 +435,9 @@ Tcpip::closeConnection(void)
     DEBUGLOG_REPORT_FUNCTION;
 
     if (_sockfd > 0) {
-        //closeConnection(_sockfd); FIXME:
-        //close(_sockIOfd);
-        //_sockIOfd = 0;
         close(_sockfd);
         _sockfd = 0;
+        return SUCCESS;
     }
   
     return ERROR;
@@ -706,6 +686,8 @@ Tcpip::readNet(std::vector<unsigned char> &buf)
     } else {
         dbglogfile << "WARNING: Can't do anything with socket fd #"
                    << _sockfd << std::endl;
+        buf.clear();
+        return buf;
     }
   
     tval.tv_sec = DEFAULTTIMEOUT;
diff --git a/lib/tcpip.h b/lib/tcpip.h
index 97897fb..5facff9 100644
--- a/lib/tcpip.h
+++ b/lib/tcpip.h
@@ -61,6 +61,7 @@ public:
     // Accept a client connection for the current server.
     retcode_t newNetConnection(void);
     retcode_t newNetConnection(bool block);
+
     // Create a client connection to a tcp/ip server
     retcode_t createNetClient(void) {
         return createNetClient("localhost", DEFAULTPORT, DEFAULTPROTO);
@@ -119,6 +120,7 @@ public:
   
 protected:
     int                 _sockfd;
+    std::vector<int>    _connections;
     int                 _sockIOfd;
     //in_addr_t         _ipaddr;
     std::string         _hostname;

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


commit d95e57fcd93186b51e9e3054dfedfead000558da
Author: Rob Savoye <address@hidden>
Date:   Sat Dec 8 17:20:46 2018 -0700

    change capitalization fo rmethod names to be consistent with the rest of 
the code.

diff --git a/lib/console.cc b/lib/console.cc
index 7f3b475..5b05848 100644
--- a/lib/console.cc
+++ b/lib/console.cc
@@ -43,27 +43,23 @@ Console::Console (void) {
 }
 
 Console::~Console (void) {
-    Close();
+    closeCon();
     inchannel.filespec.erase();
     outchannel.filespec.erase();
     state = Console::UNSET;
 }
 
 void
-Console::Open (void) {
+Console::openCon (void) {
     // Open a console for the end user to type at. We default to stdio (cin)
-    //  string str = "stdin";
+    openInChannel();
   
-    OpenInChannel();
-  
-    //  str = "stdout";
-
     // Open a the output channel for the console. We default to stdio (cout)
-    OpenOutChannel();
+    openOutChannel();
 }
 
 void
-Console::OpenInChannel (string channel) {
+Console::openInChannel (string channel) {
     if (channel == "stdin") {
         inchannel.fhandle = stdin;
         inchannel.filespec = "stdin";
@@ -91,7 +87,7 @@ Console::OpenInChannel (string channel) {
 }
 
 void
-Console::OpenInChannel (void) {
+Console::openInChannel (void) {
     inchannel.fhandle = stdin;
     inchannel.filespec = "stdin";
     state = Console::OPEN;
@@ -118,7 +114,7 @@ Console::OpenInChannel (void) {
 }
 
 void
-Console::OpenOutChannel (string channel) {
+Console::openOutChannel (string channel) {
     if (channel == "stdout") {
         outchannel.fhandle = stdout;
         outchannel.filespec = "stdout";
@@ -128,7 +124,7 @@ Console::OpenOutChannel (string channel) {
 }
 
 void
-Console::OpenOutChannel (void) {
+Console::openOutChannel (void) {
     outchannel.fhandle = stdout;
     outchannel.filespec = "stdout";
     state = Console::OPEN;
@@ -136,18 +132,18 @@ Console::OpenOutChannel (void) {
 
 // Reset the input channel to be where it was when we started
 void
-Console::Close (void) {
+Console::closeCon (void) {
     state = Console::CLOSED;
-    Reset();
+    resetCon();
 }
 
 void
-Console::Reset (void) {
+Console::resetCon (void) {
     tcsetattr(fileno(inchannel.fhandle), TCSANOW, &oldtty);
 }
 
 void
-Console::MakeRaw (int x) {
+Console::makeRaw (int x) {
     termios newtty;
     int flag;
 
@@ -165,9 +161,9 @@ Console::MakeRaw (int x) {
 }
 
 void
-Console::SendEOL (void) {
-    Putc('\r');
-    Putc('\n'); 
+Console::sendEOL (void) {
+    putcCon('\r');
+    putcCon('\n'); 
 }
 
 void
diff --git a/lib/console.h b/lib/console.h
index 9709b9d..0ec1c5b 100644
--- a/lib/console.h
+++ b/lib/console.h
@@ -35,32 +35,32 @@ public:
 
     enum state_e { UNSET, OPEN, RAW, CLOSED };
 
-    void Open (void);
-    void OpenInChannel (std::string channel);
-    void OpenInChannel (void);
-    void OpenOutChannel (std::string channel);
-    void OpenOutChannel (void);
-    void Close (void);
-    void Reset (void);
+    void openCon (void);
+    void openInChannel (std::string channel);
+    void openInChannel (void);
+    void openOutChannel (std::string channel);
+    void openOutChannel (void);
+    void closeCon (void);
+    void resetCon (void);
   
-    void SendEOL (void);
+    void sendEOL (void);
   
-    void MakeRaw (int);
-    void MakeRaw (void) { MakeRaw (fileno(inchannel.fhandle)); }
-    void MakeRaw (FILE * x) { MakeRaw (fileno(x)); }
+    void makeRaw (int);
+    void makeRaw (void) { makeRaw (fileno(inchannel.fhandle)); }
+    void makeRaw (FILE * x) { makeRaw (fileno(x)); }
 
     // get a byte from the console
-    int Getc (void) { return Getc (inchannel.fhandle); }
-    int Getc (FILE *x) { return getc(x); }
+    int getcCon (void) { return getcCon (inchannel.fhandle); }
+    int getcCon (FILE *x) { return getc(x); }
 
     // write a byte to the console
-    int Putc (int x) { return Putc (x, outchannel.fhandle); }
-    int Putc (int x, FILE *y)  { return putc(x, y); }
+    int putcCon (int x) { return putcCon (x, outchannel.fhandle); }
+    int putcCon (int x, FILE *y)  { return putc(x, y); }
 
     // write a string to the console
-    int Puts (std::string out) { return Puts (out.c_str(), 
outchannel.fhandle); }
-    int Puts (std::string out, FILE *y)  { return fputs(out.c_str(), y); }
-    int Puts (ErrCond &Err)  { return fputs(Err.GetMsg().c_str(), 
outchannel.fhandle); }
+    int putsCon (std::string out) { return putsCon (out.c_str(), 
outchannel.fhandle); }
+    int putsCon (std::string out, FILE *y)  { return fputs(out.c_str(), y); }
+    int putsCon (ErrCond &Err)  { return fputs(Err.GetMsg().c_str(), 
outchannel.fhandle); }
  
     // Accessors
 

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

Summary of changes:
 daemon/main.cc    | 45 +++++++++++++++++++++++++++++++------
 daemon/threads.cc | 59 +++++++++++++++++--------------------------------
 lib/console.cc    | 34 +++++++++++++---------------
 lib/console.h     | 36 +++++++++++++++---------------
 lib/tcpip.cc      | 66 ++++++++++++++++++++-----------------------------------
 lib/tcpip.h       |  2 ++
 6 files changed, 117 insertions(+), 125 deletions(-)


hooks/post-receive
-- 
powerguru



reply via email to

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