| 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 |  | 
|---|
| 12 | ### File Specific: | 
|---|
| 13 |    main-programmer: Silvan Nellen | 
|---|
| 14 |    co-programmer: Benjamin Wuest | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 | #define DEBUG_MODULE_NETWORK | 
|---|
| 18 |  | 
|---|
| 19 | #include "shared_network_data.h" | 
|---|
| 20 | #include "network_stream.h" | 
|---|
| 21 | #include "netdefs.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "state.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include <cassert> | 
|---|
| 26 |  | 
|---|
| 27 | #include "synchronizeable.h" | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | /** | 
|---|
| 32 |  *  default constructor | 
|---|
| 33 |  */ | 
|---|
| 34 | Synchronizeable::Synchronizeable() | 
|---|
| 35 | { | 
|---|
| 36 |   this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); | 
|---|
| 37 |   this->owner = -1; | 
|---|
| 38 |   this->state = 0; | 
|---|
| 39 |   this->hostID = SharedNetworkData::getInstance()->getHostID(); | 
|---|
| 40 |   this->setIsServer(this->hostID == 0); | 
|---|
| 41 |   this->uniqueID = NET_UID_UNASSIGNED; | 
|---|
| 42 |   this->networkStream = NULL; | 
|---|
| 43 |   this->setRequestedSync( false ); | 
|---|
| 44 |   this->setIsOutOfSync( !(this->isServer()) ); | 
|---|
| 45 |   this->bSynchronize = false; | 
|---|
| 46 |  | 
|---|
| 47 |   if( State::isOnline()) | 
|---|
| 48 |   { | 
|---|
| 49 |     NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream(); | 
|---|
| 50 |     assert(nd != NULL); | 
|---|
| 51 |     nd->connectSynchronizeable(*this); | 
|---|
| 52 |     this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID()); | 
|---|
| 53 |   } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 |  *  default destructor deletes all unneded stuff | 
|---|
| 60 |  */ | 
|---|
| 61 | Synchronizeable::~Synchronizeable() | 
|---|
| 62 | { | 
|---|
| 63 |   if ( this->networkStream ) | 
|---|
| 64 |     this->networkStream->disconnectSynchronizeable(*this); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | /** | 
|---|
| 69 |  *  write data to NetworkStream | 
|---|
| 70 |  */ | 
|---|
| 71 | int Synchronizeable::writeBytes(const byte* data, int length, int sender) | 
|---|
| 72 | { | 
|---|
| 73 |   PRINTF(5)("Synchronizeable::writeBytes was called\n"); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /** | 
|---|
| 78 |  *  read data from NetworkStream | 
|---|
| 79 |  */ | 
|---|
| 80 | int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever) | 
|---|
| 81 | { | 
|---|
| 82 |   PRINTF(5)("Synchronizeable::readBytes was called\n"); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | void Synchronizeable::writeDebug() const | 
|---|
| 87 | {} | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | void Synchronizeable::readDebug() const | 
|---|
| 91 | {} | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 |  * Sets the server flag to a given value | 
|---|
| 96 |  * @param isServer: the boolean value which the server flag is to set to | 
|---|
| 97 |  */ | 
|---|
| 98 | void Synchronizeable::setIsServer(bool isServer) | 
|---|
| 99 | { | 
|---|
| 100 |   if( isServer ) | 
|---|
| 101 |     this->state = this->state | STATE_SERVER; | 
|---|
| 102 |   else | 
|---|
| 103 |     this->state = this->state & (~STATE_SERVER); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 108 |  * Sets the outofsync flag to a given value | 
|---|
| 109 |  * @param outOfSync: the boolean value which the outofsync flag is to set to | 
|---|
| 110 |  */ | 
|---|
| 111 | void Synchronizeable::setIsOutOfSync(bool outOfSync) | 
|---|
| 112 | { | 
|---|
| 113 |   if( outOfSync ) | 
|---|
| 114 |     this->state = this->state | STATE_OUTOFSYNC; | 
|---|
| 115 |   else | 
|---|
| 116 |     this->state = this->state & (~STATE_OUTOFSYNC); | 
|---|
| 117 |   //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | /** | 
|---|
| 122 |  * Determines if the server flag is set | 
|---|
| 123 |  * @return true, if the server flag is true, false else | 
|---|
| 124 |  */ | 
|---|
| 125 | bool Synchronizeable::isServer() | 
|---|
| 126 | { | 
|---|
| 127 |   return (this->state & STATE_SERVER) >0; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 |  * Determines if the outofsync flag is set | 
|---|
| 133 |  * @return true, if the outofsync flag is true, false else | 
|---|
| 134 |  */ | 
|---|
| 135 | bool Synchronizeable::isOutOfSync() | 
|---|
| 136 | { | 
|---|
| 137 |   return (this->state & STATE_OUTOFSYNC) >0; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 |  * Determines if the requestedSync flag is set | 
|---|
| 143 |  * @return true, if the requestedSync flag is true, false else | 
|---|
| 144 |  */ | 
|---|
| 145 | bool Synchronizeable::requestedSync() | 
|---|
| 146 | { | 
|---|
| 147 |   return (this->state & STATE_REQUESTEDSYNC) >0; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 |  * Sets the requestedsync flag to a given value | 
|---|
| 153 |  * @param requestedSync: the boolean value which the requestedsync flag is to set to | 
|---|
| 154 |  */ | 
|---|
| 155 | void Synchronizeable::setRequestedSync( bool requestedSync ) | 
|---|
| 156 | { | 
|---|
| 157 |   if( requestedSync ) | 
|---|
| 158 |     this->state = this->state | STATE_REQUESTEDSYNC; | 
|---|
| 159 |   else | 
|---|
| 160 |     this->state = this->state & (~STATE_REQUESTEDSYNC); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|