Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/proxy/proxy_control.cc @ 9585

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

network nodes get added to the netmon

File size: 5.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: Patrick Boenzli (patrick@orxonox.ethz.ch)
13*/
14
15#include "proxy_control.h"
16
17#include "class_list.h"
18
19
20#include "state.h"
21#include "shared_network_data.h"
22#include "network_game_manager.h"
23#include "ip.h"
24#include "peer_info.h"
25
26#include "converter.h"
27
28#include "preferences.h"
29
30#include "debug.h"
31
32#include "monitor/network_monitor.h"
33
34
35ProxyControl* ProxyControl::singletonRef = NULL;
36
37
38/**
39 * constructor
40 */
41ProxyControl::ProxyControl()
42{
43  this->setClassID( CL_PROXY_CONTROL, "ProxyControl" );
44
45  MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_NEWCLIENT, messageHandlerNewClient, NULL );
46  MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_LEAVECLIENT, messageHandlerLeaveClient, NULL );
47
48
49  PRINTF(0)("ProxyControl created\n");
50}
51
52
53/**
54 * standard deconstructor
55 */
56ProxyControl::~ProxyControl()
57{
58  ProxyControl::singletonRef = NULL;
59}
60
61
62 /**
63 * override this function to be notified on change
64 * of your registred variables.
65 * @param id id's which have changed
66  */
67void ProxyControl::varChangeHandler( std::list< int > & id )
68{
69//   if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
70//   {
71//     this->setPlayableUniqueId( this->playableUniqueId );
72//
73//     PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
74//   }
75}
76
77
78/**
79 *  signals new client connected to this local proxy
80 *
81 *  byte 0 - 3     :      userId
82 *  byte 4 - 7     :      ip address (IPaddress.host)
83 *
84 * @param userId userId of the new client
85 */
86void ProxyControl::signalNewClient(int userId)
87{
88  PRINTF(0)("Signaling new Client: %i\n", userId);
89  // make sure we are a proxy server
90  assert(SharedNetworkData::getInstance()->isProxyServerActive());
91
92  byte data[2 * INTSIZE];
93  // write the userId in the message
94  assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE );
95  // and the ip as an int
96  PeerInfo* pInfo = SharedNetworkData::getInstance()->getNetworkMonitor()->getPeerByUserId(userId);
97  assert(pInfo != NULL);
98  assert( Converter::intToByteArray( pInfo->ip.host(), data + INTSIZE, INTSIZE ) == INTSIZE );
99
100  MessageManager::getInstance()->sendMessage( MSGID_PROXY_NEWCLIENT, data, 2*INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
101}
102
103
104/**
105 * this is the handler for proxy signals: new clients
106 *
107 * @param messageType the type of the message
108 * @param data message data
109 * @param dataLength length of the message data
110 * @param someData some other atteched data
111 * @param senderId id of the sender client
112 * @param destinationId id of the destination client
113 * @return true if succeeded
114 */
115bool ProxyControl::messageHandlerNewClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
116{
117  // body data length correct?
118  if ( dataLength != 2 * INTSIZE )
119  {
120    PRINTF(2)("new client message has wrong size: %d\n", dataLength );
121    return true;
122  }
123  // read the userId fromt he message body
124  int newClientId = 0;
125  assert( Converter::byteArrayToInt( data, &newClientId) == INTSIZE );
126  // now read the ip address
127  int ipHost = 0;
128  assert( Converter::byteArrayToInt( data + INTSIZE, &ipHost) == INTSIZE );
129
130  // register the new node at the network monitor
131  NetworkMonitor* netMon = SharedNetworkData::getInstance()->getNetworkMonitor();
132  PeerInfo* pInfo = new PeerInfo();
133  pInfo->bLocal = false;
134  pInfo->ip = IP(ipHost, 9999);
135  pInfo->nodeType = NET_CLIENT;
136  netMon->addNode(pInfo);
137
138  PRINTF(0)("Got Signal: from %i new player arrived with userId: %i and ip: %i\n", senderId, newClientId, pInfo->ip.ipString().c_str());
139  // part for the master server
140  if( SharedNetworkData::getInstance()->isMasterServer())
141  {
142    // we now create the new player ship and stuff...
143    NetworkGameManager::getInstance()->signalNewPlayer(newClientId);
144  }
145  else if(SharedNetworkData::getInstance()->isProxyServerActive())
146  {
147
148  }
149
150  return true;
151}
152
153
154
155/**
156 *  signals client disconnect
157 * @param userId userId of the old client
158 */
159void ProxyControl::signalLeaveClient(int userId)
160{
161  PRINTF(0)("Signaling new Client: %i\n", userId);
162  // make sure we are a proxy server
163  assert(SharedNetworkData::getInstance()->isProxyServerActive());
164
165  byte data[INTSIZE];
166
167  assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE );
168
169  MessageManager::getInstance()->sendMessage( MSGID_PROXY_LEAVECLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
170}
171
172
173/**
174 * this is the handler for proxy signals: removing clients
175 *
176 * @param messageType the type of the message
177 * @param data message data
178 * @param dataLength length of the message data
179 * @param someData some other atteched data
180 * @param senderId id of the sender client
181 * @param destinationId id of the destination client
182 * @return true if succeeded
183 */
184bool ProxyControl::messageHandlerLeaveClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
185{
186  // body data length correct?
187  if ( dataLength != INTSIZE )
188  {
189    PRINTF(2)("leave client message has wrong size: %d\n", dataLength );
190    return true;
191  }
192  // read the userId fromt he message body
193  int leaveClientId = 0;
194  assert( Converter::byteArrayToInt( data, &leaveClientId) == INTSIZE );
195
196  PRINTF(0)("Got Signal: from %i new player left with userId: %i\n", senderId, leaveClientId);
197  // part for the master server
198  if( SharedNetworkData::getInstance()->isMasterServer())
199  {
200    // we now create the new player ship and stuff...
201    NetworkGameManager::getInstance()->signalLeftPlayer(leaveClientId);
202  }
203  else if(SharedNetworkData::getInstance()->isProxyServerActive())
204  {
205
206  }
207
208  return true;
209}
Note: See TracBrowser for help on using the repository browser.