Changeset 1098 for code/branches/network2/src/network/Synchronisable.cc
- Timestamp:
- Apr 17, 2008, 2:35:51 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network2/src/network/Synchronisable.cc
r1062 r1098 57 57 datasize=0; 58 58 objectID=idCounter++; 59 syncList = new std::list<synchronisableVariable *>; 59 60 //registerAllVariables(); 60 61 } … … 71 72 void Synchronisable::registerVar(const void *var, int size, variableType t){ 72 73 // create temporary synch.Var struct 73 synchronisableVariable temp={size, var, t}; 74 synchronisableVariable *temp = new synchronisableVariable; 75 temp->size = size; 76 temp->var = var; 77 temp->type = t; 78 COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; 74 79 // increase datasize 75 80 datasize+=sizeof(int)+size; 76 // push temp to syncList (at the bottom) 77 syncList.push_back(temp); 81 //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl; 82 COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl; 83 syncList->push_back(temp); 78 84 } 79 85 … … 91 97 // int totalsize=0; 92 98 // //figure out size of data to be allocated 93 // for(i=syncList .begin(); i!=syncList.end(); i++){99 // for(i=syncList->begin(); i!=syncList->end(); i++){ 94 100 // // increase size (size of variable and size of size of variable ;) 95 101 // if(i->type == STRING) … … 107 113 // //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP 108 114 // int n=0; 109 // for(i=syncList .begin(); n<totalsize && i!=syncList.end(); i++){115 // for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){ 110 116 // std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int)); 111 117 // n+=sizeof(int); … … 134 140 */ 135 141 syncData Synchronisable::getData(unsigned char *mem){ 136 std::list<synchronisableVariable>::iterator i; 142 //std::cout << "inside getData" << std::endl; 143 std::list<synchronisableVariable *>::iterator i; 137 144 syncData retVal; 138 145 retVal.objectID=this->objectID; … … 141 148 retVal.data=mem; 142 149 // copy to location 143 int n=0; 144 for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){ 145 //COUT(2) << "size of variable: " << i->size << std::endl; 150 int n=0; //offset 151 for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ 146 152 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); 147 memcpy( (void *)(retVal.data+n), (const void *)&(i->size), sizeof(int) );153 memcpy( (void *)(retVal.data+n), (const void *)&((*i)->size), sizeof(int) ); 148 154 n+=sizeof(int); 149 switch( i->type){155 switch((*i)->type){ 150 156 case DATA: 151 std::memcpy( (void *)(retVal.data+n), (const void*)( i->var), i->size);152 n+= i->size;157 std::memcpy( (void *)(retVal.data+n), (const void*)((*i)->var), (*i)->size); 158 n+=(*i)->size; 153 159 break; 154 160 case STRING: 155 std::memcpy( retVal.data+n, (const void*)( ( (std::string *) i->var)->c_str()), ( (std::string *)i->var )->length()+1);156 n+=( (std::string *) i->var)->length()+1;161 std::memcpy( retVal.data+n, (const void*)( ( (std::string *) (*i)->var)->c_str()), (*i)->size); 162 n+=(*i)->size; 157 163 break; 158 164 } … … 168 174 bool Synchronisable::updateData(syncData vars){ 169 175 unsigned char *data=vars.data; 170 std::list<synchronisableVariable>::iterator i; 171 for(i=syncList.begin(); i!=syncList.end(); i++){ 172 if((int)*data==i->size || i->type==STRING){ 173 switch(i->type){ 174 case DATA: 175 data+=sizeof(int); 176 memcpy((void*)i->var, data, i->size); 177 data+=i->size; 178 break; 179 case STRING: 180 i->size = (int)*data; 181 data+=sizeof(int); 182 *((std::string *)i->var) = std::string((const char*)data); 183 data += i->size; 184 break; 176 std::list<synchronisableVariable *>::iterator i; 177 if(syncList->empty()){ 178 COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl; 179 return false; 180 } 181 COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " synchronising data" << std::endl; 182 for(i=syncList->begin(); i!=syncList->end(); i++){ 183 COUT(5) << "element size: " << (*i)->size << " type: " << (*i)->type << std::endl; 184 if(*(int *)data==(*i)->size || (*i)->type==STRING){ 185 switch((*i)->type){ 186 case DATA: 187 data+=sizeof(int); 188 memcpy((void*)(*i)->var, data, (*i)->size); 189 data+=(*i)->size; 190 break; 191 case STRING: 192 (*i)->size = *(int *)data; 193 data+=sizeof(int); 194 *((std::string *)((*i)->var)) = std::string((const char*)data); 195 data += (*i)->size; 196 break; 185 197 } 186 198 } else … … 196 208 int Synchronisable::getSize(){ 197 209 int tsize=0; 198 std::list<synchronisableVariable>::iterator i; 199 for(i=syncList.begin(); i!=syncList.end(); i++){ 200 switch(i->type){ 201 case DATA: 202 tsize+=sizeof(int); 203 tsize+=i->size; 204 break; 205 case STRING: 206 tsize+=sizeof(int); 207 tsize+=((std::string *)i->var)->length()+1; 208 break; 210 std::list<synchronisableVariable *>::iterator i; 211 for(i=syncList->begin(); i!=syncList->end(); i++){ 212 switch((*i)->type){ 213 case DATA: 214 tsize+=sizeof(int); 215 tsize+=(*i)->size; 216 break; 217 case STRING: 218 tsize+=sizeof(int); 219 (*i)->size=((std::string *)(*i)->var)->length()+1; 220 tsize+=(*i)->size; 221 break; 209 222 } 210 223 }
Note: See TracChangeset
for help on using the changeset viewer.