Changeset 9439 for trunk/MagicSoft/Cosy/tcpip
- Timestamp:
- 05/09/09 13:48:12 (16 years ago)
- Location:
- trunk/MagicSoft/Cosy/tcpip
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc
r9435 r9439 104 104 cout << "Zd/Az: " << zd << "/" << az << " "; 105 105 cout << "Ra/Dec: " << ra/15 << "h/" << dec << " "; 106 cout << "SolRad: " << solar << "W/m²" << endl; 106 //cout << "SolRad: " << solar << "W/m²"; 107 cout << endl; 107 108 108 109 fComStat = kCmdReceived; … … 122 123 return InterpreteReport(str); 123 124 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)); 127 126 } 128 127 129 128 bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force) 130 129 { 131 MTime t;132 t.Now();133 130 134 131 UShort_t y1, y2, ms1, ms2; 135 132 Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2; 136 133 134 fT.GetDate(y2, mon2, d2); 135 fT.GetTime(h2, m2, s2, ms2); 136 137 MTime t(-1); 137 138 t.GetDate(y1, mon1, d1); 138 139 t.GetTime(h1, m1, s1, ms1); 139 140 140 fT.GetDate(y2, mon2, d2);141 fT.GetTime(h2, m2, s2, ms2);141 if (t-fT>20) 142 fComStat = kComProblem; 142 143 143 144 const TString msg = … … 152 153 // Send report to CC 153 154 const bool rc = Send(msg.Data(), msg.Length()); 154 fComStat = rc ? kNoCmdReceived : kComProblem;155 155 156 156 // Write report to stream -
trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
r9432 r9439 19 19 enum ComStatus_t 20 20 { 21 kCmdReceived, 22 kNoCmdReceived, 23 kComProblem 21 kCmdReceived = 1, 22 kComProblem = 2 24 23 }; 25 24 … … 52 51 MCeCoCom(const char *addr, const int tx=7304, const int rx=7404, MLog *out=NULL) 53 52 : MTcpIpIO(addr, tx, rx), fOut(out), fStatus(0), 54 fTelescope(1), fComStat(k NoCmdReceived),53 fTelescope(1), fComStat(kComProblem), 55 54 fHumidity(0), fTemperature(0), fWindSpeed(0), fSolarRadiation(-1), 56 55 fAlarmCounter(0) -
trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
r9435 r9439 7 7 #include "MString.h" 8 8 #include "Ring.h" 9 #include "Led.h" 10 11 #include "MLog.h" 12 #include "MLogManip.h" 9 13 10 14 using namespace std; … … 45 49 if (!ReadPosition(str, ra, dec)) 46 50 { 47 cout<< "ERROR - Reading position from RADEC" << endl;51 gLog << err << "ERROR - Reading position from RADEC" << endl; 48 52 return false; 49 53 } 50 54 if (!str.IsNull()) 51 55 { 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; 57 61 58 62 ra *= 15; // h -> deg … … 72 76 if (!ReadPosition(str, ra, dec)) 73 77 { 74 cout<< "ERROR - Reading position from GRB" << endl;78 gLog << err << "ERROR - Reading position from GRB" << endl; 75 79 return false; 76 80 } 77 81 if (!str.IsNull()) 78 82 { 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; 84 88 85 89 ra *= 15; // h -> deg … … 99 103 if (!ReadPosition(str, zd, az)) 100 104 { 101 cout<< "ERROR - Reading position from ZDAZ" << endl;105 gLog << err << "ERROR - Reading position from ZDAZ" << endl; 102 106 return false; 103 107 } … … 105 109 if (!str.IsNull()) 106 110 { 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; 112 116 113 117 ZdAz za(zd, az); … … 125 129 if (str.IsNull()) 126 130 { 127 cout<< "ERROR - No identifier for preposition (PREPS) given." << endl;131 gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl; 128 132 return false; 129 133 } 130 134 if (str.First(' ')>=0) 131 135 { 132 cout<< "ERROR - PREPS command syntax error (contains whitespaces)." << endl;136 gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl; 133 137 return false; 134 138 } … … 136 140 str.ToLower(); 137 141 138 cout<< "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;142 gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl; 139 143 140 144 //cout << "MDriveCom - TRACK... start." << endl; … … 147 151 bool MDriveCom::CommandTPOINT(TString &str) 148 152 { 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); 153 168 154 169 return true; … … 157 172 bool MDriveCom::CommandSTGMD(TString &str) 158 173 { 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); 163 180 164 181 return true; … … 170 187 if (str.IsNull()) 171 188 { 172 cout<< "ERROR - No identifier for ARM command." << endl;189 gLog << err << "ERROR - No identifier for ARM command." << endl; 173 190 return false; 174 191 } 175 192 if (str.First(' ')>=0) 176 193 { 177 cout<< "ERROR - ARM command syntax error (contains whitespaces)." << endl;194 gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl; 178 195 return false; 179 196 } … … 182 199 if (str!="lock" && str!="unlock") 183 200 { 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; 189 206 190 207 bool lock = str=="lock"; … … 200 217 { 201 218 //cout << "MDriveCom - WAIT... start." << endl; 202 cout<< "CC-COMMAND " << MTime(-1) << " WAIT" << endl;219 gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl; 203 220 if (fQueue) 204 221 fQueue->PostMsg(WM_WAIT); … … 210 227 { 211 228 //cout << "MDriveCom - STOP!... start." << endl; 212 cout<< "CC-COMMAND " << MTime(-1) << " STOP!" << endl;229 gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl; 213 230 if (fQueue) 214 231 fQueue->PostMsg(WM_STOP); … … 240 257 if (cmd.IsNull() && str.IsNull()) 241 258 { 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; 247 264 return false; 248 265 } … … 258 275 } 259 276 260 bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed )277 bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd) 261 278 { 262 279 // rd [rad] … … 294 311 str += MString::Format("%08.3f ", er.Az()); 295 312 str += armed ? "1 " : "0 "; 313 str += MString::Format("%d ", stargmd); // Starguider mode: 0=none, 1=starguider, 2=starguider off 296 314 297 315 return SendRep("DRIVE-REPORT", str.Data(), kFALSE); … … 336 354 } 337 355 338 bool MDriveCom::SendTPoint(UInt_t stat) 339 { 340 //const MTime t(-1); 341 356 bool 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 ¢er, const Led &star, Int_t numleds, Int_t numrings, 359 Int_t numstars, Int_t numcor, Float_t bright) 360 { 342 361 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 7 7 8 8 class RaDec; 9 class AltAz; 9 10 class ZdAz; 10 11 class MsgQueue; 11 12 class Ring; 13 class Led; 12 14 class MLog; 15 class MTime; 16 class TVector2; 13 17 14 18 class MDriveCom : public MCeCoCom … … 49 53 50 54 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); 52 56 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 ¢er, const Led &star, Int_t numleds, Int_t numrings, 61 Int_t numstars=0, Int_t numcor=0, Float_t bright=0); 54 62 bool SendStatus(const char *stat); 55 63 }; -
trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
r9432 r9439 43 43 MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx) 44 44 { 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 46 48 fTxSocket = new TSocket(addr, tx); 49 fTxSocket->SetOption(kNoBlock, 1); 50 51 MTcpIpO::RunThread(); 47 52 } 48 53 49 54 MTcpIpO::~MTcpIpO() 50 55 { 56 CancelThread(); 57 51 58 // Now delete all TCP/IP objects 52 59 delete fTxSocket; … … 55 62 MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx) 56 63 { 57 RunThread();64 MTcpIpI::RunThread(); 58 65 } 59 66 … … 74 81 } 75 82 83 /* 76 84 TString MTcpIpO::GetSocketAddress() const 77 85 { 86 TLockGuard guard(const_cast<TMutex*>(&fMutex)); 78 87 return GetSocketAddress(*fTxSocket); 79 88 } 80 89 */ 81 90 bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len) 82 91 { 83 92 if (!tx.IsValid()) 84 {85 //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;86 93 return false; 87 }88 94 89 95 #ifdef DEBUG … … 91 97 #endif 92 98 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 94 109 if (l<0) 95 110 { … … 100 115 if (l!=len) 101 116 { 102 gLog << err << "ERROR - Sent wrong number (" << l << ") ofbytes to " << GetSocketAddress(tx) << endl;117 gLog << err << "ERROR - Could only sent " << l << " out of " << len << " bytes to " << GetSocketAddress(tx) << endl; 103 118 return false; 104 119 } … … 109 124 bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len) 110 125 { 111 // R__LOCKGUARD2(myMutex);112 126 #ifdef DEBUG 113 127 cout << "Connecting to " << addr << ":" << port << endl; … … 117 131 TSocket tx(addr, port); 118 132 return SendFrame(tx, msg, len); 119 /* 120 if (!tx.IsValid()) 121 { 122 gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl; 133 } 134 135 bool 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) 123 145 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 154 Int_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 156 175 #ifdef DEBUG 157 176 cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl; 158 177 #endif 178 179 fMutex.Lock(); 180 159 181 delete fTxSocket; 182 160 183 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; 164 197 } 165 198 … … 174 207 TString str; 175 208 176 while (! IsThreadCanceled())209 while (!MTcpIpI::IsThreadCanceled()) 177 210 { 178 211 char c; -
trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
r9432 r9439 2 2 #define COSY_MTcpIpIO 3 3 4 #ifndef MARS_MThread5 #include " MThread.h"4 #ifndef COSY_MsgQueue 5 #include "msgqueue.h" 6 6 #endif 7 7 #ifndef MARS_MTime 8 8 #include "MTime.h" 9 #endif 10 #ifndef ROOT_TMutex 11 #include <TMutex.h> 9 12 #endif 10 13 … … 22 25 23 26 Bool_t fConnectionEstablished; 27 28 MTime fLast; 24 29 25 30 Bool_t WaitForData(TSocket &sock); … … 39 44 40 45 // A generalized class for sending over tcp/ip 41 class MTcpIpO 46 class MTcpIpO : public MThread 42 47 { 43 48 private: 44 49 TSocket *fTxSocket; 50 TMutex fMutex; 45 51 46 52 Int_t fPortTx; 53 54 Int_t Thread(); 47 55 48 56 public: … … 54 62 static bool SendFrame(const char *addr, int port, const char *msg, int len); 55 63 56 TString GetSocketAddress() const; 57 bool Send(const char *msg, int len); 64 bool Send(const char *msg, int len); 58 65 }; 59 66
Note:
See TracChangeset
for help on using the changeset viewer.