Index: trunk/MagicSoft/Cosy/Changelog
===================================================================
--- trunk/MagicSoft/Cosy/Changelog	(revision 2378)
+++ trunk/MagicSoft/Cosy/Changelog	(revision 2379)
@@ -1,3 +1,10 @@
                                                                   -*-*- END -*-*-
+ 2003/10/03 - Thomas Bretz (La Palma)
+ 
+   * tcpip/*
+     - added communication classes
+
+
+
  2003/09/03 - Thomas Bretz
  
Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 2379)
@@ -0,0 +1,93 @@
+#include "MCeCoCom.h"
+
+#include <iostream>
+
+using namespace std;
+
+bool MCeCoCom::InterpreteCmd(TString cmd, TString str)
+{
+    cout << cmd << ": " << str << endl;
+    return true;
+}
+
+bool MCeCoCom::InterpreteStr(TString str)
+{
+    const Ssiz_t tok  = str.First(' ');
+    const TString cmd = str(0, tok);
+    str.Remove(0, tok+1);
+
+    if (cmd==(TString)"CC-REPORT" && str.EndsWith("OVER"))
+    {
+        cout << cmd << ": " << str << endl;
+        int y, m, d, h, min, s, ms, len;
+
+        int n=sscanf(str.Data(),
+                     "%02d %04d %02d %02d %02d %02d %02d %03d %n",
+                     &fCCStatus, &y, &m, &d, &h, &min, &s, &ms, &len);
+        if (n!=8)
+        {
+            cout << "Communication Problem: Wrong number of arguments" << endl;
+            fComStat = kComProblem;
+            return false;
+        }
+        str.Remove(0, len+1);
+
+        fT.SetTimer(y, m, d, h, min, s, ms/1000.);
+
+        // Remove the 29 tokens of the subsystem status
+        //  table 12.1 p59
+        for (int i=0; i<29; i++)
+            str.Remove(0, str.First(' ')+1);
+
+        // skip solar_irr_Wm2, wind_speed, UPS
+        float zd, az, dec, ra, temp, hum;
+        n=sscanf(str.Data(),
+                 "%f %f %f %f %f %*f %*f %f %*f %n",
+                 &zd, &az, &dec, &ra, &temp, &hum, &len);
+        if (n!=6)
+        {
+            cout << "Communication Problem: Wrong number of arguments" << endl;
+            fComStat = kComProblem;
+            return false;
+        }
+        str.Remove(0, len);
+
+        if (str!=(TString)"OVER")
+        {
+            cout << "Communication Problem: Termination (OVER) not found." << endl;
+            fComStat = kComProblem;
+            return false;
+        }
+
+        cout << "Zd/Az: " << zd << "/" << az << "   ";
+        cout << "Ra/Dec: " << ra << "/" << dec << "   ";
+        cout << "Temp:  " << temp << "'C   ";
+        cout << "Hum: " << hum << "%" << endl;
+
+        fComStat = kCmdReceived;
+        return true;
+    }
+
+    const bool rc = InterpreteCmd(cmd, str);
+    fComStat = rc ? kCmdReceived : kComProblem;
+    return rc;
+}
+
+bool MCeCoCom::Send(const char *str)
+{
+    Timer t;
+    t.Now();
+
+    const char *msg =
+        Form("%s "
+             "%02d %04d %02d %02d %02d %02d %02d %03d "
+             "%02d %04d %02d %02d %02d %02d %02d %03d "
+             "%s\n", (const char*)fCommand,
+             fStatus, t.Year(), t.Month(), t.Day(), t.H(), t.M(), t.S(), t.MilliSec(),
+             fComStat, fT.Year(), fT.Month(), fT.Day(), fT.H(), fT.M(), fT.S(), fT.MilliSec(),
+             str);
+
+    const bool rc = MTcpIpIO::Send(msg);
+    fComStat = rc ? kNoCmdReceived : kComProblem;
+    return rc;
+}
Index: trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 2379)
@@ -0,0 +1,47 @@
+#ifndef COSY_MCeCoCom
+#define COSY_MCeCoCom
+
+#ifndef ROOT_TString
+#include <TString.h>
+#endif
+
+#ifndef COSY_MTcpIpIO
+#include "MTcpIpIO.h"
+#endif
+
+#ifndef COSY_Timer
+#include "timer.h"
+#endif
+
+class MCeCoCom : public MTcpIpIO
+{
+private:
+    enum ComStatus_t
+    {
+        kCmdReceived,
+        kNoCmdReceived,
+        kComProblem
+    };
+
+    TString fCommand;     // report string of the current system
+    Timer   fT;           // time of last report received
+    char    fStatus;      // current status of this system
+    int     fCCStatus;    // global status of CC
+
+    ComStatus_t fComStat; // communication status
+
+    virtual bool InterpreteCmd(TString cmd, TString str);
+
+    bool InterpreteStr(TString str);
+    bool Send(const char *str);
+
+public:
+    MCeCoCom::MCeCoCom(const char *cmd)
+        : MTcpIpIO(), fCommand(cmd), fStatus(0), fComStat(kNoCmdReceived)
+    {
+    }
+
+    void SetStatus(Byte_t s) { fStatus=s; }
+};
+
+#endif
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 2379)
@@ -0,0 +1,32 @@
+#include "MDriveCom.h"
+
+#include <iostream>
+
+using namespace std;
+
+bool MDriveCom::InterpreteCmd(TString cmd, TString str)
+{
+    if (cmd==(TString)"STOP!")
+    {
+        cout << "Stop! " << str << endl;
+        return true;
+    }
+
+    if (cmd==(TString)"RADEC")
+    {
+        cout << "RaDec: " << str << endl;
+        return true;
+    }
+    if (cmd==(TString)"ZDAZ")
+    {
+        cout << "ZdAz: " << str << endl;
+        return true;
+    }
+    if (cmd==(TString)"PREPS")
+    {
+        cout << "Preposs: " << str << endl;
+        return true;
+    }
+    cout << "Unknown Command: " << cmd << str << endl;
+    return false;
+}
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 2379)
@@ -0,0 +1,17 @@
+#ifndef COSY_MDriveCom
+#define COSY_MDriveCom
+
+#ifndef COSY_MCeCoCom
+#include "MCeCoCom.h"
+#endif
+
+class MDriveCom : public MCeCoCom
+{
+private:
+    bool InterpreteCmd(TString cmd, TString str);
+
+public:
+    MDriveCom() : MCeCoCom("DRIVE-REPORT") {}
+};
+
+#endif
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 2379)
@@ -0,0 +1,167 @@
+#include "MTcpIpIO.h"
+
+#include <unistd.h>    // usleep
+#include <iostream>
+
+#include <TSocket.h>
+#include <TServerSocket.h>
+
+using namespace std;
+
+    /*
+     enum ESockOptions {
+     kSendBuffer,        // size of send buffer
+     kRecvBuffer,        // size of receive buffer
+     kOobInline,         // OOB message inline
+     kKeepAlive,         // keep socket alive
+     kReuseAddr,         // allow reuse of local portion of address 5-tuple
+     kNoDelay,           // send without delay
+     kNoBlock,           // non-blocking I/O
+     kProcessGroup,      // socket process group (used for SIGURG and SIGIO)
+     kAtMark,            // are we at out-of-band mark (read only)
+     kBytesToRead        // get number of bytes to read, FIONREAD (read only)
+     };
+
+     enum ESendRecvOptions {
+     kDefault,           // default option (= 0)
+     kOob,               // send or receive out-of-band data
+     kPeek,              // peek at incoming message (receive only)
+     kDontBlock          // send/recv as much data as possible without blocking
+     };
+     */
+
+MTcpIpIO::MTcpIpIO() : MThread(false), fRxSocket(NULL), fServSock(NULL)
+{
+    fTxSocket = new TSocket("ceco", 7304);
+}
+
+MTcpIpIO::~MTcpIpIO()
+{
+    delete fTxSocket;
+}
+
+bool MTcpIpIO::Send(const char *msg)
+{
+    if (!fTxSocket->IsValid())
+        return false;
+
+    const UInt_t len = fTxSocket->SendRaw(msg, strlen(msg));
+    if (len<0)
+    {
+        cout << "ERROR - Sending Message" << endl;
+        return false;
+    }
+    if (len!=strlen(msg))
+    {
+        cout << "Send wrong number (" << len << ") of Bytes." << endl;
+        return false;
+    }
+
+    cout << "Tx: " << msg << flush;
+
+    return true;
+}
+
+bool MTcpIpIO::InterpreteStr(TString str)
+{
+    cout << "Rx: " << str << flush;
+}
+
+void *MTcpIpIO::Thread()
+{
+    cout << "Starting receiver..." << endl;
+
+    while (!HasStopFlag())
+    {
+        fServSock=new TServerSocket(7404, kTRUE);
+        if (!fServSock->IsValid())
+        {
+            cout << "ServerSocket not valid: ";
+            switch (fServSock->GetErrorCode())
+            {
+            case 0: cout << "No error." << endl; break;
+            case -1: cout << "low level socket() call failed." << endl; break;
+            case -2: cout << "low level bind() call failed." << endl; break;
+            case -3: cout << "low level listen() call failed." << endl; break;
+            default: cout << "Unknown." << endl; break;
+            }
+            delete fServSock;
+            usleep(5000000);
+            continue;
+        }
+
+        fServSock->SetOption(kNoBlock, 1);
+
+        cout << "Waiting for conntection on port 7404..." << endl;
+        while (!HasStopFlag() && (Long_t)fRxSocket<=0)
+        {
+            fRxSocket = fServSock->Accept();
+            if (fRxSocket==0)
+                cout << "Error: TServerSock::Accept" << endl;
+            usleep(1);
+        }
+
+        if (fRxSocket==NULL)
+        {
+            delete fServSock;
+            continue;
+        }
+
+        if (!fRxSocket->IsValid())
+        {
+            cout << "TSocket not valid..." << endl;
+            delete fServSock;
+            delete fRxSocket;
+            continue;
+        }
+
+        cout << "Connection established..." << endl;
+
+        fRxSocket->SetOption(kNoBlock, 1);
+
+        TString str;
+        while (!HasStopFlag())
+        {
+            char c;
+            const Int_t len = fRxSocket->RecvRaw(&c, 1);
+
+            // No data received (non-blocking mode)
+            if (len<0)
+            {
+                usleep(1);
+                continue;
+            }
+
+            // Data received with zero length!
+            if (len==0)
+            {
+                cout << "len==0" << endl;
+                continue;
+            }
+
+            // Data received
+            if (len>1)
+            {
+                cout << "Data too long!!!" << endl;
+                break;
+            }
+
+            // Data received (len==1)
+            if (c!='\n')
+            {
+                str += c;
+                continue;
+            }
+
+            // String completed
+            InterpreteStr(str);
+            str = "";
+        }
+        delete fServSock;
+        delete fRxSocket;
+    }
+
+    cout << "Receiver stopped..." << endl;
+
+    return NULL;
+}
Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 2379)
@@ -0,0 +1,29 @@
+#ifndef COSY_MTcpIpIO
+#define COSY_MTcpIpIO
+
+#ifndef COSY_MThread
+#include "MThread.h"
+#endif
+
+class TString;
+class TSocket;
+class TServerSocket;
+
+class MTcpIpIO : public MThread
+{
+private:
+    TSocket       *fTxSocket;
+    TSocket       *fRxSocket;
+    TServerSocket *fServSock;
+
+public:
+    MTcpIpIO();
+    ~MTcpIpIO();
+
+    virtual bool Send(const char *msg);
+    virtual bool InterpreteStr(TString str);
+
+    void *Thread();
+};
+
+#endif
Index: trunk/MagicSoft/Cosy/tcpip/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/Makefile	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/Makefile	(revision 2379)
@@ -0,0 +1,53 @@
+##################################################################
+#
+#   makefile
+# 
+#   for the MARS software
+#
+##################################################################
+# @maintitle
+
+# @code
+
+#
+#  please change all system depend values in the 
+#  config.mk.${OSTYPE} file 
+#
+#
+include ../Makefile.conf.$(OSTYPE)
+include ../Makefile.conf.general
+
+# @endcode 
+
+INCLUDES = -I. -I../base
+
+# @code 
+
+CINT     = TcpIp
+LIB      = tcpip.a
+
+#------------------------------------------------------------------------------
+
+.SUFFIXES: .c .cc .cxx .h .hxx .o 
+
+SRCFILES = MTcpIpIO.cc \
+	   MCeCoCom.cc \
+           MDriveCom.cc
+
+SRCS        = $(SRCFILES)
+HEADERS     = $(SRCFILES:.cc=.h)
+OBJS        = $(SRCFILES:.cc=.o) 
+CINTHEADERS = $(HEADERS)
+
+############################################################
+
+all: $(LIB)
+
+include ../Makefile.rules
+
+clean:	rmlib rmcint rmobjs rmcore
+
+mrproper:	clean rmbak
+
+# @endcode
+
Index: trunk/MagicSoft/Cosy/tcpip/TcpIpIncl.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/TcpIpIncl.h	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/TcpIpIncl.h	(revision 2379)
@@ -0,0 +1,3 @@
+#ifndef __CINT__
+
+#endif // __CINT__
Index: trunk/MagicSoft/Cosy/tcpip/TcpIpLinkDef.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/TcpIpLinkDef.h	(revision 2379)
+++ trunk/MagicSoft/Cosy/tcpip/TcpIpLinkDef.h	(revision 2379)
@@ -0,0 +1,7 @@
+#ifdef __CINT__
+
+#pragma link off all globals;
+#pragma link off all classes;
+#pragma link off all functions;
+
+#endif
