Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 8815)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 8816)
@@ -18,5 +18,6 @@
      - added virtual destructor as suggested by gcc 4
 
-   * devdrv/macs.[h,cc], devdrv/shaftencoder.[h,cc]:
+   * devdrv/macs.[h,cc], devdrv/shaftencoder.[h,cc], candrv/canopen.[h,cc],
+     candrv/network.[h,cc], candrv/nodedrv.[h,cc]:
      - replaced timeval_t* by a const reference in all arguments of
        member functions
@@ -27,4 +28,61 @@
    * videodev/FilterLed.cc:
      - initialized fImg as suggested by valgrind
+
+   * candrv/canopen.cc, candrv/network.cc, main/MTracking.cc,
+     main/MPpointing.cc, tcpip/MTcpIpIO.cc:
+     - replaced the lout-logging by gLog
+
+   * candrv/canopen.[h,cc]:
+     - derive from a generalized base class (Interface) now instead of
+       the specialized VmodIcan interface class
+     - added member function to start and stop the underlaying interface
+     - moved the RawCan handling function from VModIcan here
+       (HandleMessage)
+
+   * candrv/network.[h,cc]:
+     - start interface via CanOpen::Start and stop it via CanOpen::Stop
+     - removed obsolete arguments from constructor
+     - initialize fNodes and fNodeInitialized as suggested by valgrind
+
+   * gui/MGCoordinate.cc:
+     - SetOwner of fList to make sure that all gui object are properly deleted
+     - initialize the "label"-pointer as suggested by valgrind
+
+   * gui/MGCosy.h:
+     - removed obsolete forward class declaration of TGMenuBar
+
+   * gui/MGCoordinate.cc, gui/MGPngReader.cc
+     - SetOwner of fList to make sure that all gui object are properly deleted
+
+   * main/MTracking.[h,cc]:
+     - use new threading model (uses RunThread and CancelThread instead of 
+       Start Stop)
+     - changed return code of Thread from void* to Int_t#
+     - replaced chacking the HasStopFlag by TThread::CancelPoint calls
+     - renamed MPointing to MSlewing to get rid of a namespace conflic
+       with Mars' MPointing
+
+   * main/MainLinkDef.h:
+     - added the missing + for MCosy and MTracking
+     - replaced MPointing by MSlewing
+
+   * main/MPpointing.[h,cc]:
+     - renamed class to MSlewing
+     - removed
+
+   * main/MSlewing.[h,cc]:
+     - added
+
+   * tcpip/MTcpIpIO.[h,cc]:
+     - splitted MTcpIpIO into two base classes, one for input, one for output
+     - made the ports an argument
+     - derived from new threading class
+     - also added a static function to send a frame over a exclusively
+       opened port
+     - replaced old HasStopFlag by new IsThreadCanceled (this is used
+       instead of a CancelPoint to ensure correct cleanup)
+
+   * tcpip/MDriveCom.h:
+     - updated constructor
 
 
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 8815)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 8816)
@@ -10,4 +10,5 @@
 class MsgQueue;
 class Ring;
+class MLog;
 
 class MDriveCom : public MCeCoCom
@@ -39,6 +40,6 @@
 	kMonitoring = 0x40
     };
-  
-    MDriveCom(MsgQueue *q, MLog &out=gLog) : MCeCoCom("DRIVE-REPORT", out), fQueue(q) {}
+
+    MDriveCom(MsgQueue *q, MLog *out) : MCeCoCom(out), fQueue(q) {}
 
     bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er);
Index: /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8815)
+++ /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8816)
@@ -2,8 +2,10 @@
 
 #include <unistd.h>    // usleep
-#include <iostream>
 
 #include <TSocket.h>
 #include <TServerSocket.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
 
 #undef DEBUG
@@ -33,73 +35,45 @@
      */
 
