Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/network_stream.cc @ 6082

Last change on this file since 6082 was 5996, checked in by patrick, 19 years ago

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

File size: 5.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->type = NET_CLIENT;
47  this->networkSocket = new NetworkSocket();
48  this->networkProtocol = new NetworkProtocol();
49  this->synchronizeables = NULL;
50  this->connectionMonitor = new ConnectionMonitor();
51}
52
53NetworkStream::NetworkStream(IPaddress& address, NodeType type)
54{
55  this->type = type;
56  this->init();
57  this->networkSocket = new NetworkSocket(address);
58  this->networkProtocol = new NetworkProtocol();
59  this->synchronizeables = NULL;
60  this->connectionMonitor = new ConnectionMonitor();
61}
62
63
64NetworkStream::NetworkStream(unsigned int port, NodeType type)
65{
66  this->type = type;
67  this->init();
68  this->networkSocket = new NetworkSocket();
69//  this->networkSocket->listen(port);
70  this->networkProtocol = new NetworkProtocol();
71  this->synchronizeables = NULL;
72  this->connectionMonitor = new ConnectionMonitor();
73}
74
75
76NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
77    : DataStream()
78{
79  this->type = type;
80  this->init();
81  this->networkSocket = new NetworkSocket(address);
82  this->networkProtocol = new NetworkProtocol();
83  this->synchronizeables = &sync;
84  this->connectionMonitor = new ConnectionMonitor();
85  this->bActive = true;
86}
87
88
89NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type)
90    : DataStream()
91{
92  this->type = type;
93  this->init();
94  this->networkSocket = new NetworkSocket();
95//  this->networkSocket->listen(port);
96  this->networkProtocol = new NetworkProtocol();
97  this->synchronizeables = &sync;
98  this->connectionMonitor = new ConnectionMonitor();
99  this->bActive = true;
100}
101
102
103void NetworkStream::init()
104{
105  /* set the class id for the base object */
106  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
107  this->state = NET_REC_HEADER;
108  this->bActive = false;
109}
110
111
112NetworkStream::~NetworkStream()
113{
114
115  networkSocket->disconnectServer();
116
117  if( this->networkSocket)
118    delete networkSocket;
119
120  delete connectionMonitor;
121  delete networkProtocol;
122}
123
124
125void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
126{
127  this->synchronizeables = &sync;
128  if( this->networkSocket != NULL)
129    this->bActive = true;
130}
131
132
133void NetworkStream::processData()
134{
135  int dataLength = 0;
136
137  /* DOWNSTREAM */
138  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
139  PRINT(0)("============= DOWNSTREAM:===============\n");
140  /* first of all read the synchronizeable's data: */
141  if( this->isServer())
142    dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
143
144  if( dataLength > 0)
145  {
146    /* send the received data to connectionMonitor */
147    this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
148
149    dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
150                 *(this->synchronizeables), 12/* some random number (no real id)*/);
151
152    /* pass the data to the network socket */
153 //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
154    /* check if there was an error */
155    if( dataLength == -1)
156    {
157      PRINTF(0)("Error in writing data to the NetworkSocket\n");
158    }
159  }
160
161
162  /* UPSTREAM */
163  dataLength = 0;
164  PRINT(0)("============== UPSTREAM:================\n");
165  /* first of all read the next Orxonox Network Header */
166
167  if( this->state == NET_REC_HEADER)
168  {
169//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
170    if( dataLength == sizeof(Header))
171    {
172      this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
173      printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
174             this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
175             this->packetHeader.receiverID, this->packetHeader.length);
176      /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
177
178      this->state = NET_REC_DATA;
179    }
180  }
181  if( this->state == NET_REC_DATA)
182  {
183    /* now read the data */
184//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
185    /* check if the data is available and process it if so */
186    if( dataLength == this->packetHeader.length)
187    {
188      printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
189      /* send the received data to connectionMonitor */
190      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
191      /* now pass the data to the sync object */
192      if( !this->isServer())
193        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
194
195      this->state = NET_REC_HEADER;
196    }
197  }
198
199
200}
Note: See TracBrowser for help on using the repository browser.