1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2016/09/02
|
---|
6 | // filename: Forwarder.cpp
|
---|
7 | //
|
---|
8 | // authors: Thomas Fuhrmann
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Forward input data to output data, using sockets
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "Forwarder.h"
|
---|
19 |
|
---|
20 | #include <Socket.h>
|
---|
21 | #include <Thread.h>
|
---|
22 | #include <FrameworkManager.h>
|
---|
23 |
|
---|
24 | //todo remove for tests
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | using namespace std;
|
---|
28 | using namespace flair::core;
|
---|
29 |
|
---|
30 | Forwarder::Forwarder(const FrameworkManager *parent, string name, string &inputAddress,
|
---|
31 | int inputPort, string &outputAddress, int outputPort):
|
---|
32 | Thread(parent, name, 6), inputAddress(inputAddress),
|
---|
33 | inputPort(inputPort), outputAddress(outputAddress),
|
---|
34 | outputPort(outputPort) {
|
---|
35 | cout << "MavPlanner Forwarder constructor" << endl;
|
---|
36 |
|
---|
37 | // string tmpOutput = outputAddress + ":" + to_string(outputPort);
|
---|
38 | // cout << "MavPlanner Forwarder output address : " << tmpOutput << endl;
|
---|
39 |
|
---|
40 | cout << "input socket : " << inputAddress + ":" + to_string(inputPort) << endl;
|
---|
41 | cout << "output socket : " << outputAddress + ":" + to_string(outputPort) << endl;
|
---|
42 |
|
---|
43 | inputSocket = new Socket((Thread *)this, "input socket", inputAddress + ":" + to_string(inputPort));
|
---|
44 | outputSocket = new Socket((Thread *)this, "output socket", outputAddress + ":" + to_string(outputPort));
|
---|
45 | }
|
---|
46 |
|
---|
47 | Forwarder::~Forwarder() {
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Forwarder::Run() {
|
---|
52 | if (getFrameworkManager()->ErrorOccured()) {
|
---|
53 | SafeStop();
|
---|
54 | Thread::Err("An error occurred, we don't launch the Run loop.\n");
|
---|
55 | }
|
---|
56 | int recvDataSize = 0;
|
---|
57 | char* srcBuffer;
|
---|
58 | size_t srcSize;
|
---|
59 | while (!ToBeStopped()) {
|
---|
60 | // Thread::SleepMS(2000);
|
---|
61 | // TIME_NONBLOCK
|
---|
62 | recvDataSize = inputSocket->RecvMessage(inputBuffer, 200, 5000000000, srcBuffer, &srcSize);
|
---|
63 | cout << "Forwarder message received, size="<< recvDataSize << endl;
|
---|
64 | outputSocket->SendMessage(inputBuffer, recvDataSize);
|
---|
65 | }
|
---|
66 | } |
---|