Advanced Strategic Command
pbem-server.cpp
Go to the documentation of this file.
1 
8 /*
9  This file is part of Advanced Strategic Command; http://www.asc-hq.de
10  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; see the file COPYING. If not, write to the
24  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  Boston, MA 02111-1307 USA
26 */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <cstring>
31 
32 #include <curl/curl.h>
33 #include <curl/types.h>
34 #include <curl/easy.h>
35 
36 
37 #include "../global.h"
38 #include "../misc.h"
39 #include "../sgstream.h"
40 #include "../loaders.h"
41 #include "../gamemap.h"
42 #include "../dialog.h"
43 #include "pbem-server.h"
44 
46 
47 #include "../paradialog.h"
48 
49 #include <pgpropertyeditor.h>
50 #include <pgpropertyfield_integer.h>
51 #include <pgpropertyfield_intdropdown.h>
52 #include <pgpropertyfield_dropdown.h>
53 #include <pgpropertyfield_checkbox.h>
54 #include <pgpropertyfield_string.h>
55 
56 
57 // @PBEMSERVER
58 // the interface between the ASC engine and the server communication interface
59 
60 
62  private:
63  ASCString name;
64  ASCString password;
65 
66  ASC_PropertyEditor* propertyEditor;
67 
68  bool ok()
69  {
70  if ( propertyEditor->Apply() )
71  QuitModal();
72  return true;
73  }
74 
75  public:
76  AuthenticationDialog() : ASC_PG_Dialog(NULL, PG_Rect( -1, -1, 300, 150 ), "Logging in to server...")
77  {
78  propertyEditor = new ASC_PropertyEditor( this, PG_Rect( 10, GetTitlebarHeight() + 20, Width() - 20, Height() - GetTitlebarHeight() - 50 ), "PropertyEditor", 50 );
79 
80  new PG_PropertyField_String<ASCString>( propertyEditor , "Username", &name );
81  (new PG_PropertyField_String<ASCString>( propertyEditor , "Default Password", &password ))->SetPassHidden('*');
82 
83  PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
84  ok->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &AuthenticationDialog::ok )));
85  }
86 
87  const ASCString& getName() { return name; };
88  const ASCString& getPassword() { return password; };
89 };
90 
91 
92 
93 
95  : filename("myFileName"), gameID(-1)
96 {
97 
98 }
99 
100 
101 void PBEMServer::readChildData ( tnstream& stream )
102 {
103  stream.readInt();
104  serverAddress = stream.readString();
105  gameID = stream.readInt();
106 }
107 
108 void PBEMServer::writeChildData ( tnstream& stream ) const
109 {
110  stream.writeInt( 1 );
111  stream.writeString(serverAddress );
112  stream.writeInt ( gameID );
113 }
114 
115 void PBEMServer::setup()
116 {
117  // enterfilename();
118 }
119 
120 
121 
123 {
124  static ASC_PBEM* session = NULL;
125  if ( !session ) {
126  session = new ASC_PBEM( serverAddress );
127 
128  bool success;
129  do {
131  ad.Show();
132  ad.RunModal();
133  success = session->login( ad.getName(), ad.getPassword() );
134  if( !success )
135  warningMessage("login failed!");
136  //TODO dialog: retry? Cancel?
137  } while ( !success );
138  }
139  return session;
140 }
141 
142 
143 void PBEMServer::send( const GameMap* map, int lastPlayer, int lastturn )
144 {
145 
146 
147  if ( choice_dlg("Do you want to upload the game to the ASC server?", "~y~es","~n~o") == 1) {
148 
149 
150  if ( gameID == -1 )
151  gameID = editInt( "turn", gameID );
152 
153 
154  MemoryStreamStorage buffer;
155 
156  {
157  MemoryStream stream ( &buffer, tnstream::writing );
158 
159  tnetworkloaders nwl;
160  nwl.savenwgame( &stream, map );
161  }
162 
163  // now we have the data to be transmitted in the buffer
164  // buffer.
165 
166  ASC_PBEM* session = getSession();
167  bool result = session->uploadFile( filename, buffer.getBuffer(), buffer.getSize(), gameID );
168  if ( !result )
169  warningMessage( "upload failed");
170 
171  } else {
172  // writing to file
173 
174  }
175 }
176 
177 
179 {
180  /*
181  GameMap* map = NULL;
182  try {
183  tnfilestream gamefile ( filename, tnstream::reading );
184  tnetworkloaders nwl;
185  map = nwl.loadnwgame( &gamefile );
186  }
187  catch ( tinvalidversion iv ) {
188  throw iv;
189  }
190  catch ( tfileerror ) {
191  errorMessage ( filename + " is not a legal email game" );
192  return NULL;
193  }
194  return map;
195  */
196  return NULL;
197 }
198 
200 {
201 
202  // TODO getting from GameOptions...
203  // return "http://84.59.97.144:8080/ascServer";
204  return "http://localhost:8080/ascServer";
205 }
206 
207 
208 void PBEMServer::setServerAddress( const ASCString& address )
209 {
210  serverAddress = address;
211 }
212 
213 
214 namespace {
215  const bool r1 = networkTransferMechanismFactory::Instance().registerClass( PBEMServer::mechanismID(), ObjectCreator<GameTransferMechanism, PBEMServer> );
216 }
217 
bool login(ASCString user, ASCString passwd)
An in-memory storage of streamed data.
Definition: basestrm.h:172
static ASCString getDefaultServerAddress()
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
void readChildData(tnstream &stream)
int getSize() const
Definition: basestrm.h:195
const Uint8 * getBuffer() const
Definition: basestrm.h:194
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
const ASCString & getName()
Definition: pbem-server.cpp:87
void send(const GameMap *map, int lastPlayer, int lastturn)
void warningMessage(const ASCString &str)
The interface for all kinds of IO stream.
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
bool uploadFile(ASCString fileName, const Uint8 *data, const int size, const int gameID)
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
Reads data from or writes data to a MemoryStreamStorage This allows a completely volatile storage of ...
Definition: basestrm.h:204
void writeChildData(tnstream &stream) const
ASC_PBEM * getSession()
int savenwgame(tnstream *strm, const GameMap *gamemap)
Definition: loaders.cpp:1235
virtual ASCString readString(bool includeCR=false)
Reads and returns a string.
Definition: basestrm.cpp:535
int editInt(const ASCString &title, int defaultValue, int minValue, int maxValue)
Definition: dialog.cpp:2163
const ASCString & getPassword()
Definition: pbem-server.cpp:88
GameMap * receive()
static ASCString mechanismID()
Definition: pbem-server.h:49
int choice_dlg(const ASCString &title, const ASCString &leftButton, const ASCString &rightButton)
Definition: dialog.cpp:616
void setServerAddress(const ASCString &address)
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182