Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

pbem-server.cpp

Go to the documentation of this file.
00001 
00008 /*
00009     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00010     Copyright (C) 1994-2005  Martin Bickel  and  Marc Schellenberger
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; see the file COPYING. If not, write to the 
00024     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00025     Boston, MA  02111-1307  USA
00026 */
00027 
00028 #include <stdio.h>                    
00029 #include <stdlib.h>
00030 #include <cstring>
00031 
00032 #include <curl/curl.h>
00033 #include <curl/types.h>
00034 #include <curl/easy.h>
00035 
00036 
00037 #include "../global.h"
00038 #include "../misc.h"
00039 #include "../sgstream.h"
00040 #include "../loaders.h"
00041 #include "../gamemap.h"
00042 #include "../dialog.h"
00043 #include "pbem-server.h"
00044 
00045 #include "pbem-server-interaction.h"
00046 
00047 #include "../paradialog.h"
00048 
00049 #include <pgpropertyeditor.h>
00050 #include <pgpropertyfield_integer.h>
00051 #include <pgpropertyfield_intdropdown.h>
00052 #include <pgpropertyfield_dropdown.h>
00053 #include <pgpropertyfield_checkbox.h>
00054 #include <pgpropertyfield_string.h>
00055 
00056 
00057 // @PBEMSERVER 
00058 // the interface between the ASC engine and the server communication interface
00059 
00060 
00061 class AuthenticationDialog : public ASC_PG_Dialog {
00062    private:
00063       ASCString name;
00064       ASCString password;
00065       
00066       ASC_PropertyEditor* propertyEditor;
00067    
00068       bool ok()
00069       {
00070          if ( propertyEditor->Apply() )
00071             QuitModal();
00072          return true;  
00073       }
00074       
00075    public:
00076       AuthenticationDialog() : ASC_PG_Dialog(NULL, PG_Rect( -1, -1, 300, 150 ), "Logging in to server...")
00077       {
00078          propertyEditor = new ASC_PropertyEditor( this, PG_Rect( 10, GetTitlebarHeight() + 20, Width() - 20, Height() - GetTitlebarHeight() - 50 ), "PropertyEditor", 50 );
00079       
00080          new PG_PropertyField_String<ASCString>( propertyEditor , "Username", &name );
00081          (new PG_PropertyField_String<ASCString>( propertyEditor , "Default Password", &password ))->SetPassHidden('*');
00082          
00083          PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
00084          ok->sigClick.connect( SigC::slot( *this, &AuthenticationDialog::ok ));
00085       }
00086       
00087       const ASCString& getName() { return name; };
00088       const ASCString& getPassword() { return password;  };
00089 };
00090 
00091 
00092 
00093 
00094 PBEMServer :: PBEMServer() 
00095     : filename("myFileName"), gameID(-1)
00096 {
00097    
00098 }
00099 
00100 
00101 void PBEMServer::readChildData ( tnstream& stream )
00102 {
00103    stream.readInt();
00104    serverAddress = stream.readString();
00105    gameID = stream.readInt();
00106 }
00107 
00108 void PBEMServer::writeChildData ( tnstream& stream ) const
00109 {
00110    stream.writeInt( 1 );
00111    stream.writeString(serverAddress );
00112    stream.writeInt ( gameID );
00113 }
00114 
00115 void PBEMServer::setup()
00116 {
00117    // enterfilename();
00118 }
00119 
00120 
00121 
00122 ASC_PBEM* PBEMServer::getSession()
00123 {
00124    static ASC_PBEM* session = NULL;
00125    if  ( !session ) {
00126       session = new ASC_PBEM( serverAddress );
00127       
00128       bool success;
00129       do {
00130          AuthenticationDialog ad;
00131          ad.Show();
00132          ad.RunModal();
00133          success = session->login( ad.getName(), ad.getPassword() );
00134          if( !success )
00135             warning("login failed!");
00136          //TODO dialog: retry? Cancel?
00137       } while ( !success );
00138    }
00139    return session;
00140 }
00141 
00142 
00143 void PBEMServer::send( const GameMap* map, int lastPlayer, int lastturn )
00144 {
00145 
00146    
00147    if ( choice_dlg("Do you want to upload the game to the ASC server?", "~y~es","~n~o") == 1) {
00148         
00149       
00150       if ( gameID == -1 )
00151          gameID = editInt( "turn", gameID );
00152 
00153       
00154       tmemorystreambuf buffer;
00155       
00156       {
00157          tmemorystream stream ( &buffer, tnstream::writing );
00158          
00159          tnetworkloaders nwl;
00160          nwl.savenwgame( &stream, map );
00161       }
00162       
00163       // now we have the data to be transmitted in the buffer
00164       // buffer.
00165       
00166       ASC_PBEM* session = getSession();
00167       bool result = session->uploadFile( filename, buffer.getBuffer(), buffer.getSize(), gameID );
00168       if ( !result )
00169          warning( "upload failed");
00170       
00171    } else {
00172       // writing to file  
00173       
00174    }
00175 }
00176 
00177 
00178 GameMap* PBEMServer::receive()
00179 {
00180    /*
00181    GameMap* map = NULL;
00182    try {      
00183       tnfilestream gamefile ( filename, tnstream::reading );
00184       tnetworkloaders nwl;
00185       map = nwl.loadnwgame( &gamefile );
00186    } 
00187    catch ( tinvalidversion iv ) {
00188       throw iv;
00189    }
00190    catch ( tfileerror ) {
00191       errorMessage ( filename + " is not a legal email game" );
00192       return NULL;
00193    }
00194    return map;
00195    */
00196    return NULL;
00197 }
00198 
00199 ASCString PBEMServer::getDefaultServerAddress()
00200 {
00201    
00202    // TODO getting from GameOptions...
00203    // return "http://84.59.97.144:8080/ascServer";
00204      return "http://localhost:8080/ascServer";
00205 }
00206 
00207 
00208 void PBEMServer::setServerAddress( const ASCString& address )
00209 {
00210    serverAddress = address;
00211 }
00212 
00213 
00214 namespace {
00215    const bool r1 = networkTransferMechanismFactory::Instance().registerClass( PBEMServer::mechanismID(), ObjectCreator<GameTransferMechanism, PBEMServer> );
00216 }
00217 

Generated on Tue Jun 24 01:27:47 2008 for Advanced Strategic Command by  doxygen 1.4.2