src/network/nettest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "config.h"
00029 #include "nNetwork.h"
00030 #include "nNet.h"
00031 #include <string>
00032 #include <iostream>
00033 
00034 #include <sys/types.h>
00035 #if HAVE_UNISTD_H
00036 #include <unistd.h>
00037 #endif
00038 #ifndef WIN32
00039 #include <sys/socket.h>
00040 #include <netdb.h>
00041 #include <arpa/inet.h> 
00042 #else
00043 #include <windows.h>
00044 #endif
00045 
00046 #include "nNetObject.h"
00047 
00048 class floattest;
00049 class deptest;
00050 
00051 List<floattest> floattests;
00052 
00053 class floattest: public nNetObject{
00054     int listID;
00055 
00056 public:
00057     REAL x;
00058     deptest *dep;
00059 
00060     virtual void debug_out(){
00061         con << id << ":" << owner << ":" << x;
00062     }
00063 
00064     floattest():nNetObject(),listID(-1),x(-4){
00065         dep=NULL;
00066         con << "created floattest.\n";
00067         floattests.Add(this,listID);
00068     }
00069     floattest(nMessage &m):nNetObject(m),listID(-1){
00070         dep=NULL;
00071         //con << "created floattest.\n";
00072         floattests.Add(this,listID);
00073     }
00074 
00075     virtual ~floattest(){
00076         if(dep){
00077             tERR_ERROR("floattest deleted before deptest!");
00078         }
00079 
00080         floattests.Remove(this,listID);
00081         con << "deleted floattest.\n";
00082     }
00083 
00084     virtual bool AcceptClientSync() const{
00085         return true;
00086     }
00087 
00088     virtual void WriteSync(nMessage &m){
00089         nNetObject::WriteSync(m);
00090         m << x;
00091     }
00092 
00093     virtual void ReadSync(nMessage &m){
00094         nNetObject::ReadSync(m);
00095         m >> x;
00096     }
00097 
00098     virtual nDescriptor &CreatorDescriptor() const;
00099 };
00100 
00101 nNOInitialisator<floattest> floattest_init(200,"floattest");
00102 
00103 nDescriptor &floattest::CreatorDescriptor() const{
00104     return floattest_init.desc;
00105 }
00106 
00107 List<deptest, false, true> deptests;
00108 
00109 class deptest: public nNetObject{
00110     int listID;
00111 
00112 public:
00113     floattest *dep;
00114 
00115     void debug_out(){
00116         con << "x:";
00117         dep->debug_out();
00118     }
00119 
00120     deptest(floattest *x):listID(-1),dep(x){
00121         con << "created deptest.\n";
00122         dep->dep=this;
00123         deptests.Add(this,listID);
00124     }
00125 
00126     deptest(nMessage &m):nNetObject(m),listID(-1){
00127         con << "creating deptest.\n";
00128         unsigned short id;
00129         m.Read(id);
00130         dep=(floattest *)nNetObject::Object(id);
00131         if (dep->dep){
00132             tERR_ERROR("two deptests for the same same floattest!");
00133         }
00134 
00135         dep->dep=this;
00136         deptests.Add(this,listID);
00137     }
00138 
00139     virtual ~deptest(){
00140         dep->dep=NULL;
00141         con << "deleted deptest.\n";
00142         deptests.Remove(this,listID);
00143     }
00144 
00145     virtual bool ClearToTransmit(int user) const{
00146         return dep->HasBeenTransmitted(user);
00147     }
00148 
00149     virtual void WriteCreate(nMessage &m){
00150         nNetObject::WriteCreate(m);
00151         m.Write(dep->ID());
00152     }
00153 
00154     virtual bool AcceptClientSync() const{
00155         return true;
00156     }
00157 
00158     virtual nDescriptor &CreatorDescriptor() const;
00159 };
00160 
00161 nNOInitialisator<deptest> deptest_init("deptest");
00162 
00163 nDescriptor &deptest::CreatorDescriptor() const{
00164     return deptest_init.desc;
00165 }
00166 
00167 
00168 int ntest(const tString &serv,bool server){
00169     floattest x;
00170     deptest *y=NULL;
00171 
00172 
00173 
00174     int i=100000;
00175     if (server){
00176         sn_SetNetState(nSERVER);
00177         //y=new deptest(&x);
00178     }
00179     else{
00180         sn_Connect(serv);
00181         i=1000;
00182     }
00183 
00184 
00185     while(i>0 && sn_GetNetState()!=nSTANDALONE){
00186         sn_Receive();
00187         nNetObject::SyncAll();
00188         sn_SendPlanned();
00189 
00190         x.x+=.007;
00191         if (i%10==0){
00192             x.RequestSync();
00193             if (sn_GetNetState()==nCLIENT || i%100==0){
00194                 for(int j=floattests.Len()-1;j>=0;j--){
00195                     floattests[j]->debug_out();
00196                     con << '\t';
00197                 }
00198                 con << '\n';
00199             }
00200         }
00201 
00202         i--;
00203         usleep(10000);
00204 
00205         /*
00206         static int lastprint=0;
00207 
00208         if (current_id%10==0 && current_id!=lastprint){
00209           lastprint=current_id;
00210           con << "netid=" << current_id << '\n';
00211         }
00212         */
00213     }
00214 
00215     if (i==0)
00216         con << "Regular logout..\n";
00217 
00218     sn_SetNetState(nSTANDALONE);
00219     usleep(100000);
00220     sn_Receive();
00221     sn_SendPlanned();
00222 
00223     if (y)
00224         delete y;
00225 
00226     return 0;
00227 }
00228 
00229 

Generated on Sat Mar 15 22:55:49 2008 for Armagetron Advanced by  doxygen 1.5.4