Changeset 1346 in ntrip for trunk/BNC/bncsocket.cpp


Ignore:
Timestamp:
Dec 27, 2008, 12:40:39 PM (16 years ago)
Author:
mervart
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/bncsocket.cpp

    r1345 r1346  
    2222using namespace std;
    2323
     24#define BNCVERSION "1.7"
     25
    2426// Constructor
    2527////////////////////////////////////////////////////////////////////////////
     
    99101  return _socket->waitForBytesWritten(msecs);
    100102}
     103
     104// Connect to Caster, send the Request (static)
     105////////////////////////////////////////////////////////////////////////////
     106bncSocket* bncSocket::request(const QUrl& mountPoint,
     107                              QByteArray& latitude, QByteArray& longitude,
     108                              QByteArray& nmea, int timeOut,
     109                              QString& msg) {
     110
     111  // Connect the Socket
     112  // ------------------
     113  QSettings settings;
     114  QString proxyHost = settings.value("proxyHost").toString();
     115  int     proxyPort = settings.value("proxyPort").toInt();
     116 
     117  bncSocket* socket = new bncSocket(new QTcpSocket());
     118  if ( proxyHost.isEmpty() ) {
     119    socket->connectToHost(mountPoint.host(), mountPoint.port());
     120  }
     121  else {
     122    socket->connectToHost(proxyHost, proxyPort);
     123  }
     124  if (!socket->waitForConnected(timeOut)) {
     125    msg += "Connect timeout\n";
     126    delete socket;
     127    return 0;
     128  }
     129
     130  // Send Request
     131  // ------------
     132  QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
     133  QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
     134  QByteArray userAndPwd;
     135
     136  if(!uName.isEmpty() || !passW.isEmpty())
     137  {
     138    userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
     139    passW.toAscii()).toBase64() + "\r\n";
     140  }
     141
     142  QUrl hlp;
     143  hlp.setScheme("http");
     144  hlp.setHost(mountPoint.host());
     145  hlp.setPort(mountPoint.port());
     146  hlp.setPath(mountPoint.path());
     147
     148  QByteArray reqStr;
     149  if ( proxyHost.isEmpty() ) {
     150    if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
     151    reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
     152             + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
     153             + userAndPwd + "\r\n";
     154  } else {
     155    reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
     156             + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
     157             + "Host: " + hlp.host().toAscii() + "\r\n"
     158             + userAndPwd + "\r\n";
     159  }
     160
     161  // NMEA string to handle VRS stream
     162  // --------------------------------
     163  double lat, lon;
     164
     165  lat = strtod(latitude,NULL);
     166  lon = strtod(longitude,NULL);
     167
     168  if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
     169    const char* flagN="N";
     170    const char* flagE="E";
     171    if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
     172    if ((lon < 0.) && (lon >= -180.))  {lon=lon*(-1.); flagE="W";}
     173    if (lon < -180.)  {lon=(lon+360.); flagE="E";}
     174    if (lat < 0.)  {lat=lat*(-1.); flagN="S";}
     175    QTime ttime(QDateTime::currentDateTime().toUTC().time());
     176    int lat_deg = (int)lat; 
     177    double lat_min=(lat-lat_deg)*60.;
     178    int lon_deg = (int)lon; 
     179    double lon_min=(lon-lon_deg)*60.;
     180    int hh = 0 , mm = 0;
     181    double ss = 0.0;
     182    hh=ttime.hour();
     183    mm=ttime.minute();
     184    ss=(double)ttime.second()+0.001*ttime.msec();
     185    QString gga;
     186    gga += "GPGGA,";
     187    gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
     188    gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
     189    gga += flagN;
     190    gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
     191    gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
     192    int xori;
     193    char XOR = 0;
     194    char *Buff =gga.toAscii().data();
     195    int iLen = strlen(Buff);
     196    for (xori = 0; xori < iLen; xori++) {
     197      XOR ^= (char)Buff[xori];
     198    }
     199    gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
     200    reqStr += "$";
     201    reqStr += gga;
     202    reqStr += "\r\n";
     203  }
     204
     205  msg += reqStr;
     206
     207  socket->write(reqStr, reqStr.length());
     208
     209  if (!socket->waitForBytesWritten(timeOut)) {
     210    msg += "Write timeout\n";
     211    delete socket;
     212    return 0;
     213  }
     214
     215  return socket;
     216}
     217
Note: See TracChangeset for help on using the changeset viewer.