Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: changed the synchronizeable interface, since the data synchronizeables, more debug output

File size: 3.9 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}
81
82
83NetworkStream::~NetworkStream()
84{
85
86 networkSocket->disconnectServer();
87
88 delete networkSocket;
89 delete connectionMonitor;
90 delete networkProtocol;
91}
92
93void NetworkStream::processData()
94{
95  int ret = 0;
96
97  /* DOWNSTREAM */
98  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
99  PRINT(0)("============= DOWNSTREAM:===============\n");
100  /* first of all read the synchronizeable's data: */
101  ret = this->synchronizeables->readBytes((byte*)downBuffer);
102
103  /* send the received data to connectionMonitor */
104  this->connectionMonitor->processPacket((byte*)downBuffer, ret);
105
106  ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE,
107                                            *(this->synchronizeables), 12);
108
109  printf("created header: data packet wights: %i bytes\n", ret);
110  /* pass the data to the network socket */
111  ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);
112  /* check if there was an error */
113  printf("NetworkSocket: sent %i bytes\n", ret);
114  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
115
116
117
118  /* UPSTREAM */
119  ret = 0;
120  PRINT(0)("============== UPSTREAM:================\n");
121  /* first of all read  data from networkSocket*/
122  ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */);
123  /* error checking: data read? */
124  printf("NetworkSocket: received %i bytes\n", ret);
125  if( ret != 11 /*PACKAGE_SIZE + sizeof(Header)*/) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");}
126  else
127  {
128    /* send the received data to connectionMonitor */
129    this->connectionMonitor->processPacket((byte*)upBuffer, ret);
130
131    /* extract Header */
132    Header header = this->networkProtocol->extractHeader((byte*) upBuffer , ret);
133
134    /* now pass the data to the sync object */
135    this->synchronizeables->writeBytes((byte*)upBuffer, header.length);
136  }
137
138}
Note: See TracBrowser for help on using the repository browser.