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

pbem-server-interaction.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "pbem-server-interaction.h"
00004 
00005 
00006 
00020 size_t ASC_PBEM_writeInternal( void* inboundData, size_t size, size_t nmemb, void *vector )
00021 {
00022    if( size != sizeof( char ) ) return 0;
00023    char* buffer = new char[ nmemb+1 ];
00024    memset( buffer, '\0', nmemb+1 );
00025    for( int i=0; i<nmemb; i++ )
00026    {
00027       buffer[ i ] = ((char*) inboundData )[ i ];
00028    }
00029    ( (std::vector<ASCString> *) vector )->push_back( buffer );
00030    delete buffer;
00031    return nmemb;
00032 }
00033 
00034 
00035 int ASC_PBEM_writeBuffer( char* buffer, const char* source, int startPos, int bufferSize, int sourceSize )
00036 {
00037    int i=0;
00038    for( ; i<bufferSize && i<(sourceSize-startPos); i++ )
00039    {
00040       buffer[ i ] = source[ startPos + i ];
00041       //std::cout << buffer[ i ];
00042    }
00043    
00044    return i;
00045 }
00046 
00047 size_t ASC_PBEM_readInternal( char* outboundBuffer, size_t size, size_t nitems, void *infoStruct )
00048 {
00049    ASC_PBEM_FileUploadControl * control = (ASC_PBEM_FileUploadControl*) infoStruct;
00050    
00051    switch( control->step )
00052    {
00053       case 0: // header of first part
00054       {
00055          ASCString header = ASCString("--") + control->boundary + "\n" +
00056             "Content-Disposition: form-data; name=\"file\"; filename=\"" +
00057             control->fileName + "\"\n" +
00058             "Content-Type: application/octet-stream\n \n";
00059          // end of header has to be a \n<space>\n for the server to reliably identify it as empty line
00060          int sent = ASC_PBEM_writeBuffer( outboundBuffer, header.c_str(), control->sent_step, size*nitems, header.size() );
00061          if( sent+control->sent_step == header.size() )
00062          {
00063             control->sent_step = 0;
00064             control->step++;
00065          }else
00066          {
00067             control->sent_step += sent;
00068          }
00069          
00070          return sent;
00071       }
00072       
00073       case 1: // first part
00074       {
00075          int sent = ASC_PBEM_writeBuffer( outboundBuffer, control->data, control->sent_step, size*nitems, control->size );
00076          if( sent+control->sent_step == control->size )
00077          {
00078             control->sent_step = 0;
00079             control->step++;
00080          }else
00081          {
00082             control->sent_step += sent;
00083          }
00084          
00085          return sent;
00086       }
00087       
00088       case 2: // footer: "--" + boundary + "--"
00089       {
00090          // keine ahnung warum da 2x\n nötig sind... wird aber sonst nicht korrekt geparsed
00091          ASCString header = ASCString("\n\n--") + control->boundary;
00092          if( control->parameters.size() == 0 ) 
00093             header += "--\n";
00094          else
00095             header += "\n";
00096             
00097          int sent = ASC_PBEM_writeBuffer( outboundBuffer, header.c_str(), control->sent_step, size*nitems, header.size() );
00098          if( sent+control->sent_step == header.size() )
00099          {
00100             control->sent_step = 0;
00101             control->step++;
00102          }else
00103          {
00104             control->sent_step += sent;
00105          }
00106          
00107          return sent;
00108       }
00109       default: // footer: "--" + boundary + "--"
00110       {
00111          // parameters
00112          int parameterIndex = (control->step - 3) * 2;
00113          if( control->parameters.size() > parameterIndex )
00114          {
00115             ASCString name = control->parameters[ parameterIndex ];
00116             ASCString value = control->parameters[ parameterIndex + 1 ];
00117 
00118             ASCString data = ASCString("Content-Disposition: form-data; name=\"") + name + "\"\n\n" + value +
00119                "\n\n--" + control->boundary;
00120                
00121             if( control->parameters.size() > parameterIndex + 2 )
00122             {
00123                data += "\n";
00124             }else
00125             {
00126                data += "--\n";
00127             }
00128 
00129             int sent = ASC_PBEM_writeBuffer( outboundBuffer, data.c_str(), control->sent_step, size*nitems, data.size() );
00130             if( sent+control->sent_step == data.size() )
00131             {
00132                control->sent_step = 0;
00133                control->step++;
00134             }else
00135             {
00136                control->sent_step += sent;
00137             }
00138             
00139             return sent;
00140          }else
00141          {
00142             return 0;
00143          }
00144       }
00145    }
00146    return 0;
00147 }
00148 
00149 
00150 ASC_PBEM::ASC_PBEM( ASCString serverBase )
00151 {
00152    this->serverBase = serverBase;
00153    if( serverBase[ serverBase.size() - 1 ] != '/' )
00154    {
00155       this->serverBase = serverBase + "/";
00156    }
00157    curl_handle = curl_easy_init();
00158    
00159    usable = true;
00160    statusCode = -1;
00161    loggedIn = false;
00162    serverVersion = 0;
00163 
00164    request( "" );
00165    
00166    usable = false;
00167    
00168    if( 
00169       statusCode == 200 && 
00170       serverVersion >= MIN_SERVER_VERSION && 
00171       serverVersion < MAX_SERVER_VERSION
00172    )
00173    {
00174       usable = true;
00175    }
00176    
00177 }
00178 
00179 
00180 ASC_PBEM::~ASC_PBEM()
00181 {
00182    curl_easy_cleanup( curl_handle );
00183 }
00184 
00185 bool ASC_PBEM::isUsable() 
00186 {
00187    return usable;
00188 }
00189 
00190 bool ASC_PBEM::isLoggedIn() 
00191 {
00192    return loggedIn;
00193 }
00194 
00195 bool ASC_PBEM::request( ASCString url )
00196 {
00197    std::vector<ASCString> parameters;
00198    return request( url, parameters );
00199 }
00200 
00201 bool ASC_PBEM::request( ASCString url, std::vector<ASCString> parameters )
00202 {
00203    if( ! usable ) return false;
00204    
00205    statusCode = -1;
00206    header.clear();
00207    body.clear();
00208    curl_easy_reset( curl_handle );
00209    ASCString urlInternal = serverBase + url;
00210    curl_easy_setopt( curl_handle, CURLOPT_URL, urlInternal.c_str() );
00211    curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, &ASC_PBEM_writeInternal );
00212   curl_easy_setopt( curl_handle, CURLOPT_WRITEHEADER, &header );
00213   curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, &body );
00214 
00215   // post parameters
00216    // struct curl_httppost* post = NULL;
00217    // struct curl_httppost* last = NULL;
00218 
00219    ASCString encodedRequest; // lifetime considerations require this parameter to be _outside_ the loop
00220    if( parameters.size() > 0 )
00221    {
00222       for( int i=0; i<parameters.size(); i++ ) 
00223       {
00224          ASCString name = parameters[ i ];
00225          i++;
00226          ASCString value = parameters[ i ];
00227          if( encodedRequest.length() > 0 ) encodedRequest += "&"; 
00228          encodedRequest += name + "=" + value;
00229       }
00230       curl_easy_setopt( curl_handle, CURLOPT_POSTFIELDS, encodedRequest.c_str() );
00231    }
00232    
00233   // additional headers; currently only sessionid
00234    struct curl_slist *headers=NULL;
00235    ASCString cookieString = "cookie: JSESSIONID=" + sessionID + ";"; // lifetime considerations require this parameter to be _outside_ the loop
00236    ASCString acceptString = "Accept: text/plain"; // lifetime considerations require this parameter to be _outside_ the loop
00237    if( sessionID.length() > 0 )
00238    {
00239       headers = curl_slist_append( headers, cookieString.c_str() );  
00240    }
00241    headers = curl_slist_append( headers, acceptString.c_str() );  
00242    curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, headers );
00243    
00244    // actual perform the request
00245    usable = curl_easy_perform( curl_handle ) == 0;
00246    
00247    // parse header for status code and session id
00248    if( usable )
00249    {
00250       ASCString headerString;
00251       for( int i=0; i<header.size(); i++ ) headerString += header[ i ];
00252       parseHeader( headerString );
00253    }
00254    
00255    // clean up
00256    curl_slist_free_all( headers );
00257 
00258    return usable;
00259 }
00260 
00261 
00262 bool ASC_PBEM::request( ASCString url, ASC_PBEM_FileUploadControl *fileUploadControl )
00263 {
00264    if( ! usable ) return false;
00265    
00266    statusCode = -1;
00267    header.clear();
00268    body.clear();
00269    curl_easy_reset( curl_handle );
00270    ASCString urlInternal = serverBase + url;
00271    curl_easy_setopt( curl_handle, CURLOPT_URL, urlInternal.c_str() );
00272    curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, &ASC_PBEM_writeInternal );
00273   curl_easy_setopt( curl_handle, CURLOPT_WRITEHEADER, &header );
00274   curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, &body );
00275 
00276    curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, &ASC_PBEM_readInternal);
00277    curl_easy_setopt(curl_handle, CURLOPT_INFILE, fileUploadControl);
00278    curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, true );
00279 
00280   // additional headers; sessionid + content type
00281    struct curl_slist *headers=NULL;
00282    ASCString cookieString = "cookie: JSESSIONID=" + sessionID + ";"; // lifetime considerations require this parameter to be _outside_ the loop
00283    ASCString contentString = "content-type: multipart/form-data; boundary=" + fileUploadControl->boundary;
00284    ASCString acceptString = "Accept: text/plain"; // lifetime considerations require this parameter to be _outside_ the loop
00285    if( sessionID.length() > 0 )
00286    {
00287       headers = curl_slist_append( headers, cookieString.c_str() );  
00288    }
00289    headers = curl_slist_append( headers, contentString.c_str() );  
00290    headers = curl_slist_append( headers, acceptString.c_str() );  
00291    curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, headers );
00292    
00293    // actual perform the request
00294    usable = curl_easy_perform( curl_handle ) == 0;
00295    
00296    // parse header for status code and session id
00297    if( usable )
00298    {
00299       ASCString headerString;
00300       for( int i=0; i<header.size(); i++ ) headerString += header[ i ];
00301       parseHeader( headerString );
00302    }
00303    
00304    // clean up
00305    curl_slist_free_all( headers );
00306 
00307    return usable;
00308 }
00309 
00310 void ASC_PBEM::parseHeader( ASCString headerString )
00311 {
00312    int currentIndex = 0;
00313    while( currentIndex < headerString.size() )
00314    {
00315       int lineEnd = headerString.find( '\n', currentIndex );
00316       ASCString line;
00317       if( lineEnd != ASCString::npos )
00318       {
00319          line = headerString.substr( currentIndex, lineEnd-currentIndex );
00320          parseHeaderLine( line );
00321       }else
00322       {
00323          line = headerString.substr( currentIndex );
00324          parseHeaderLine( line );
00325          break;
00326       }
00327       currentIndex = lineEnd + 1;
00328    }
00329 }
00330 
00331 void ASC_PBEM::parseHeaderLine( ASCString line )
00332 {
00333    if( line.find( "HTTP" ) == 0 )
00334    {
00335       // http status line... grab code
00336       int space = line.find( " " );
00337       if( space == ASCString::npos ) return;
00338       space++;
00339       int end = line.find( " ", space );
00340       if( end == ASCString::npos ) return;
00341       
00342       ASCString codeStr = line.substr( space, end-space );
00343       statusCode = atoi( codeStr.c_str() );
00344    }else if( line.find( "Set-Cookie:" ) == 0 )
00345    {
00346       if( line.find( "JSESSIONID" ) != ASCString::npos )
00347       {
00348          ASCString sessionID = line.substr( line.find( "JSESSIONID" ) );
00349          if( sessionID.find( "=" ) == ASCString::npos ) return;
00350          sessionID = sessionID.substr( sessionID.find( "=" ) + 1 );
00351          if( sessionID.find( ";" ) == ASCString::npos ) return;
00352          sessionID = sessionID.substr( 0, sessionID.find( ";" ) );
00353          this->sessionID = sessionID;
00354       }
00355    }else if( line.find( "X-Servlet:" ) == 0 )
00356    {
00357       if( line.find( "ASC_PBEM_Server/" ) != ASCString::npos )
00358       {
00359          serverVersion = atoi( line.substr( line.find( "ASC_PBEM_Server/" ) + 16 ).c_str() );
00360       }else
00361       {
00362          serverVersion = -1;
00363       }
00364    }
00365 }
00366 
00367 bool ASC_PBEM::logout()
00368 {
00369    // don't check for isLoggedIn() on logout... if for any bug
00370    // this flag isn't set, we should still be able to invalidate 
00371    // this session
00372    
00373    // whatever happens, clean up the flag
00374    loggedIn = false;
00375    
00376    if( ! request( "tools/logout" ) ) return false;
00377    
00378    return statusCode == 200;
00379 }
00380 
00381 bool ASC_PBEM::login( ASCString user, ASCString passwd )
00382 {
00383    if( loggedIn ) return false;
00384    
00385    std::vector< ASCString > parameters;
00386    parameters.push_back( "name" );
00387    parameters.push_back( user );
00388    parameters.push_back( "passwd" );
00389    parameters.push_back( passwd );
00390    
00391    if( ! request( "tools/login", parameters ) ) return false;
00392    
00393    loggedIn = statusCode == 202;
00394    
00395    return loggedIn;
00396 }
00397 
00398 bool ASC_PBEM::createAccount( ASCString user, ASCString passwd, ASCString email )
00399 {
00400    if( loggedIn ) return false;
00401    
00402    std::vector< ASCString > parameters;
00403    parameters.push_back( "name" );
00404    parameters.push_back( user );
00405    parameters.push_back( "passwd" );
00406    parameters.push_back( passwd );
00407    parameters.push_back( "passwd2" );
00408    parameters.push_back( passwd );
00409    parameters.push_back( "email" );
00410    parameters.push_back( email );
00411    
00412    if( ! request( "tools/create", parameters ) ) return false;
00413    
00414    return statusCode == 201;
00415 }
00416 
00417 bool ASC_PBEM::activateAccount( ASCString user, ASCString code )
00418 {
00419    if( loggedIn ) return false;
00420    
00421    std::vector< ASCString > parameters;
00422    parameters.push_back( "name" );
00423    parameters.push_back( user );
00424    parameters.push_back( "code" );
00425    parameters.push_back( code );
00426    
00427    if( ! request( "tools/activate", parameters ) ) return false;
00428    
00429    return statusCode == 202;
00430 }
00431 
00432 bool ASC_PBEM::uploadFile( ASCString fileName, const char* data, const int size, const int gameID )
00433 {
00434    if( ! loggedIn ) return false;
00435    
00436    
00437    ASC_PBEM_FileUploadControl control( data, size );
00438    control.step = 0;
00439    control.sent_step = 0;
00440    control.fileName = fileName;
00441    control.boundary = "--------------------ASCPBEM65834626956";
00442    // ??? todo: a better boundary id would be nice... perhapse a random id
00443    
00444    char gameIDString[ 30 ];
00445    sprintf( gameIDString, "%i", gameID );
00446    
00447    control.parameters.push_back( "gameID" );
00448    control.parameters.push_back( gameIDString );
00449    
00450    if( ! request( "tools/upload", &control ) ) return false;
00451    
00452    return statusCode == 201;
00453 }
00454 
00455 bool ASC_PBEM::createGame( ASCString fileName, const char* data, const int size, ASCString gameName, ASCString fileNamePattern, char* roles, int* players, int projectID, int turn, int currentSlot )
00456 {
00457    if( ! loggedIn ) return false;
00458    
00459    
00460    ASC_PBEM_FileUploadControl control( data, size );
00461    control.step = 0;
00462    control.sent_step = 0;
00463    control.fileName = fileName;
00464    control.boundary = "--------------------ASCPBEM65834626956";
00465    // ??? todo: a better boundary id would be nice... perhapse a random id
00466    
00467    char projectIDString[ 30 ];
00468    sprintf( projectIDString, "%i", projectID );
00469 
00470    char turnString[ 30 ];
00471    sprintf( turnString, "%i", turn );
00472 
00473    char currentSlotString[ 30 ];
00474    sprintf( currentSlotString, "%i", currentSlot );
00475    
00476    control.parameters.push_back( "projectID" );
00477    control.parameters.push_back( projectIDString );
00478    control.parameters.push_back( "turn" );
00479    control.parameters.push_back( turnString );
00480    control.parameters.push_back( "currentSlot" );
00481    control.parameters.push_back( currentSlotString );
00482    control.parameters.push_back( "gameName" );
00483    control.parameters.push_back( gameName );
00484    control.parameters.push_back( "pattern" );
00485    control.parameters.push_back( fileNamePattern );
00486    
00487    for( int i=0; i<8; i++ )
00488    {
00489       char playerString[ 10 ];
00490       char playerIDString[ 10 ];
00491       sprintf( playerString, "player_%i", i );
00492       sprintf( playerIDString, "%i", players[ i ] );
00493       
00494       char roleString[ 10 ];
00495       char roleIDString[ 10 ];
00496       sprintf( roleString, "role_%i", i );
00497       sprintf( roleIDString, "%c", roles[ i ] );
00498 
00499       control.parameters.push_back( playerString );
00500       control.parameters.push_back( playerIDString );
00501       control.parameters.push_back( roleString );
00502       control.parameters.push_back( roleIDString );
00503    }
00504    
00505    if( ! request( "tools/newgame", &control ) ) return false;
00506    
00507    return statusCode == 201;
00508 }
00509 
00510 
00511 std::vector<TGameInfo> ASC_PBEM::getCurrentGamesInfo( bool myTurnOnly )
00512 {
00513    std::vector<TGameInfo> games;
00514    
00515    if( !loggedIn ) return games;
00516    
00517    std::vector< ASCString > parameters;
00518    parameters.push_back( "myTurnOnly" );
00519    if( myTurnOnly )
00520       parameters.push_back( "1" );
00521    else
00522       parameters.push_back( "0" );
00523    
00524    if( request( "tools/currentGameList", parameters ) )
00525    {
00526       if( statusCode == 200 )
00527       {
00528          int currentIndex = 0;
00529          ASCString bodyString = getBody();
00530          while( currentIndex < bodyString.size() )
00531          {
00532             int lineEnd = bodyString.find( '\n', currentIndex );
00533             ASCString line;
00534             if( lineEnd != ASCString::npos )
00535             {
00536                line = bodyString.substr( currentIndex, lineEnd-currentIndex );
00537                games.push_back( parseGameInfoLine( line ) );
00538             }else
00539             {
00540                line = bodyString.substr( currentIndex );
00541                games.push_back( parseGameInfoLine( line ) );
00542                break;
00543             }
00544             currentIndex = lineEnd + 1;
00545          }
00546          
00547       }
00548    }
00549    
00550    return games;
00551    
00552 }
00553 
00554 
00555 std::vector<TUserData> ASC_PBEM::getUserList( bool activeOnly )
00556 {
00557    std::vector<TUserData> users;
00558    
00559    if( !loggedIn ) return users;
00560    
00561    std::vector< ASCString > parameters;
00562    parameters.push_back( "activeOnly" );
00563    if( activeOnly )
00564       parameters.push_back( "1" );
00565    else
00566       parameters.push_back( "0" );
00567    
00568    if( request( "tools/userList", parameters ) )
00569    {
00570       if( statusCode == 200 )
00571       {
00572          int currentIndex = 0;
00573          ASCString bodyString = getBody();
00574          while( currentIndex < bodyString.size() )
00575          {
00576             int lineEnd = bodyString.find( '\n', currentIndex );
00577             ASCString line;
00578             if( lineEnd != ASCString::npos )
00579             {
00580                line = bodyString.substr( currentIndex, lineEnd-currentIndex );
00581                users.push_back( parseUserInfoLine( line ) );
00582             }else
00583             {
00584                line = bodyString.substr( currentIndex );
00585                users.push_back( parseUserInfoLine( line ) );
00586                break;
00587             }
00588             currentIndex = lineEnd + 1;
00589          }
00590          
00591       }
00592    }
00593    
00594    return users;
00595 }
00596 
00597 std::vector<ASCString> ASC_PBEM::listPlayers()
00598 {
00599    std::vector<ASCString> newList;
00600    std::vector<TUserData> list = getUserList();
00601 
00602    for ( std::vector<TUserData>::iterator i = list.begin(); i != list.end(); ++i )
00603       newList.push_back( i->userName );
00604 
00605    return newList;
00606 }
00607 
00608 
00609 
00610 TFileData* ASC_PBEM::downloadGame( TGameInfo game )
00611 {
00612    if( ! loggedIn ) return NULL;
00613    /*
00614    std::vector< ASCString > parameters;
00615    
00616    parameters.push_back( "name" );
00617    parameters.push_back( user );
00618    parameters.push_back( "code" );
00619    parameters.push_back( code );
00620    */
00621    char gameIDString[ 30 ];
00622    sprintf( gameIDString, "%i",  game.gameID );
00623    char* fileNameEscaped = curl_escape( game.currentSaveGameName.c_str(), 0 );
00624    
00625    ASCString requestString = "tools/download/";
00626    requestString += fileNameEscaped;
00627    requestString += "?gameID=";
00628    requestString += gameIDString; 
00629    
00630    if( ! request( requestString ) )
00631    {
00632       curl_free( fileNameEscaped );
00633       return NULL;
00634    }
00635    curl_free( fileNameEscaped );
00636    
00637    if( statusCode == 200 )
00638    {
00639       ASCString bodyString = getBody();
00640       
00641       TFileData *data = new TFileData;
00642       data->fileName = game.currentSaveGameName;
00643       data->fileSize = bodyString.size();
00644       data->fileData = new char[ bodyString.size() ];
00645       ASC_PBEM_writeBuffer( data->fileData, bodyString.c_str(), 0, bodyString.size(), bodyString.size() );
00646       
00647       return data;
00648    }
00649    
00650    return NULL;
00651 }
00652 
00653 
00654 
00655 TUserData ASC_PBEM::parseUserInfoLine( ASCString line )
00656 {
00657    TUserData info;
00658    
00659    info.userID = atoi( line.substr( 0, line.find( '\t' ) ).c_str() );
00660    int currentIndex = line.find( '\t' ) + 1;
00661    info.userName = line.substr( currentIndex );
00662    
00663    return info;
00664 }
00665 
00666 TGameInfo ASC_PBEM::parseGameInfoLine( ASCString line )
00667 {
00668    TGameInfo info;
00669    
00670    info.gameID = atoi( line.substr( 0, line.find( '\t' ) ).c_str() );
00671    int currentIndex = line.find( '\t' ) + 1;
00672    info.name = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
00673    currentIndex = line.find( '\t', currentIndex ) + 1;
00674    info.projectID = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
00675    currentIndex = line.find( '\t', currentIndex ) + 1;
00676    info.currentSlot = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
00677    currentIndex = line.find( '\t', currentIndex ) + 1;
00678    info.turn = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
00679    currentIndex = line.find( '\t', currentIndex ) + 1;
00680    info.currentSaveGameName = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
00681    currentIndex = line.find( '\t', currentIndex ) + 1;
00682    info.lastActive = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
00683    currentIndex = line.find( '\t', currentIndex ) + 1;
00684    info.currentPlayerName = line.substr( currentIndex );
00685    
00686    return info;
00687 }
00688 
00689 ASCString ASC_PBEM::getHeader()
00690 {
00691    ASCString headerString;
00692    for( int i=0; i<header.size(); i++ ) 
00693       headerString += header[ i ];
00694    return headerString;
00695 }
00696 
00697 ASCString ASC_PBEM::getBody()
00698 {
00699    ASCString bodyString;
00700    for( int i=0; i<body.size(); i++ ) 
00701       bodyString += body[ i ];
00702    return bodyString;
00703 }
00704 
00705 int ASC_PBEM::getStatusCode()
00706 {
00707    return statusCode;
00708 }
00709 
00710 int nmain(int argc, char *argv[])
00711 {
00712    std::cout << "start" << std::endl;
00713   curl_global_init(CURL_GLOBAL_ALL);
00714    
00715    if( false )
00716    {
00717       // schnelle übersicht, was man wie machen kann
00718       // wichtig: curl_global_init(CURL_GLOBAL_ALL); nicht vergessen ;)
00719       // weiter unten sind dann die sachen, die ich zum testen verwendet habe, 
00720       // ist halt unklarer geschrieben ;)
00721       
00782    }
00783    
00784    ASC_PBEM asc_pbem( "http://localhost:8080/ascServer/" );
00785    
00786    if( asc_pbem.isUsable() )
00787    {
00788       std::cout << "primary isUsable" << std::endl;
00789       /*
00790       if( asc_pbem.createAccount( "codeUser", "codePW", "jade@Rhiow.Errant" ) )
00791       {
00792          std::cout << "create codeUser worked" << std::endl;
00793          
00794          std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00795          std::cout << "body:" << asc_pbem.getBody() << std::endl;
00796          std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00797          
00798       }else
00799       {
00800          std::cout << "create codeUser failed" << std::endl;
00801       }
00802       */
00803       /*
00804       if( asc_pbem.activateAccount( "codeUser", "8670616" ))
00805       {
00806          std::cout << "activate codeUser worked" << std::endl;
00807       }else
00808       {
00809          std::cout << "activate codeUser failed" << std::endl;
00810       }
00811       */
00812       
00813       if( asc_pbem.login( "miau", "aaaa" ) )
00814       {
00815          std::cout << "login worked" << std::endl;
00816       }else
00817       {
00818          std::cout << "login failed" << std::endl;
00819       }
00820       /*
00821       ASCString uploadData = "a\ns\nd\nmiau\n";
00822       int gameID = 3;
00823       //std::cout << uploadData.size() << std::endl;
00824       if( asc_pbem.uploadFile( "some filename.ascpbm", uploadData.c_str(), uploadData.size(), gameID ) )
00825       {
00826          std::cout << "uploadFile worked" << std::endl;*/
00827          /*
00828          std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00829          std::cout << "body:" << asc_pbem.getBody() << std::endl;
00830          std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00831          */
00832          
00833          /*
00834       }else
00835       {
00836          std::cout << "uploadFile failed" << std::endl;
00837          
00838          std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00839          std::cout << "body:" << asc_pbem.getBody() << std::endl;
00840          std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00841          
00842       }*/
00843       
00844       std::vector<TGameInfo> allGames = asc_pbem.getCurrentGamesInfo( false );
00845       std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00846       std::cout << "body:" << asc_pbem.getBody() << std::endl;
00847       std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00848       std::cout << "allGames.size() = " << allGames.size() << std::endl;
00849 
00850       for( int i=0; i<allGames.size(); i++ )
00851       {
00852          TGameInfo game = allGames[ i ];
00853          std::cout << "Game " << game.gameID << std::endl;
00854          std::cout << "name " << game.name << std::endl;
00855          std::cout << "projectID " << game.projectID << std::endl;
00856          std::cout << "currentSlot " << game.currentSlot << std::endl;
00857          std::cout << "turn " << game.turn << std::endl;
00858          std::cout << "currentSaveGameName " << game.currentSaveGameName << std::endl;
00859          std::cout << "lastActive " << game.lastActive << std::endl;
00860          std::cout << "currentPlayerName " << game.currentPlayerName << std::endl;
00861       }
00862       
00863       /*
00864       char roles[ 8 ];
00865       int players[ 8 ];
00866       for( int i=0; i<8; i++ )
00867       {
00868          roles[ i ] = asc_pbem.ROLE_PLAYER;
00869          players[ i ] = -1;
00870       }
00871       
00872       players[ 0 ] = 11;
00873       players[ 1 ] = 11;
00874       
00875       int projectID = -1;
00876       int turn = 1;
00877       int currentSlot = 1;
00878       ASCString gameName = "evil isle";
00879       ASCString fileName = "evilisle-A-1.ascpbm";
00880       ASCString fileNamePattern = "evilisle-$p-$t.ascpbm";
00881       
00882       if( asc_pbem.createGame( fileName, fileName.c_str(), fileName.size(), gameName, fileNamePattern, roles, players, projectID, turn, currentSlot ) )
00883       {
00884          std::cout << "game created" << std::endl;
00885          std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00886          std::cout << "body:" << asc_pbem.getBody() << std::endl;
00887          std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00888       }else
00889       {
00890          std::cout << "game creation failed" << std::endl;
00891          std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00892          std::cout << "body:" << asc_pbem.getBody() << std::endl;
00893          std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00894       }
00895       */
00896       
00897       
00898       /*
00899       for( int i=0; i<allGames.size(); i++ )
00900       {
00901          TGameInfo game = allGames[ i ];
00902          
00903          if( game.gameID == 3 )
00904          {
00905             TFileData *download = asc_pbem.downloadGame( game );
00906             
00907             if( download == NULL )
00908             {
00909                std::cout << "download failed" << std::endl;
00910             }else
00911             {
00912                std::cout << "download worked:" << std::endl;
00913                std::cout << download->fileName << std::endl;
00914                std::cout << download->fileSize << std::endl;
00915                std::cout << download->fileData << std::endl;
00916                
00917                delete download;
00918             }
00919          }
00920          
00921          
00922       }
00923       */
00924 /*
00925       std::vector<TUserData> users = asc_pbem.getUserList();
00926       std::cout << "users.size() = " << users.size() << std::endl;
00927 
00928       for( int i=0; i<users.size(); i++ )
00929       {
00930          TUserData user = users[ i ];
00931          std::cout << "userName " << user.userName << std::endl;
00932          std::cout << "userID " << user.userID << std::endl;
00933       }
00934 */
00935       if( asc_pbem.logout() )
00936       {
00937          std::cout << "logout worked" << std::endl;
00938       }else
00939       {
00940          std::cout << "logout failed" << std::endl;
00941       }
00942    }else
00943    {
00944       std::cout << "primary NOT isUsable" << std::endl;
00945       
00946       std::cout << "header:" << asc_pbem.getHeader() << std::endl;
00947       std::cout << "body:" << asc_pbem.getBody() << std::endl;
00948       std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
00949    }
00950    return 0;    
00951 }
00952 

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