-MTcpIpIO::MTcpIpIO(MLog &out)
-    : MThread(false), Log(out), fRxSocket(NULL), fServSock(NULL), fSendInterval(1000)
-{
-    fTxSocket = new TSocket("ceco", 7304);
+MTcpIpO::MTcpIpO(Int_t tx)
+{
+    fTxSocket = new TSocket("ceco", tx);
+}
+
+MTcpIpO::~MTcpIpO()
+{
+    // Now delete all TCP/IP objects
+    delete fTxSocket;
+}
+
+MTcpIpIO::MTcpIpIO(Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(tx)
+{
+    RunThread();
 }
 
 MTcpIpIO::~MTcpIpIO()
 {
-    //
-    // Make sure, that no loop waiting for connection
-    // is running anymore!
-    //
-    Stop();
-
-    //
-    // Now delete all TCP/IP objects
-    //
-    //cout << "Delete TxSocket " << fTxSocket << "..." << flush;
-    delete fTxSocket;
-    //cout << "Done." << endl;
-    if (fServSock)
-    {
-        //cout << "Delete ServSock " << fServSock << "..." << flush;
-        delete fServSock;
-        //cout << "Done." << endl;
-    }
-    if (fRxSocket)
-    {
-        //cout << "Delete RxSocket " << fRxSocket << "..." << flush;
-        delete fRxSocket;
-        //cout << "Done." << endl;
-    }
-}
-
-bool MTcpIpIO::Send(const char *msg, bool force=kFALSE)
-{
-
-    const MTime t(-1);
-
-    if ((double)t-(double)fTime<0.001*fSendInterval && !force) 
-          return true;
-    
-
-    if (lout.Lock("MTcpIpIO::Send"))
-    {
-        //const Int_t rc = lout.IsOutputDeviceEnabled(MLog::eGui);
-        //lout.DisableOutputDevice(MLog::eGui);
-        lout << msg << flush;
-        lout.UnLock("MTcpIpIO::Send");
-        //if (rc)
-        //    lout.EnableOutputDevice(MLog::eGui);
-    }
-
-    fTime = t;
-
-    if (!fTxSocket->IsValid())
-        return false;
-
-    const Int_t len = fTxSocket->SendRaw(msg, strlen(msg));
-    if (len<0)
+}
+
+bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len)
+{
+    if (!tx.IsValid())
+    {
+        //cout << "*!* Transmit socket invalid!" << endl;
+        return false;
+    }
+
+    const Int_t l = tx.SendRaw(msg, len);
+    if (l<0)
     {
         cout << "ERROR - Sending Message" << endl;
         return false;
     }
-    if (len!=(Int_t)strlen(msg))
-    {
-        cout << "Send wrong number (" << len << ") of Bytes." << endl;
-        return false;
-    }
+
+    if (l!=len)
+    {
+        cout << "Send wrong number (" << l << ") of Bytes." << endl;
+        return false;
+    }
+
 #ifdef DEBUG
     cout << "Tx: " << msg << flush;
@@ -109,4 +83,47 @@
 }
 
+bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len)
+{
+    //    R__LOCKGUARD2(myMutex);
+
+    cout << "Connecting to " << addr << ":" << port << endl;
+
+    // FIXME: Set tx-socket to nonblocking?
+    TSocket tx(addr, port);
+    //    return SendFrame(tx, msg, len);
+
+    if (!tx.IsValid())
+    {
+        //cout << "*!* Transmit socket invalid!" << endl;
+        return false;
+    }
+
+    cout << "Sending to " << addr << ":" << port << endl;
+
+    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
+    if (l<0)
+    {
+        cout << "ERROR - Sending Message" << endl;
+        return false;
+    }
+
+    if (l!=len)
+    {
+        cout << "Send wrong number (" << l << ") of Bytes." << endl;
+        return false;
+    }
+
+#ifdef DEBUG
+    cout << "Tx: " << msg << flush;
+#endif
+
+    return true;
+}
+
+bool MTcpIpO::Send(const char *msg, Int_t len)
+{
+    return SendFrame(*fTxSocket, msg, len);
+}
+
 bool MTcpIpIO::InterpreteStr(TString str)
 {
@@ -115,21 +132,70 @@
 }
 
