A working blockingUDPTransport with test.
TODO: debug stopping listener thread.
This commit is contained in:
95
testApp/remote/testBlockingUDPSrv.cpp
Normal file
95
testApp/remote/testBlockingUDPSrv.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* blockingUDPTest.cpp
|
||||
*
|
||||
* Created on: Dec 28, 2010
|
||||
* Author: Miha Vitorovic
|
||||
*/
|
||||
|
||||
#include "remote.h"
|
||||
#include "blockingUDP.h"
|
||||
#include "logger.h"
|
||||
#include "inetAddressUtil.h"
|
||||
#include "hexDump.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace epics::pvAccess;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::hex;
|
||||
using std::dec;
|
||||
|
||||
class DummyResponseHandler : public ResponseHandler {
|
||||
public:
|
||||
DummyResponseHandler() :
|
||||
packets(0) {
|
||||
}
|
||||
|
||||
int getPackets() {
|
||||
return packets;
|
||||
}
|
||||
|
||||
virtual void handleResponse(osiSockAddr* responseFrom,
|
||||
Transport* transport, int8 version, int8 command, int payloadSize,
|
||||
ByteBuffer* payloadBuffer);
|
||||
private:
|
||||
int packets;
|
||||
};
|
||||
|
||||
void DummyResponseHandler::handleResponse(osiSockAddr* responseFrom,
|
||||
Transport* transport, int8 version, int8 command, int payloadSize,
|
||||
ByteBuffer* payloadBuffer) {
|
||||
|
||||
packets++;
|
||||
|
||||
std::ostringstream os;
|
||||
|
||||
cout<<"Received new UDP datagram["<<packets<<"]..."<<endl;
|
||||
cout<<"From: "<<inetAddressToString(responseFrom)<<endl;
|
||||
cout<<"Version: 0x"<<hex<<(int)version<<endl;
|
||||
cout<<"Command: 0x"<<hex<<(int)command<<endl;
|
||||
cout<<"Payload size: "<<dec<<payloadSize<<endl;
|
||||
|
||||
char payload[50];
|
||||
|
||||
for(int i = 0; i<payloadSize;) {
|
||||
int dataCount = payloadSize-i<50 ? payloadSize-i : 50;
|
||||
payloadBuffer->get(payload, 0, dataCount);
|
||||
os<<"Payload ("<<i<<"-"<<(dataCount-1)<<")";
|
||||
hexDump(os.str(), (int8*)payload, dataCount);
|
||||
i += dataCount;
|
||||
}
|
||||
|
||||
cout<<endl<<endl;
|
||||
}
|
||||
|
||||
void testBlockingUDPConnector() {
|
||||
BlockingUDPConnector connector(false, NULL, true);
|
||||
|
||||
DummyResponseHandler drh;
|
||||
|
||||
osiSockAddr bindAddr;
|
||||
|
||||
bindAddr.ia.sin_family = AF_INET;
|
||||
bindAddr.ia.sin_port = htons(65000);
|
||||
bindAddr.ia.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
Transport* transport = connector.connect(NULL, &drh, &bindAddr, 1, 50);
|
||||
|
||||
((BlockingUDPTransport*)transport)->start();
|
||||
|
||||
cout<<"Waiting for 10 packets..."<<endl;
|
||||
|
||||
while(drh.getPackets()<10) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
createFileLogger("testBlockingUDPSrv.log");
|
||||
|
||||
testBlockingUDPConnector();
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user