Ignore:
Timestamp:
05/09/09 13:48:12 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Cosy/tcpip
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc

    r9435 r9439  
    104104    cout << "Zd/Az: " << zd << "/" << az << "  ";
    105105    cout << "Ra/Dec: " << ra/15 << "h/" << dec << "  ";
    106     cout << "SolRad: " << solar << "W/m²" << endl;
     106    //cout << "SolRad: " << solar << "W/m²";
     107    cout << endl;
    107108
    108109    fComStat = kCmdReceived;
     
    122123        return InterpreteReport(str);
    123124
    124     const bool rc = InterpreteCmd(cmd, str.Strip(TString::kBoth));
    125     fComStat = rc ? kCmdReceived : kComProblem;
    126     return rc;
     125    return InterpreteCmd(cmd, str.Strip(TString::kBoth));
    127126}
    128127
    129128bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force)
    130129{
    131     MTime t;
    132     t.Now();
    133130
    134131    UShort_t y1, y2, ms1, ms2;
    135132    Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;
    136133
     134    fT.GetDate(y2, mon2, d2);
     135    fT.GetTime(h2, m2, s2, ms2);
     136
     137    MTime t(-1);
    137138    t.GetDate(y1, mon1, d1);
    138139    t.GetTime(h1, m1, s1, ms1);
    139140
    140     fT.GetDate(y2, mon2, d2);
    141     fT.GetTime(h2, m2, s2, ms2);
     141    if (t-fT>20)
     142        fComStat = kComProblem;
    142143
    143144    const TString msg =
     
    152153    // Send report to CC
    153154    const bool rc = Send(msg.Data(), msg.Length());
    154     fComStat = rc ? kNoCmdReceived : kComProblem;
    155155
    156156    // Write report to stream
  • trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h

    r9432 r9439  
    1919    enum ComStatus_t
    2020    {
    21         kCmdReceived,
    22         kNoCmdReceived,
    23         kComProblem
     21        kCmdReceived = 1,
     22        kComProblem  = 2
    2423    };
    2524
     
    5251    MCeCoCom(const char *addr, const int tx=7304, const int rx=7404, MLog *out=NULL)
    5352        : MTcpIpIO(addr, tx, rx), fOut(out), fStatus(0),
    54         fTelescope(1), fComStat(kNoCmdReceived),
     53        fTelescope(1), fComStat(kComProblem),
    5554        fHumidity(0), fTemperature(0), fWindSpeed(0), fSolarRadiation(-1),
    5655        fAlarmCounter(0)
  • trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc

    r9435 r9439  
    77#include "MString.h"
    88#include "Ring.h"
     9#include "Led.h"
     10
     11#include "MLog.h"
     12#include "MLogManip.h"
    913
    1014using namespace std;
     
    4549    if (!ReadPosition(str, ra, dec))
    4650    {
    47         cout << "ERROR - Reading position from RADEC" << endl;
     51        gLog << err << "ERROR - Reading position from RADEC" << endl;
    4852        return false;
    4953    }
    5054    if (!str.IsNull())
    5155    {
    52         cout << "ERROR - Too many bytes in command RADEC" << endl;
    53         return false;
    54     }
    55 
    56     cout << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
     56        gLog << err << "ERROR - Too many bytes in command RADEC" << endl;
     57        return false;
     58    }
     59
     60    gLog << all << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
    5761
    5862    ra *= 15; // h -> deg
     
    7276    if (!ReadPosition(str, ra, dec))
    7377    {
    74         cout << "ERROR - Reading position from GRB" << endl;
     78        gLog << err << "ERROR - Reading position from GRB" << endl;
    7579        return false;
    7680    }
    7781    if (!str.IsNull())
    7882    {
    79         cout << "ERROR - Too many bytes in command GRB" << endl;
    80         return false;
    81     }
    82 
    83     cout << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
     83        gLog << err << "ERROR - Too many bytes in command GRB" << endl;
     84        return false;
     85    }
     86
     87    gLog << all << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
    8488
    8589    ra *= 15; // h -> deg
     
    99103    if (!ReadPosition(str, zd, az))
    100104    {
    101         cout << "ERROR - Reading position from ZDAZ" << endl;
     105        gLog << err << "ERROR - Reading position from ZDAZ" << endl;
    102106        return false;
    103107    }
     
    105109    if (!str.IsNull())
    106110    {
    107         cout << "ERROR - Too many bytes in command ZDAZ" << endl;
    108         return false;
    109     }
    110 
    111     cout << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
     111        gLog << err << "ERROR - Too many bytes in command ZDAZ" << endl;
     112        return false;
     113    }
     114
     115    gLog << all << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
    112116
    113117    ZdAz za(zd, az);
     
    125129    if (str.IsNull())
    126130    {
    127         cout << "ERROR - No identifier for preposition (PREPS) given." << endl;
     131        gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
    128132        return false;
    129133    }
    130134    if (str.First(' ')>=0)
    131135    {
    132         cout << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
     136        gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
    133137        return false;
    134138    }
     
    136140    str.ToLower();
    137141
    138     cout << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
     142    gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
    139143
    140144    //cout << "MDriveCom - TRACK... start." << endl;
     
    147151bool MDriveCom::CommandTPOINT(TString &str)
    148152{
    149     cout << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
    150 
    151     if (fQueue)
    152         fQueue->PostMsg(WM_TPOINT);//, (void*)str.Data(), str.Length()+1);
     153    gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
     154
     155    TObjArray *arr = str.Tokenize(' ');
     156
     157    if (arr->GetEntries()!=2)
     158    {
     159        delete arr;
     160        gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
     161        return false;
     162    }
     163
     164    delete arr;
     165
     166    if (fQueue)
     167        fQueue->PostMsg(WM_STARGTPOINT, (void*)str.Data(), str.Length()+1);//, (void*)str.Data(), str.Length()+1);
    153168
    154169    return true;
     
    157172bool MDriveCom::CommandSTGMD(TString &str)
    158173{
    159     cout << "CC-COMMAND " << MTime(-1) << "STGMD" << endl;
    160 
    161     if (fQueue)
    162         fQueue->PostMsg(WM_STARGMODE);//, (void*)str.Data(), str.Length()+1);
     174    gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
     175
     176    bool on = str=="ON";
     177
     178    if (fQueue)
     179        fQueue->PostMsg(WM_STARGMODE, &on, sizeof(bool));//, (void*)str.Data(), str.Length()+1);
    163180
    164181    return true;
     
    170187    if (str.IsNull())
    171188    {
    172         cout << "ERROR - No identifier for ARM command." << endl;
     189        gLog << err << "ERROR - No identifier for ARM command." << endl;
    173190        return false;
    174191    }
    175192    if (str.First(' ')>=0)
    176193    {
    177         cout << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
     194        gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
    178195        return false;
    179196    }
     
    182199    if (str!="lock" && str!="unlock")
    183200    {
    184         cout << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
    185         return false;
    186     }
    187 
    188     cout << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
     201        gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
     202        return false;
     203    }
     204
     205    gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
    189206
    190207    bool lock = str=="lock";
     
    200217    {
    201218        //cout << "MDriveCom - WAIT... start." << endl;
    202         cout << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
     219        gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
    203220        if (fQueue)
    204221            fQueue->PostMsg(WM_WAIT);
     
    210227    {
    211228        //cout << "MDriveCom - STOP!... start." << endl;
    212         cout << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
     229        gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
    213230        if (fQueue)
    214231            fQueue->PostMsg(WM_STOP);
     
    240257    if (cmd.IsNull() && str.IsNull())
    241258    {
    242         cout << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
    243         return false;
    244     }
    245 
    246     cout << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
     259        gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
     260        return false;
     261    }
     262
     263    gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
    247264    return false;
    248265}
     
    258275}
    259276
    260 bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed)
     277bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
    261278{
    262279    // rd [rad]
     
    294311    str += MString::Format("%08.3f ", er.Az());
    295312    str += armed ? "1 " : "0 ";
     313    str += MString::Format("%d ", stargmd);           // Starguider mode: 0=none, 1=starguider, 2=starguider off
    296314
    297315    return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
     
    336354}
    337355
    338 bool MDriveCom::SendTPoint(UInt_t stat)
    339 {
    340     //const MTime t(-1);
    341 
     356bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
     357                           const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
     358                           const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
     359                           Int_t numstars, Int_t numcor, Float_t bright)
     360{
    342361    SetStatus(stat);
    343     /*
    344      TString str;
    345      str += name;  // star name
    346      str += " ";
    347      str += nominalaz;
    348      str += nominalel;
    349      str += realaz;
    350      str += realel;
    351      str += nomra;
    352      str += nomdec;
    353      str += diffaz;
    354      str += diffel;
    355      str += mjd;
    356      str += numleds;
    357      str += artmag;
    358      */
    359     return SendRep("TPOINT-REPORT", "", kTRUE);
    360 }
     362
     363    TString str = type;
     364    str += MString::Format(" %8.4f ", za0.Az());
     365    str += MString::Format("%8.4f ", za0.Alt());
     366    str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
     367    str += MString::Format("%8.4f ", 90-za1.Zd());
     368    str += MString::Format("%8.4f ", xy.X());
     369    str += MString::Format("%8.4f ", xy.Y());
     370    str += MString::Format("%8.4f ", dzd);
     371    str += MString::Format("%8.4f ", daz);
     372    str += MString::Format("%12.6f ", t.GetMjd());
     373    str += MString::Format("%f ", center.GetMag());
     374    str += MString::Format("%f ", star.GetMag());
     375    str += MString::Format("%f ", center.GetX());
     376    str += MString::Format("%f ", center.GetY());
     377    str += MString::Format("%f ", star.GetX());
     378    str += MString::Format("%f ", star.GetY());
     379    str += numleds;
     380    str += " ";
     381    str += numrings;
     382    str += " ";
     383    str += numstars;
     384    str += " ";
     385    str += numcor;
     386    str += " ";
     387    str += bright;
     388    str += " ";
     389    str += mag;
     390    str += " ";
     391    str += name;
     392
     393    return SendRep("TPOINT-REPORT", str, kTRUE);
     394}
  • trunk/MagicSoft/Cosy/tcpip/MDriveCom.h

    r9435 r9439  
    77
    88class RaDec;
     9class AltAz;
    910class ZdAz;
    1011class MsgQueue;
    1112class Ring;
     13class Led;
    1214class MLog;
     15class MTime;
     16class TVector2;
    1317
    1418class MDriveCom : public MCeCoCom
     
    4953
    5054
    51     bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed);
     55    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd);
    5256    bool SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Int_t numleds, Int_t numrings);
    53     bool SendTPoint(UInt_t stat);
     57//    bool SendTPoint(char type);
     58    bool SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
     59                    const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
     60                    const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
     61                    Int_t numstars=0, Int_t numcor=0, Float_t bright=0);
    5462    bool SendStatus(const char *stat);
    5563};
  • trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc

    r9432 r9439  
    4343MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx)
    4444{
    45     gLog << inf2 << "- Open send socket to " << addr << ":" << tx << endl;
     45    // was "inf2" but nobody knows what the prg is doing if it times out
     46    gLog << all << "- Open send socket to " << addr << ":" << tx << endl;
     47
    4648    fTxSocket = new TSocket(addr, tx);
     49    fTxSocket->SetOption(kNoBlock, 1);
     50
     51    MTcpIpO::RunThread();
    4752}
    4853
    4954MTcpIpO::~MTcpIpO()
    5055{
     56    CancelThread();
     57
    5158    // Now delete all TCP/IP objects
    5259    delete fTxSocket;
     
    5562MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx)
    5663{
    57     RunThread();
     64    MTcpIpI::RunThread();
    5865}
    5966
     
    7481}
    7582
     83/*
    7684TString MTcpIpO::GetSocketAddress() const
    7785{
     86    TLockGuard guard(const_cast<TMutex*>(&fMutex));
    7887    return GetSocketAddress(*fTxSocket);
    7988}
    80 
     89*/
    8190bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len)
    8291{
    8392    if (!tx.IsValid())
    84     {
    85         //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;
    8693        return false;
    87     }
    8894
    8995#ifdef DEBUG
     
    9197#endif
    9298
    93     const Int_t l = tx.SendRaw(msg, len);
     99    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
     100
     101    // Frame not sent, EWOULDBLOCK
     102    if (l==-4)
     103    {
     104        gLog << err << "ERROR - Sending to " << GetSocketAddress(tx) << " would block." << endl;
     105/*--->*/        tx.Close(); // ?????
     106        return false;
     107    }
     108
    94109    if (l<0)
    95110    {
     
    100115    if (l!=len)
    101116    {
    102         gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
     117        gLog << err << "ERROR - Could only sent " << l << " out of "  << len << " bytes to " << GetSocketAddress(tx) << endl;
    103118        return false;
    104119    }
     
    109124bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len)
    110125{
    111     //    R__LOCKGUARD2(myMutex);
    112126#ifdef DEBUG
    113127    cout << "Connecting to " << addr << ":" << port << endl;
     
    117131    TSocket tx(addr, port);
    118132    return SendFrame(tx, msg, len);
    119 /*
    120     if (!tx.IsValid())
    121     {
    122         gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl;
     133}
     134
     135bool MTcpIpO::Send(const char *msg, Int_t len)
     136{
     137    const Int_t mtx = fMutex.TryLock();
     138    if (mtx==13)
     139        gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl;
     140
     141    // If Mutex cannot be locked, i.e. we are currently reopening
     142    // the send socket we cannot wait, because we would block the
     143    // executing threrad.
     144    if (mtx)
    123145        return false;
    124     }
    125 
    126     gLog << dbg << "Sending to " << addr << ":" << port << endl;
    127 
    128     const Int_t l = tx.SendRaw(msg, len, kDontBlock);
    129     if (l<0)
    130     {
    131         gLog << err << "ERROR - Sending TCP/IP frame to " << addr << ":" << port << endl;
    132         return false;
    133     }
    134 
    135     if (l!=len)
    136     {
    137         gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << addr << ":" << port << endl;
    138         return false;
    139     }
    140 
    141 #ifdef DEBUG
    142     cout << "Tx: " << msg << flush;
    143 #endif
    144 
    145     return true;
    146     */
    147 }
    148 
    149 bool MTcpIpO::Send(const char *msg, Int_t len)
    150 {
    151     if (!fTxSocket->IsValid())
    152     {
    153         const TInetAddress &a = fTxSocket->GetInetAddress();
    154         if (!a.IsValid())
    155             return false;
     146
     147    const bool rc = SendFrame(*fTxSocket, msg, len);
     148
     149    fMutex.UnLock();
     150
     151    return rc;
     152}
     153
     154Int_t MTcpIpO::Thread()
     155{
     156    const TInetAddress &a = fTxSocket->GetInetAddress();
     157    if (!a.IsValid())
     158    {
     159        gLog << err << "ERROR: Send socket address invalid." << endl;
     160        return 0;
     161    }
     162
     163    Int_t wait = 1000; /// Wait a ms
     164
     165    while (!IsThreadCanceled())
     166    {
     167        if (fTxSocket->IsValid())
     168        {
     169            MThread::Sleep(1000); // Wait a ms
     170            wait = 1000;
     171            continue;
     172        }
     173
     174
    156175#ifdef DEBUG
    157176        cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
    158177#endif
     178
     179        fMutex.Lock();
     180
    159181        delete fTxSocket;
     182
    160183        fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
    161     }
    162 
    163     return SendFrame(*fTxSocket, msg, len);
     184        fTxSocket->SetOption(kNoBlock, 1);
     185
     186        fMutex.UnLock();
     187
     188        if (fTxSocket->IsValid())
     189            continue;
     190
     191        MThread::Sleep(wait); // Wait a ms
     192        if (wait<30000000) // 30s
     193            wait *= 2;
     194    }
     195
     196    return 1;
    164197}
    165198
     
    174207    TString str;
    175208
    176     while (!IsThreadCanceled())
     209    while (!MTcpIpI::IsThreadCanceled())
    177210    {
    178211        char c;
  • trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h

    r9432 r9439  
    22#define COSY_MTcpIpIO
    33
    4 #ifndef MARS_MThread
    5 #include "MThread.h"
     4#ifndef COSY_MsgQueue
     5#include "msgqueue.h"
    66#endif
    77#ifndef MARS_MTime
    88#include "MTime.h"
     9#endif
     10#ifndef ROOT_TMutex
     11#include <TMutex.h>
    912#endif
    1013
     
    2225
    2326    Bool_t fConnectionEstablished;
     27
     28    MTime  fLast;
    2429
    2530    Bool_t WaitForData(TSocket &sock);
     
    3944
    4045// A generalized class for sending over tcp/ip
    41 class MTcpIpO
     46class MTcpIpO : public MThread
    4247{
    4348private:
    4449    TSocket *fTxSocket;
     50    TMutex   fMutex;
    4551
    4652    Int_t fPortTx;
     53
     54    Int_t Thread();
    4755
    4856public:
     
    5462    static bool    SendFrame(const char *addr, int port, const char *msg, int len);
    5563
    56     TString GetSocketAddress() const;
    57     bool    Send(const char *msg, int len);
     64    bool Send(const char *msg, int len);
    5865};
    5966
Note: See TracChangeset for help on using the changeset viewer.