Changeset 12598 for trunk/Cosy


Ignore:
Timestamp:
11/21/11 13:08:27 (13 years ago)
Author:
tbretz
Message:
Implemented a new class MTcpIpFact using a duplex communication for FACT
Location:
trunk/Cosy/tcpip
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cosy/tcpip/MTcpIpIO.cc

    r10031 r12598  
    6262}
    6363
    64 MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout) : MTcpIpI(rx, timeout), MTcpIpO(addr, tx)
     64MTcpIpCC::MTcpIpCC(Int_t rx, UInt_t timeout) : MTcpIpI(rx, timeout)
    6565{
    6666    MTcpIpI::RunThread();
     67}
     68
     69MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout) : MTcpIpCC(rx, timeout), MTcpIpO(addr, tx)
     70{
    6771}
    6872
     
    277281}
    278282
    279 bool MTcpIpIO::InterpreteStr(TString str)
     283bool MTcpIpCC::InterpreteStr(TString str)
    280284{
    281285    cout << "Rx: " << str << flush;
     
    283287}
    284288
    285 Bool_t MTcpIpIO::ReadSocket(TSocket &rx)
     289Bool_t MTcpIpCC::ReadSocket(TSocket &rx)
    286290{
    287291    TString str;
     
    308312
    309313        // Data received (len==1)
    310         if (c!='\n')
     314        if (c!='\n' && c!=0)
    311315        {
    312316            str += c;
     
    331335    }
    332336
     337    fRxMutex.Lock();
     338
    333339    fConnectionEstablished = kTRUE;
     340    fRxSocket = &sock;
     341
     342    fRxMutex.UnLock();
    334343
    335344    MTimeout timeout(fTimeout);
     
    409418        gLog << all << MTime(-1) << " Connection established to " << MTcpIpO::GetSocketAddress(*socket) << "..." << endl;
    410419
    411         if (!WaitForData(*socket))
    412             fConnectionEstablished = kFALSE;
     420        WaitForData(*socket);
     421
     422        fRxMutex.Lock();
     423
     424        fRxSocket = 0;
     425        fConnectionEstablished = kFALSE;
     426
     427        fRxMutex.UnLock();
    413428
    414429#ifdef DEBUG
     
    455470
    456471    return 0;
     472}
     473
     474bool MTcpIpFact::Send(const char *msg, int len)
     475{
     476    const Int_t mtx = fRxMutex.TryLock();
     477    if (mtx==13)
     478        gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl;
     479
     480    // If Mutex cannot be locked, i.e. we are currently reopening
     481    // the send socket we cannot wait, because we would block the
     482    // executing threrad.
     483    if (mtx)
     484        return false;
     485
     486    const bool rc = fRxSocket ? MTcpIpO::SendFrame(*fRxSocket, msg, len) : false;
     487
     488    fRxMutex.UnLock();
     489
     490    return rc;
    457491}
    458492
  • trunk/Cosy/tcpip/MTcpIpIO.h

    r10031 r12598  
    2020class MTcpIpI : public MThread
    2121{
     22protected:
     23    TSocket *fRxSocket;
     24    TMutex   fRxMutex;
     25
    2226private:
    2327    Int_t  fPortRx;     // Port on which to listen for connections
     
    3640
    3741public:
    38     MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
     42    MTcpIpI(Int_t rx, UInt_t timeout=5000) : MThread(Form("MTcpIpI::%d", rx)), fRxSocket(0), fPortRx(rx), fTimeout(timeout), fConnectionEstablished(kFALSE) { /*RunThread();*/ }
    3943    ~MTcpIpI() { CancelThread(); }
    4044
     
    6569};
    6670
    67 // This class es espcially meant to receive and send ascii messages
    68 class MTcpIpIO : public MTcpIpI, public MTcpIpO
     71class MTcpIpCC : public MTcpIpI
    6972{
    7073private:
     
    7275
    7376public:
    74     MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
     77    MTcpIpCC(Int_t rx, UInt_t timeout=5000);
    7578
    7679    virtual bool InterpreteStr(TString str);
     80};
     81
     82// This class es espcially meant to receive and send ascii messages
     83class MTcpIpIO : public MTcpIpCC, public MTcpIpO
     84{
     85
     86public:
     87    MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
    7788};
    7889
     
    91102};
    92103
     104class MTcpIpFact : public MTcpIpCC
     105{
     106private:
     107
     108public:
     109    MTcpIpFact(const char *dumm1, Int_t dummy2, Int_t rx, UInt_t timeout) : MTcpIpCC(rx, timeout)
     110    {
     111    }
     112
     113    bool Send(const char *msg, int len);
     114};
     115
     116
    93117#endif
Note: See TracChangeset for help on using the changeset viewer.