Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/network_stream.cc @ 5809

Last change on this file since 5809 was 5809, checked in by patrick, 18 years ago

The NetworkSocket now uses a real network protocol: packet size is read out dynamicaly: therefore the NetworkStream now has an internal state that
checks if its reading header or body data

File size: 4.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: claudio
13   co-programmer:
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22
23#include "base_object.h"
24#include "network_protocol.h"
25#include "network_socket.h"
26#include "connection_monitor.h"
27#include "synchronizeable.h"
28#include "list.h"
29#include "debug.h"
30
31/* include your own header */
32#include "network_stream.h"
33
34/* probably unnecessary */
35using namespace std;
36
37
38#define PACKAGE_SIZE  256
39
40
41NetworkStream::NetworkStream()
42  : DataStream()
43{
44  this->init();
45  /* initialize the references */
46  this->networkSocket = new NetworkSocket();
47  this->networkProtocol = new NetworkProtocol();
48  this->synchronizeables = NULL;
49  this->connectionMonitor = new ConnectionMonitor();
50}
51
52
53NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
54  : DataStream()
55{
56  this->init();
57  this->networkSocket = new NetworkSocket(address);
58  this->networkProtocol = new NetworkProtocol();
59  this->synchronizeables = &sync;
60  this->connectionMonitor = new ConnectionMonitor();
61}
62
63
64NetworkStream::NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type)
65  : DataStream()
66{
67  this->init();
68  this->networkSocket = new NetworkSocket();
69  this->networkSocket->listen(port);
70  this->networkProtocol = new NetworkProtocol();
71  this->synchronizeables = &sync;
72  this->connectionMonitor = new ConnectionMonitor();
73}
74
75
76void NetworkStream::init()
77{
78  /* set the class id for the base object */
79  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
80  this->state = NET_REC_HEADER;
81}
82
83
84NetworkStream::~NetworkStream()
85{
86
87 networkSocket->disconnectServer();
88
89 if( this->networkSocket)
90   delete networkSocket;
91
92 delete connectionMonitor;
93 delete networkProtocol;
94}
95
96void NetworkStream::processData()
97{
98  int dataLength = 0;
99
100  /* DOWNSTREAM */
101  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
102  PRINT(0)("============= DOWNSTREAM:===============\n");
103  /* first of all read the synchronizeable's data: */
104  dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
105
106  /* send the received data to connectionMonitor */
107  this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
108
109  dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
110      *(this->synchronizeables), 12/* some random number (no real id)*/);
111
112  /* pass the data to the network socket */
113  dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
114  /* check if there was an error */
115  if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
116
117
118
119  /* UPSTREAM */
120  dataLength = 0;
121  PRINT(0)("============== UPSTREAM:================\n");
122  /* first of all read the next Orxonox Network Header */
123
124  switch( this->state)
125  {
126
127    case NET_REC_HEADER:
128      dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
129      if( dataLength == sizeof(Header))
130      {
131        this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
132        printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
133               this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
134               this->packetHeader.receiverID, this->packetHeader.length);
135        /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
136
137        this->state = NET_REC_DATA;
138      }
139      break;
140
141    case NET_REC_DATA:
142      /* now read the data */
143      dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
144      /* check if the data is available and process it if so */
145      if( dataLength == this->packetHeader.length)
146      {
147        printf("NetworkStream::processData() - Got Data: \n");
148        /* send the received data to connectionMonitor */
149        this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
150        /* now pass the data to the sync object */
151        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
152
153        this->state = NET_REC_HEADER;
154      }
155      break;
156
157    default:
158      break;
159  }
160
161
162}
Note: See TracBrowser for help on using the repository browser.