tmechobridge.cpp
241 lines
| 8.1 KiB
| text/x-c
|
CppLexer
/ rmapplugin / tmechobridge.cpp
r23 | #include "tmechobridge.h" | |||
TMEchoBridge::TMEchoBridge(QWidget *parent) : | ||||
QWidget(parent) | ||||
{ | ||||
totalOfBytes = 0; | ||||
r24 | totalOfPackets = 0; | |||
totalOfPacketsDropped = 0; | ||||
r23 | ||||
tmEchoBridgeServer = new QTcpServer(); | ||||
tmEchoBridgeSocket = new QTcpSocket(); | ||||
xmlWriter = new QXmlStreamWriter(); | ||||
console = new QTextEdit(); | ||||
label_TMServerPort = new QLabel(tr("TM Server Port:")); | ||||
label_totalOfBytes = new QLabel(tr("Number of TM bytes sent: -")); | ||||
r24 | label_totalOfPackets = new QLabel(tr("Number of TM packets sent: -")); | |||
label_totalOfPacketsDropped = new QLabel(tr("Number of TM packets dropped: -")); | ||||
r23 | ||||
spinbox_TMServerPort = new QSpinBox(); | ||||
spinbox_TMServerPort->setRange(59002, 65535); | ||||
button_openServerTCTM = new QPushButton(tr("Open Port")); | ||||
button_testServerTCTM = new QPushButton(tr("Test Port")); | ||||
button_sendTestPacket = new QPushButton(tr("Send a test packet")); | ||||
button_clearConsole = new QPushButton(tr("Clear console")); | ||||
r24 | button_resetTMStatistics = new QPushButton(tr("Reset TM Statistics")); | |||
r23 | ||||
groupbox_ports = new QGroupBox(tr("Port of the TM Echo Bridge")); | ||||
layout_ports = new QGridLayout(); | ||||
mainLayout = new QGridLayout(); | ||||
initSocketStatesList(); | ||||
layout_ports->addWidget(label_TMServerPort, 0, 0, 1, 1); | ||||
layout_ports->addWidget(spinbox_TMServerPort, 0, 1, 1, 1); | ||||
layout_ports->addWidget(button_openServerTCTM, 2, 0, 1, 2); | ||||
layout_ports->addWidget(button_testServerTCTM, 3, 0, 1, 2); | ||||
layout_ports->addWidget(button_sendTestPacket, 4, 0, 1, 2); | ||||
r24 | layout_ports->addWidget(label_totalOfPackets, 5, 0, 1, 2); | |||
layout_ports->addWidget(label_totalOfBytes, 6, 0, 1, 2); | ||||
layout_ports->addWidget(label_totalOfPacketsDropped, 7, 0, 1, 2); | ||||
layout_ports->addWidget(button_resetTMStatistics, 8, 0, 1, 2); | ||||
layout_ports->setRowStretch(9, 1); | ||||
r23 | layout_ports->setColumnStretch(2, 1); | |||
groupbox_ports->setLayout(layout_ports); | ||||
mainLayout->addWidget(groupbox_ports, 0, 0, 1, 1); | ||||
mainLayout->addWidget(console, 0, 1, 1, 1); | ||||
mainLayout->addWidget(button_clearConsole, 1, 1, 1, 1); | ||||
this->setLayout(mainLayout); | ||||
connect(this->tmEchoBridgeServer, SIGNAL(newConnection()), this, SLOT(newConnectionOnTMEchoBridgeServer())); | ||||
connect(this->button_openServerTCTM, SIGNAL(clicked()), this, SLOT(listenOnTMEchoBridgePort())); | ||||
connect(this->button_testServerTCTM, SIGNAL(clicked()), this, SLOT(testTMEchoBridgePort())); | ||||
connect(this->button_sendTestPacket, SIGNAL(clicked()), this, SLOT(sendXML_GSE_HK())); | ||||
connect(this->button_clearConsole, SIGNAL(clicked()), this, SLOT(clearConsole())); | ||||
r24 | connect(this->button_resetTMStatistics, SIGNAL(clicked()), this, SLOT(resetTMStatistics())); | |||
r23 | } | |||
void TMEchoBridge::listenOnTMEchoBridgePort() | ||||
{ | ||||
QString str; | ||||
tmEchoBridgeServer->listen(QHostAddress::Any, this->getTMPort()); | ||||
str = tmEchoBridgeServer->errorString(); | ||||
if (!str.isEmpty()) | ||||
{ | ||||
this->displayOnConsole(str); | ||||
} | ||||
this->displayOnConsole("Listening TM on port " | ||||
+ QString::number(this->getTMPort()) | ||||
); | ||||
} | ||||
void TMEchoBridge::testTMEchoBridgePort() | ||||
{ | ||||
if (tmEchoBridgeSocket != NULL) | ||||
{ | ||||
this->displayOnConsole("TM socket *** " + socketStates.at(tmEchoBridgeSocket->state()) ); | ||||
} | ||||
else | ||||
{ | ||||
this->displayOnConsole("TC socket *** not tested, (socketTC) is NULL" ); | ||||
} | ||||
} | ||||
void TMEchoBridge::newConnectionOnTMEchoBridgeServer() | ||||
{ | ||||
this->displayOnConsole("got new connection on TM port"); | ||||
tmEchoBridgeSocket = tmEchoBridgeServer->nextPendingConnection(); | ||||
this->displayOnConsole("TM socket *** " + socketStates.at(tmEchoBridgeSocket->state()) ); | ||||
xmlWriter->setDevice(this->tmEchoBridgeSocket); | ||||
} | ||||
int TMEchoBridge::getTMPort() | ||||
{ | ||||
return spinbox_TMServerPort->value(); | ||||
} | ||||
void TMEchoBridge::displayOnConsole(QString message) | ||||
{ | ||||
console->append(message); | ||||
} | ||||
void TMEchoBridge::initSocketStatesList() | ||||
{ | ||||
socketStates.append("The socket is not connected"); | ||||
socketStates.append("The socket is performing a host name lookup"); | ||||
socketStates.append("The socket has started establishing a connection"); | ||||
socketStates.append("A connection is established"); | ||||
socketStates.append("The socket is bound to an address and port (for servers)"); | ||||
socketStates.append("The socket is about to close (data may still be waiting to be written)"); | ||||
socketStates.append("For internal use only"); | ||||
} | ||||
void TMEchoBridge::sendXML_GSE_HK() | ||||
{ | ||||
char data[10]; | ||||
for (int i = 0; i <10; i++) | ||||
{ | ||||
data[i] = i; | ||||
} | ||||
if (tmEchoBridgeSocket->state() == QAbstractSocket::ConnectedState) | ||||
{ | ||||
r24 | xmlWriter->writeStartDocument(); | |||
r23 | xmlWriter->writeStartElement("Event"); | |||
// EventDate | ||||
xmlWriter->writeStartElement("EventDate"); | ||||
xmlWriter->writeEndElement(); | ||||
// Event Time | ||||
xmlWriter->writeStartElement("EventTime"); | ||||
xmlWriter->writeEndElement(); | ||||
// EventBinary | ||||
xmlWriter->writeStartElement("EventBinary"); | ||||
xmlWriter->writeCharacters("FFFFCC0099"); | ||||
xmlWriter->writeCharacters(QString::fromLocal8Bit(data, 10)); | ||||
xmlWriter->writeEndElement(); | ||||
xmlWriter->writeEndElement(); | ||||
r24 | ||||
xmlWriter->writeEndDocument(); | ||||
r23 | } | |||
else | ||||
{ | ||||
this->displayOnConsole("TM socket *** " + socketStates.at(tmEchoBridgeSocket->state()) ); | ||||
} | ||||
} | ||||
void TMEchoBridge::sendTMPacket(TMPacketToRead *tmPacketToRead) | ||||
{ | ||||
r24 | QByteArray ccsdsArray; | |||
if (tmEchoBridgeSocket->state() == QAbstractSocket::ConnectedState) | ||||
{ | ||||
ccsdsArray.append("<Event>"); | ||||
ccsdsArray.append("<EventDate>"); | ||||
ccsdsArray.append("</EventDate>"); | ||||
ccsdsArray.append("<EventTime>"); | ||||
ccsdsArray.append("</EventTime>"); | ||||
ccsdsArray.append("<EventBinary>"); | ||||
ccsdsArray.append(QByteArray::fromRawData( (char*) tmPacketToRead->Value, tmPacketToRead->size).toHex()); | ||||
ccsdsArray.append("</EventBinary>"); | ||||
ccsdsArray.append("</Event>"); | ||||
tmEchoBridgeSocket->write(ccsdsArray); | ||||
tmEchoBridgeSocket->flush(); | ||||
totalOfBytes = totalOfBytes + tmPacketToRead->size; | ||||
totalOfPackets = totalOfPackets + 1; | ||||
label_totalOfBytes->setText("Number of TM bytes sent: " + QString::number(totalOfBytes)); | ||||
label_totalOfPackets->setText("Number of TM packets sent: " + QString::number(totalOfPackets)); | ||||
} | ||||
else | ||||
{ | ||||
totalOfPacketsDropped = totalOfPacketsDropped + 1; | ||||
r26 | label_totalOfPacketsDropped->setText("Number of TM packets dropped: " + QString::number(totalOfPacketsDropped)); | |||
r23 | } | |||
} | ||||
r45 | void TMEchoBridge::sendTMPacketLESIA(TMPacketToRead *tmPacketToRead) | |||
{ | ||||
QByteArray ccsdsArray; | ||||
if (tmEchoBridgeSocket->state() == QAbstractSocket::ConnectedState) | ||||
{ | ||||
ccsdsArray.append("<Event>"); | ||||
ccsdsArray.append("<EventDate>"); | ||||
ccsdsArray.append("</EventDate>"); | ||||
ccsdsArray.append("<EventTime>"); | ||||
ccsdsArray.append("</EventTime>"); | ||||
ccsdsArray.append("<EventBinary>"); | ||||
ccsdsArray.append(QByteArray::fromRawData( (char*) &tmPacketToRead->Value[4], tmPacketToRead->size-4).toHex()); | ||||
ccsdsArray.append("</EventBinary>"); | ||||
ccsdsArray.append("</Event>"); | ||||
tmEchoBridgeSocket->write(ccsdsArray); | ||||
tmEchoBridgeSocket->flush(); | ||||
totalOfBytes = totalOfBytes + tmPacketToRead->size; | ||||
totalOfPackets = totalOfPackets + 1; | ||||
label_totalOfBytes->setText("Number of TM bytes sent: " + QString::number(totalOfBytes)); | ||||
label_totalOfPackets->setText("Number of TM packets sent: " + QString::number(totalOfPackets)); | ||||
} | ||||
else | ||||
{ | ||||
totalOfPacketsDropped = totalOfPacketsDropped + 1; | ||||
label_totalOfPacketsDropped->setText("Number of TM packets dropped: " + QString::number(totalOfPacketsDropped)); | ||||
} | ||||
} | ||||
r23 | void TMEchoBridge::clearConsole() | |||
{ | ||||
this->console->clear(); | ||||
} | ||||
r24 | ||||
void TMEchoBridge::resetTMStatistics() | ||||
{ | ||||
label_totalOfBytes->setText("Number of TM bytes sent: -"); | ||||
label_totalOfPackets->setText("Number of TM packets sent: -"); | ||||
label_totalOfPacketsDropped->setText("Number of TM packets dropped: -"); | ||||
totalOfBytes = 0; | ||||
totalOfPackets = 0; | ||||
totalOfPacketsDropped = 0; | ||||
} | ||||