Index: trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8378)
+++ 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;
+}
