Changeset 10031 for trunk


Ignore:
Timestamp:
10/21/10 16:16:52 (14 years ago)
Author:
tbretz
Message:
Changed connection from SPS to a client-only connection.
Location:
trunk/Cosy
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cosy/Changelog

    r10026 r10031  
    3131   * */Makefile, Makefile*:
    3232     - removed necessity of defining OSTYPE
     33
     34   * Makefile.rules:
     35     - replaced cvs by svn
     36
     37   * tpoint/TPointGui.cc:
     38     - included stdlib.h
     39
     40   * devdrv/dkc.[h,cc]:
     41     - added five-byte error massages from IndraDrive
     42     + for errnum and errinf are no coded with 1+5 byte
     43       instead of 2+4
     44     - added UPS information to PDO3, consequently changed fStatusPDO3
     45       from BYTE_t to WORD_t
     46     + fixed in the SPS that the armed status was not transmitted in the
     47       correct byte
     48     - adapted inline functions accordingly
     49
     50   * cosy.cc:
     51     - added a debug line to see when the gui is up
     52     - removed receiver port from instantiation of Ethernet
     53
     54   * candrv/ethernet.[h,cc]:
     55     - replaced bidirectional Tcp/Ip sockets by an unidirectional one,
     56       thus only one party (cosy) connects (to the SPS)
     57     - removed dependancy on MTcpIpI and replaced by new class MTcpIpOI
     58     - removed obsolete fTxAddress and fTxPort
     59     - only start thread if it is not already running to prevent parallel
     60       connection attempts on the same socket
     61     + IsConnectionstablished is not evaluated yet
     62
     63   * tcpip/MTcpIpIO.[h,cc]:
     64     - removed empty destructor of MTcpIpIO
     65     - changed typical reconnection time from 30s to 3s. This seems
     66       more reasonably adapted to human behaviour
     67     - added new class MTcpIpOI which is a bi-directional communication
     68       channel via TCP/IP
     69
     70   * main/MCosy.cc:
     71     - correctly propagate the pdo3 status
    3372
    3473
  • trunk/Cosy/candrv/ethernet.cc

    r9439 r10031  
    5555//  and switch the can bus communication on
    5656//
    57 Ethernet::Ethernet(const char *addr, const int tx, const int rx, CanOpen *receiver)
    58     : MTcpIpI(rx), /*MTcpIpO(addr, tx),*/ Interface(receiver), fTxAddress(addr), fTxPort(tx)
     57Ethernet::Ethernet(const char *addr, const int tx, CanOpen *receiver)
     58    : MTcpIpOI(addr, tx), Interface(receiver)
    5959{
    6060    gLog << inf2 << "- Ethernet initialized." << endl;
     
    6868Ethernet::~Ethernet()
    6969{
    70     MTcpIpI::CancelThread();
     70    CancelThread();
    7171    gLog << inf2 << "- Ethernet stopped." << endl;
    7272}
     
    7979
    8080    Message msg;
     81    msg.len = 0;
    8182    msg.cmd = M_BCAN_RX_ind;
    8283    msg.data[0] = 0;
    8384    msg.data[1] = 0;
    8485
    85     const TString address = MTcpIpO::GetSocketAddress(rx);
    86 
    87     while (!MTcpIpI::IsThreadCanceled())
     86    const TString address = GetSocketAddress(rx);
     87
     88    while (!IsThreadCanceled())
    8889    {
    8990        unsigned char c;
     
    280281    st.Start();
    281282#endif
    282     MTcpIpO::SendFrame(fTxAddress, fTxPort, (char*)(msg.data+1), msg.len-1);
    283 //    Send((char*)(msg.data+1), msg.len-1);
     283
     284    Send((char*)(msg.data+1), msg.len-1);
     285
    284286#ifdef DEBUG
    285287    st.Print();
    286288#endif
    287     //Send((char*)(msg.data+1), msg.len-1);
    288 
    289     /*
    290     const WORD_t desc = MsgDescr(cobid, 8, rtr);
    291 
    292     Message msg;
    293 
    294     msg.cmd = M_BCAN_TX_req;
    295 
    296     msg.len = 12;
    297     msg.data[0]  = 0;
    298     msg.data[1]  = 0;
    299     msg.data[2]  = word_to_msb(desc);
    300     msg.data[3]  = word_to_lsb(desc);
    301 
    302     memcpy(&msg.data[4], m, 8);
    303 
    304     while (!Send(&msg));*/
    305 }
     289}
  • trunk/Cosy/candrv/ethernet.h

    r9439 r10031  
    1010#endif
    1111
    12 class Ethernet : public MTcpIpI, /*public MTcpIpO,*/ public Interface
     12class Ethernet : public MTcpIpOI, public Interface
    1313{
    1414private:
    15     TString fTxAddress;
    16     Int_t   fTxPort;
    17 
    1815    // Send interface based on MTcpIpI
    1916    Bool_t ReadSocket(TSocket &rx);
    2017
    2118    // Start/stop communication inherited from Interface
    22     void Start() { MTcpIpI::RunThread(); }
    23     void Stop()  { MTcpIpI::CancelThread(); }
    24 
    25     Bool_t HasConnection() const { return MTcpIpI::IsConnectionEstablished(); }
     19    void Start() { if (!IsThreadRunning()) RunThread(); }
     20    void Stop()  { CancelThread(); }
    2621
    2722public:
    28     Ethernet(const char *addr, const int tx, const int rx, CanOpen *receiver);
     23    Ethernet(const char *addr, const int tx, CanOpen *receiver);
    2924    virtual ~Ethernet();
    3025
  • trunk/Cosy/cosy.cc

    r9443 r10031  
    253253    com->SetMsgQueue(cosy);
    254254
    255     Interface *interface = new Ethernet(sps, 5357, 5358, cosy);
     255    Interface *interface = new Ethernet(sps, 5357, cosy);
    256256    // Interface *interface = new VmodIcan(cosy, "/dev/dpm_00", 125);
    257257
     
    259259
    260260    cosy->Start(env);
     261
     262    gLog << all << "- MCosy started." << endl;
    261263
    262264    // FIXME: Is this the right position?
  • trunk/Cosy/tcpip/MTcpIpIO.cc

    r9582 r10031  
    6767}
    6868
    69 MTcpIpIO::~MTcpIpIO()
    70 {
    71 }
    72 
    7369TString MTcpIpO::GetSocketAddress(const TSocket &s)
    7470{
     
    192188
    193189        MThread::Sleep(wait); // Wait a ms
    194         if (wait<30000000) // 30s
     190        if (wait<3000000) // 3s
     191            wait *= 2;
     192    }
     193
     194    return 1;
     195}
     196
     197Int_t MTcpIpOI::Thread()
     198{
     199    fConnectionEstablished = kFALSE;
     200
     201    const TInetAddress &a = fTxSocket->GetInetAddress();
     202    if (!a.IsValid())
     203    {
     204        gLog << err << "- MTcpIpOI::Thread - ERROR: Send socket address invalid." << endl;
     205        return 0;
     206    }
     207
     208    gLog << inf << "- MTcpIpOI::Thread created connecting to " << a.GetHostAddress() << ":" << fPortTx << endl;
     209
     210    Int_t wait = 1000; /// Wait a ms
     211
     212    MTimeout timeout(fTimeout);
     213
     214    while (!IsThreadCanceled())
     215    {
     216        if (fTxSocket->IsValid())
     217        {
     218            fConnectionEstablished = kTRUE;
     219            fTimeout = 1000;
     220
     221            // Get connection on port fPortRx and redirected
     222            // Check for pending data (every ms)
     223            switch (fTxSocket->Select(TSocket::kRead, 1))
     224            {
     225            case kTRUE:  // Data pending... go on reading
     226                if (!ReadSocket(*fTxSocket))
     227                {
     228                    gLog << warn << MTime(-1) << " WARNING - Connection lost to " << MTcpIpO::GetSocketAddress(*fTxSocket) << endl;
     229                    fTxSocket->Close();
     230                    continue;
     231                }
     232                timeout.Start(fTimeout);
     233                continue;
     234
     235            case kFALSE: // Time out, no data yet, go on waiting
     236              if (timeout.HasTimedOut())
     237              {
     238                  gLog << warn << MTime(-1) << " WARNING - Connection to " << MTcpIpO::GetSocketAddress(*fTxSocket) << " timed out after " << fTimeout << "ms." << endl;
     239                  fTxSocket->Close();
     240                  continue;
     241              }
     242              continue;
     243
     244            default:  // Error occurance
     245                gLog << err << "TSocket::Select returned an error: " << strerror(errno) << endl;
     246                fTxSocket->Close();
     247                continue;
     248            }
     249        }
     250
     251        fConnectionEstablished = kFALSE;
     252
     253#ifdef DEBUG
     254        cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
     255#endif
     256
     257        fMutex.Lock();
     258
     259        delete fTxSocket;
     260
     261        fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
     262        fTxSocket->SetOption(kNoBlock, 1);
     263
     264        fMutex.UnLock();
     265
     266        timeout.Start(fTimeout);
     267
     268        if (fTxSocket->IsValid())
     269            continue;
     270
     271        MThread::Sleep(wait); // Wait a ms
     272        if (wait<3000000) // 3s
    195273            wait *= 2;
    196274    }
  • trunk/Cosy/tcpip/MTcpIpIO.h

    r9582 r10031  
    4646class MTcpIpO : public MThread
    4747{
    48 private:
     48protected:
    4949    TSocket *fTxSocket;
    5050    TMutex   fMutex;
     
    7373public:
    7474    MTcpIpIO(const char *addr, Int_t tx, Int_t rx, UInt_t timeout=5000);
    75     ~MTcpIpIO();
    7675
    7776    virtual bool InterpreteStr(TString str);
    7877};
    7978
     79class MTcpIpOI : public MTcpIpO
     80{
     81private:
     82    Int_t Thread();
     83    virtual Bool_t ReadSocket(TSocket &rx) = 0;
     84
     85    Bool_t fConnectionEstablished;
     86    Int_t  fTimeout;    // [ms] Timeout to listen for data to be received
     87public:
     88    MTcpIpOI(const char *addr, Int_t tx, UInt_t timeout=5000) : MTcpIpO(addr, tx), fConnectionEstablished(kFALSE), fTimeout(timeout) { }
     89
     90    Bool_t IsConnectionEstablished() const { return fConnectionEstablished; }
     91};
     92
    8093#endif
Note: See TracChangeset for help on using the changeset viewer.