[9293] | 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 |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #include "synchronizeable_ip.h" |
---|
| 17 | #include "converter.h" |
---|
[9334] | 18 | // #include "ip.h" |
---|
[9293] | 19 | |
---|
| 20 | /** |
---|
| 21 | * standard constructor |
---|
| 22 | * @todo this constructor is not jet implemented - do it |
---|
| 23 | */ |
---|
[9334] | 24 | SynchronizeableIP::SynchronizeableIP( IP * ptrIn, IP * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 2*INTSIZE, permission, priority ) |
---|
[9293] | 25 | { |
---|
| 26 | this->vPtrIn = ptrIn; |
---|
| 27 | this->vPtrOut = ptrOut; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * standard deconstructor |
---|
| 33 | */ |
---|
| 34 | SynchronizeableIP::~SynchronizeableIP () |
---|
| 35 | { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * write var data to byte buffer |
---|
| 40 | * @param buf pointer to write to |
---|
| 41 | * @param maxLength writeToBuf will not write more than maxLength bytes |
---|
| 42 | * @return number bytes written |
---|
| 43 | */ |
---|
| 44 | int SynchronizeableIP::writeToBuf( byte * buf, int maxLength ) |
---|
| 45 | { |
---|
| 46 | int n = 0; |
---|
| 47 | int res; |
---|
| 48 | |
---|
[9344] | 49 | res = Converter::intToByteArray( (int)vPtrIn->host(), buf, maxLength ); |
---|
[9294] | 50 | assert(res > 0); |
---|
| 51 | n += res; |
---|
[9293] | 52 | |
---|
[9334] | 53 | res = Converter::intToByteArray( (int)vPtrIn->port(), buf, maxLength); |
---|
[9294] | 54 | assert(res > 0); |
---|
| 55 | n += res; |
---|
| 56 | |
---|
| 57 | assert( 2 * INTSIZE == n); |
---|
| 58 | |
---|
[9293] | 59 | return n; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * read var data from byte buffer |
---|
| 64 | * @param buf pointer to read from |
---|
| 65 | * @param maxLength readFromBuf will not read more than maxLength bytes |
---|
| 66 | * @return number bytes read |
---|
| 67 | */ |
---|
| 68 | int SynchronizeableIP::readFromBuf( byte * buf, int maxLength ) |
---|
| 69 | { |
---|
[9294] | 70 | assert( maxLength >= 2 * INTSIZE); |
---|
[9293] | 71 | |
---|
[9294] | 72 | int host, port; |
---|
[9293] | 73 | |
---|
| 74 | int res; |
---|
| 75 | int n = 0; |
---|
| 76 | |
---|
[9344] | 77 | IP oldVal = *vPtrOut; |
---|
[9294] | 78 | |
---|
| 79 | res = Converter::byteArrayToInt( buf + n, &host); |
---|
| 80 | assert( res > 0); |
---|
| 81 | n += res; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | res = Converter::byteArrayToInt( buf + n, &port); |
---|
| 85 | assert( res > 0); |
---|
| 86 | n += res; |
---|
| 87 | |
---|
[9334] | 88 | *this->vPtrOut = IP(host, port); |
---|
[9294] | 89 | |
---|
[9349] | 90 | printf(" %s old: %i, %i -- new: %i, %i\n", name.c_str(), oldVal.host(), oldVal.port(), this->vPtrOut->host(), this->vPtrOut->port()); |
---|
[9344] | 91 | setHasChanged( *this->vPtrOut != oldVal); |
---|
[9294] | 92 | |
---|
| 93 | assert( n == 2 * INTSIZE); |
---|
| 94 | |
---|
[9293] | 95 | return n; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * print out variable value |
---|
| 101 | */ |
---|
| 102 | void SynchronizeableIP::SynchronizeableIP::debug( ) |
---|
| 103 | { |
---|
[9344] | 104 | printf("SYNCHRONIZEABLE_VAR: %s IN: %i, %i OUT: %i, %i\n", name.c_str(), vPtrIn->host(), vPtrIn->port(), vPtrOut->host(), vPtrOut->port()); |
---|
[9293] | 105 | } |
---|
| 106 | |
---|