Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: some more fixes on the network branche: for testing purpuses, the length of the binary data packet will be hard set to 11bytes.

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, 9999 );
58   // this->networkSocket->connectToServer(address, 88);
59  this->networkProtocol = new NetworkProtocol();
60  this->synchronizeables = &sync;
61  this->connectionMonitor = new ConnectionMonitor();
62}
63
64
65NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type)
66  : DataStream()
67{
68  this->init();
69  this->networkSocket = new NetworkSocket(/* address, type */);
70  this->networkSocket->listen(9999);
71  this->networkProtocol = new NetworkProtocol();
72  this->synchronizeables = &sync;
73  this->connectionMonitor = new ConnectionMonitor();
74}
75
76
77void NetworkStream::init()
78{
79  /* set the class id for the base object */
80  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
81}
82
83
84NetworkStream::~NetworkStream()
85{
86
87 networkSocket->disconnectServer();
88
89 delete networkSocket;
90 delete connectionMonitor;
91 delete networkProtocol;
92}
93
94void NetworkStream::processData()
95{
96
97
98  int ret = 0;
99
100  /* DOWNSTREAM */
101
102  PRINT(0)("\n\n");
103  PRINT(0)("============= DOWNSTREAM:===============\n");
104  /* first of all read the synchronizeable's data: */
105  ret = this->synchronizeables->readBytes((byte*)downBuffer);
106
107  /* send the received data to connectionMonitor */
108  this->connectionMonitor->processPacket((byte*)downBuffer, ret);
109
110  ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE,
111                                            *(this->synchronizeables), 12);
112
113  printf("created header: data packet wights: %i bytes\n", ret);
114  /* pass the data to the network socket */
115  ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);
116  /* check if there was an error */
117  printf("NetworkSocket: sent %i bytes\n", ret);
118  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
119
120
121
122  /* UPSTREAM */
123
124  ret = 0;
125  PRINT(0)("============== UPSTREAM:================\n");
126  /* first of all read  data from networkSocket*/
127  ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */);
128  /* error checking: data read? */
129  printf("NetworkSocket: received %i bytes\n", ret);
130  if( ret != PACKAGE_SIZE + sizeof(Header)) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");}
131  else
132  {
133    /* send the received data to connectionMonitor */
134    this->connectionMonitor->processPacket((byte*)upBuffer, ret);
135
136    /* extract Header */
137    this->networkProtocol->extractHeader((byte*) upBuffer , ret);
138
139    /* now pass the data to the sync object */
140    this->synchronizeables->writeBytes((byte*)upBuffer, ret);
141  }
142
143}
Note: See TracBrowser for help on using the repository browser.