RavenNetTinyServer.cpp 2.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 *		RAVEN Network Interface
 *
 *		(c) Copyright Institut fr Technische Akustik (ITA)
 *			RWTH Aachen (http://www.akustik.rwth-aachen.de)
 *
 *  ---------------------------------------------------------------------------------
 *
 *    Datei:			RavenNetTinyServer.cpp
 *
 *    Zweck:			Test Raven over Net using simple Vista network functionality
 *
 *    Autor(en):		Jonas Stienen (stienen@akustik.rwth-aachen.de)
 *
 *  ---------------------------------------------------------------------------------
 */
  
// $Id: VistaNetTest.cpp 2732 2012-06-26 13:38:17Z stienen $

#include <iostream>
#include <string>

#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>

#include <VistaInterProcComm/IPNet/VistaIPAddress.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>

//#include <VistaBase/VistaTimeUtils.h>

#include <ITASimulationScheduler/Raven/R_RavenConfig.h>

static int g_iServerPort = 12480;

int main(int argc, char** argv) {

	std::cout << "Setting up server listening on all interfaces to TCP port '" << g_iServerPort << "'" << std::endl;
	VistaTCPServer oServer( "0.0.0.0", g_iServerPort, 1 );

	std::cout << "Waiting for connection" << std::endl;
	VistaConnectionIP* pHeadConnection = new VistaConnectionIP( oServer.GetNextClient() );

	VistaSocketAddress oAddress;
	VistaIPAddress rAddr;
	std::string sAddrLocal;
	pHeadConnection->GetLocalAddress().GetIPAddress().GetAddressString(sAddrLocal);
	std::string sAddrPeer = pHeadConnection->GetPeerName();
	int iPortLocal = pHeadConnection->GetLocalAddress().GetPortNumber();
	int iPortPeer = pHeadConnection->GetPeerPort();
	std::cout <<	"Connection established from " << sAddrPeer << ":" << iPortPeer << 
					" to " << sAddrLocal << ":" << iPortLocal << std::endl;


	std::cout << "Waiting for data" << std::endl;
	long l = pHeadConnection->WaitForIncomingData( 0 );
	

	int iMessageType;
	pHeadConnection->ReadInt32( iMessageType );
	std::cout << "Message type is '" << iMessageType << "'" << std::endl;

	if( iMessageType == 101 )
	{
		long l = pHeadConnection->WaitForIncomingData( 0 );
		std::cout << "Received message with length '" << l << "', trying to read config data" << std::endl;
		//CRavenConfig oConfig;
		void* pBuffer = new char[1024];
		pHeadConnection->ReadRawBuffer( pBuffer, l );
		int* pIntBuffer = (int*) pBuffer;
		int l_i = (int) l / (double) sizeof( int );
		for( int i=0; i<l_i; i++ ) std::cout << pIntBuffer[i] << std::endl;
		//std::cout << oConfig.ToString() << std::endl;
		delete[] pBuffer;
	}

	/*
	std::cout << "Server receiving data" << std::endl;
	char* buf = new char[l];
	pHeadConnection->Receive( buf, l );
	std::string sMsg = std::string( buf, l );
	delete[] buf;
	std::cout << "Message content: '" << sMsg << "'" << std::endl;
	*/
	
	std::cout << "Sending acknowledge flag" << std::endl;
	bool bAck = false;
	pHeadConnection->Send( &bAck, 1 );

	pHeadConnection->Close( false );

	return 0;
}