-void MTcpIpIO::Clear()
-{
+void MTcpIpIO::ReadSocket(TSocket &rx)
+{
+    // Clear buffer!
     char c;
-    while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
+//    while (fRxSocket->RecvRaw(&c, 1)>0 && !HasStopFlag())
+    while (rx.RecvRaw(&c, 1)>0 && !IsThreadCanceled())
         usleep(1);
-}
-
-void *MTcpIpIO::Thread()
-{
-    cout << "Starting receiver..." << endl;
-
-    while (!HasStopFlag())
-    {
-        fServSock=new TServerSocket(7404, kTRUE);
+
+    TString str;
+    //        while (!HasStopFlag())
+    while (!IsThreadCanceled())
+    {
+        char c;
+        const Int_t len = rx.RecvRaw(&c, 1);
+
+        // No data received (non-blocking mode)
+        if (len<0)
+        {
+            usleep(1);
+            continue;
+        }
+
+        // Data received with zero length!
+        if (len==0)
+        {
+            // THIS MEANS CONNECTIION LOST!!!!
+            cout << "============> len==0 (CONNECTION LOST?)" << endl;
+            break; // This break is for TEST PURPOSE FIXME!!!
+            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 = "";
+    }
+}
+
+//void *MTcpIpIO::Thread()
+Int_t MTcpIpI::Thread()
+{
+    gLog << inf << "- Starting receiver on port " << fPortRx << "..." << endl;
+
+    TServerSocket *fServSock=NULL;
+    TSocket       *fRxSocket=NULL;
+
+//    while (!HasStopFlag())
+    while (!IsThreadCanceled())
+    {
+        fServSock=new TServerSocket(fPortRx, kTRUE);
         if (!fServSock->IsValid())
         {
-            cout << "ServerSocket not valid: ";
+            cout << "ServerSocket on port " << fPortRx << " invalid: ";
             switch (fServSock->GetErrorCode())
             {
@@ -142,5 +208,5 @@
             delete fServSock;
             fServSock=NULL;
-            usleep(5000000);
+            MyThreadX::Sleep(5000000);
             continue;
         }
@@ -148,10 +214,13 @@
         fServSock->SetOption(kNoBlock, 1);
 
-        cout << "Waiting for conntection on port 7404..." << endl;
-        while (!HasStopFlag() && (Long_t)fRxSocket<=0)
-        {
+        cout << "Waiting for connection on port " << fPortRx << "..." << endl;
+//        while (!HasStopFlag() && (Long_t)fRxSocket<=0)
+        while (!IsThreadCanceled() && (Long_t)fRxSocket<=0)
+        {
+            //TThread::CancelPoint();
+
             fRxSocket = fServSock->Accept();
             if (fRxSocket==0)
-                cout << "Error: TServerSock::Accept" << endl;
+                cout << "Error: TServerSock::Accept on port " << fPortRx << "." << endl;
             usleep(10);
         }
@@ -170,5 +239,5 @@
         if (!fRxSocket->IsValid())
         {
-            cout << "TSocket not valid..." << endl;
+            cout << "TSocket invalid on port " << fPortRx << "." << endl;
             delete fServSock;
             delete fRxSocket;
@@ -178,48 +247,12 @@
         }
 
-        cout << "Connection established..." << endl;
+        cout << "Connection established on port " << fPortRx << "." << endl;
 
         fRxSocket->SetOption(kNoBlock, 1);
 
-        Clear();
-
-        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 = "";
-        }
+        // ------ IDENTICAL UP TO HERE ------
+
+        ReadSocket(*fRxSocket);
+
         delete fServSock;
         delete fRxSocket;
@@ -228,6 +261,7 @@
     }
 
-    cout << "Receiver stopped..." << endl;
-
-    return NULL;
-}
+    gLog << inf << "- Receiver stopped on port " << fPortRx << "." << endl;
+
+    return 0;
+//    return NULL;
+}
Index: /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 8815)
+++ /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 8816)
@@ -2,9 +2,6 @@
 #define COSY_MTcpIpIO
 
-#ifndef COSY_MThread
+#ifndef MARS_MThread
 #include "MThread.h"
-#endif
-#ifndef COSY_Log
-#include "log.h"
 #endif
 #ifndef MARS_MTime
@@ -12,28 +9,52 @@
 #endif
 
+
 class TString;
 class TSocket;
 class TServerSocket;
 
-class MTcpIpIO : public MThread, public Log
+// A generalized class for receiving over tcp/ip
+class MTcpIpI : public MyThreadX
 {
 private:
-    TSocket       *fTxSocket;
-    TSocket       *fRxSocket;
-    TServerSocket *fServSock;
+    Int_t fPortRx;
 
-    MTime fTime;
-    Int_t fSendInterval; // [ms]
+    Int_t Thread();
 
-    void Clear();
+    virtual void ReadSocket(TSocket &rx) = 0;
 
 public:
-    MTcpIpIO(MLog &out=gLog);
+    MTcpIpI(Int_t rx) : MyThreadX(Form("MTcpIpI::%d", rx)), fPortRx(rx) { /*RunThread();*/ }
+    ~MTcpIpI() { CancelThread(); }
+};
+
+
+// A generalized class for sending over tcp/ip
+class MTcpIpO
+{
+private:
+    TSocket *fTxSocket;
+
+public:
+    MTcpIpO(Int_t tx);
+    ~MTcpIpO();
+
+    static bool SendFrame(TSocket &tx, const char *msg, int len);
+    static bool SendFrame(const char *addr, int port, const char *msg, int len);
+
+    bool Send(const char *msg, int len);
+};
+
+// This class es espcially meant to receive and send ascii messages
+class MTcpIpIO : public MTcpIpI, public MTcpIpO
+{
+private:
+    void ReadSocket(TSocket &rx);
+
+public:
+    MTcpIpIO(Int_t tx, Int_t rx);
     ~MTcpIpIO();
 
-    virtual bool Send(const char *msg, bool force);
     virtual bool InterpreteStr(TString str);
-
-    void *Thread();
 };
 
