From: <eg...@us...> - 2006-10-23 21:56:20
|
Revision: 152 http://svn.sourceforge.net/opengate/?rev=152&view=rev Author: egore Date: 2006-10-23 14:55:52 -0700 (Mon, 23 Oct 2006) Log Message: ----------- use libcurl (currently broken!) update few XML-files from website add comments to some files add more loading-mechanisms completely break network_meta (no authetication against meta-server possible!) Modified Paths: -------------- ChangeLog aclocal.m4 configure.in data/Components/aluminium.xml data/Components/antimony.xml data/Components/copper.xml data/Components/cpu.xml data/Components/gold.xml data/Components/laser_components.xml data/Components/ram.xml src/common/company.cpp src/common/company.h src/common/faction.cpp src/common/faction.h src/common/faction_storage.cpp src/common/faction_storage.h src/common/loader.cpp src/common/loader.h src/common/objects/ship.cpp src/opengate_client.cpp src/server/network.h src/server/network_meta.cpp src/server/network_meta.h Modified: ChangeLog =================================================================== --- ChangeLog 2006-09-02 09:12:15 UTC (rev 151) +++ ChangeLog 2006-10-23 21:55:52 UTC (rev 152) @@ -1,3 +1,17 @@ +2006-10-23 Christoph Brill <eg...@us...> + + * src/common/company.cpp, src/common/company.h, + src/common/objects/station.cpp, src/common/objects/station.h: more + XML-loading-work + + * src/common/faction.h, src/common/faction_storage.h: doxygen for + documentation + +2006-10-18 Christoph Brill <eg...@us...> + + * src/server/network_meta.cpp, src/server/network_meta.h: try porting + the login mechanism to the new server-infrastructure without success + 2006-09-02 Christoph Brill <eg...@us...> * src/server/network.h, src/server/network.cpp, Modified: aclocal.m4 =================================================================== --- aclocal.m4 2006-09-02 09:12:15 UTC (rev 151) +++ aclocal.m4 2006-10-23 21:55:52 UTC (rev 152) @@ -6606,7 +6606,8 @@ _PKG_TEXT ])], - [$4]) + [AC_MSG_RESULT([no]) + $4]) elif test $pkg_failed = untried; then ifelse([$4], , [AC_MSG_FAILURE(dnl [The pkg-config script could not be found or is too old. Make sure it Modified: configure.in =================================================================== --- configure.in 2006-09-02 09:12:15 UTC (rev 151) +++ configure.in 2006-10-23 21:55:52 UTC (rev 152) @@ -36,6 +36,10 @@ AM_CXXFLAGS="$AM_CXXFLAGS $LIBXML2_CFLAGS" AM_LDFLAGS="$AM_LDFLAGS $LIBXML2_LIBS" +#libcurl - http://curl.haxx.se/libcurl/ +PKG_CHECK_MODULES(LIBCURL, [libcurl >= 7.5]) +AM_CXXFLAGS="$AM_CXXFLAGS $LIBCURL_CFLAGS" +AM_LDFLAGS="$AM_LDFLAGS $LIBCURL_LIBS" AC_SUBST(AM_CXXFLAGS, "$AM_CXXFLAGS") AC_SUBST(AM_LDFLAGS, "$AM_LDFLAGS") Modified: data/Components/aluminium.xml =================================================================== (Binary files differ) Modified: data/Components/antimony.xml =================================================================== (Binary files differ) Modified: data/Components/copper.xml =================================================================== (Binary files differ) Modified: data/Components/cpu.xml =================================================================== (Binary files differ) Modified: data/Components/gold.xml =================================================================== (Binary files differ) Modified: data/Components/laser_components.xml =================================================================== (Binary files differ) Modified: data/Components/ram.xml =================================================================== (Binary files differ) Modified: src/common/company.cpp =================================================================== --- src/common/company.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/company.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -23,15 +23,27 @@ */ #include "company.h" +#include "loader.h" -Company::Company(std::string name) { - _name = name; +Company::Company(std::string filename) { + _filename = filename; + initialize(); } Company::~Company() { } +void Company::initialize() { + + Loader* loader = Loader::get_instance(); + loader->parse_file(_filename); + + // Naming + _name = loader->get_option("/company/name"); + +} + std::string Company::get_name() { return _name; } Modified: src/common/company.h =================================================================== --- src/common/company.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/company.h 2006-10-23 21:55:52 UTC (rev 152) @@ -25,18 +25,65 @@ #ifndef _OPENGATE_COMPANY_ #define _OPENGATE_COMPANY_ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + #include <string> +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * \brief This class defines a company in opengate. + * + * There are several companies in opengate. Which company produced a component + * is currently completely irrelevant, but will later be used for the economic + * calculations. + * + * \author Christoph Brill + * \version 1.0 + * \date 2006 + */ class Company { public: - Company(std::string name); + /*! + * This creates a company by loading it from a file based by it's filename. + * \param filename The relative path from the base datadir to the file + */ + Company(std::string filename); + + /*! + * This destroys the company. + */ virtual ~Company(); + /*! + * Getter for the name of the company. + * + * \return returns the name of the company (i.e. "T&P Systems") + */ std::string get_name(); private: + + virtual void initialize(); + + /*! + * The name of the company + */ std::string _name; + /*! + * The filename of the company + */ + std::string _filename; + }; +#ifdef __cplusplus +} #endif + +#endif /* _OPENGATE_COMPANY_ */ Modified: src/common/faction.cpp =================================================================== --- src/common/faction.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/faction.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -23,15 +23,27 @@ */ #include "faction.h" +#include "loader.h" -Faction::Faction(std::string name) { - _name = name; - _description = std::string(""); +Faction::Faction(std::string filename) { + _filename = filename; + initialize(); } Faction::~Faction() { } +void Faction::initialize() { + + Loader* loader = Loader::get_instance(); + loader->parse_file(_filename); + + // Naming + _name = loader->get_option("/faction/name"); + _description = loader->get_option("/faction/description"); + +} + std::string Faction::get_name() { return _name; } Modified: src/common/faction.h =================================================================== --- src/common/faction.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/faction.h 2006-10-23 21:55:52 UTC (rev 152) @@ -25,34 +25,72 @@ #ifndef _OPENGATE_FACTION_ #define _OPENGATE_FACTION_ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + #include <string> -/** - * This class defines the basic structures for a faction in opengate. A faction - * is for example "Solrain". +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * \brief This class defines the basic structures for a faction in opengate. * - * @author Christoph Brill + * A faction is for example "Solrain". A faction has a short description and + * a name. That's all for now. + * + * \author Christoph Brill + * \version 1.0 + * \date 2006 */ class Faction { public: - Faction(std::string name); + /*! + * This constructor "creates" a faction. Actually a faction is loaded + * from a file so that passing the filename will load the details. + * \param filename The relative path from the base datadir to the file + */ + Faction(std::string filename); + + /*! + * This deletes the faction. Since nothing has been done with a faction + * it is normally discarded. + */ ~Faction(); - /** + /*! * Getter for the name of the faction. - * - * @return returns the name of the faction (i.e. "Octavius") + * + * \return returns the name of the faction (i.e. "Octavius") */ std::string get_name(); - /** + /*! * Getter for the description of the faction. - * - * @return returns a short background description of the faction (like history, population, etc.) + * + * \return returns a short background description of the faction (like history, population, etc.) */ std::string get_description(); private: + + virtual void initialize(); + std::string _filename; + + /*! + * The name of the faction + */ std::string _name; + + /*! + * The short description of the faction + */ std::string _description; }; +#ifdef __cplusplus +} #endif + +#endif /* _OPENGATE_FACTION_ */ + Modified: src/common/faction_storage.cpp =================================================================== --- src/common/faction_storage.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/faction_storage.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -25,7 +25,8 @@ #include "faction_storage.h" #include "exception.h" -FactionStorage* FactionStorage::_storage_instance = NULL; +// Take care the singleton-member-variable is set to 0 at startup +FactionStorage* FactionStorage::_storage_instance = 0; FactionStorage::FactionStorage() { @@ -38,30 +39,30 @@ } } -Faction* FactionStorage::get_faction(std::string faction_name) { - if (!storage_contains(faction_name)) { - Faction* faction = new Faction(faction_name); +Faction* FactionStorage::get_faction(std::string faction_filename) { + if (!storage_contains(faction_filename)) { + Faction* faction = new Faction(faction_filename); data.push_back(faction); data.sort(); return faction; } - return find_faction(faction_name); + return find_faction(faction_filename); } -bool FactionStorage::storage_contains(std::string faction_name) { +bool FactionStorage::storage_contains(std::string faction_filename) { std::list<Faction*>::const_iterator iter; for (iter = data.begin(); iter != data.end(); iter++) { - if (((Faction*)*iter)->get_name() == faction_name) { + if (((Faction*)*iter)->get_name() == faction_filename) { return true; } } return false; } -Faction* FactionStorage::find_faction(std::string faction_name) { +Faction* FactionStorage::find_faction(std::string faction_filename) { std::list<Faction*>::const_iterator iter; for (iter = data.begin(); iter != data.end(); iter++) { - if (((Faction*)*iter)->get_name() == faction_name) { + if (((Faction*)*iter)->get_name() == faction_filename) { return (Faction*)*iter; } } Modified: src/common/faction_storage.h =================================================================== --- src/common/faction_storage.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/faction_storage.h 2006-10-23 21:55:52 UTC (rev 152) @@ -25,26 +25,100 @@ #ifndef _OPENGATE_FACTION_STORAGE_ #define _OPENGATE_FACTION_STORAGE_ -#include "faction.h" +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + #include <list> #include <string> +#include "faction.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*! + * \brief This class defines a loader for the factions in Opengate + * + * If you need a faction, use this class to load it. It is able to + * load a faction from it's XML-file and give it to you. It implements + * the "Singleton" design pattern so you just need to call get_instance() + * to get a valid faction storage. + * + * \author Christoph Brill + * \version 1.0 + * \date 2006 + */ class FactionStorage { public: - FactionStorage(); + /*! + * This destructes the storage and all stored factions with it. + */ ~FactionStorage(); + + /*! + * This method return the only instance of the faction storage. + * If you need one, use this method and don't create a new one. + * \return Returns the only instance of FactionStorage + */ inline static FactionStorage* get_instance() { - if (_storage_instance == NULL) { + if (_storage_instance == 0) { _storage_instance = new FactionStorage(); } return _storage_instance; } - Faction* get_faction(std::string faction_name); - bool storage_contains(std::string faction_name); - Faction* find_faction(std::string faction_name); + + /*! + * Get a faction by it's name. If the faction has been already + * loaded it is returned. If noone wanted it yet it is loaded + * from XML (using the Factions constructor). + * \param faction_filename Filename of the faction we want + * \return The faction as loaded from it's file + */ + Faction* get_faction(std::string faction_filename); + + /*! + * This method tests if a faction has been loaded. In most cases + * it's only instersing for internal cases, but if someone wants + * to know, here you can find the info. + * \param faction_filename Filename of the faction we want to know if it + * was loaded + * \return Returns true, if the Faction was loaded before + */ + bool storage_contains(std::string faction_filename); + + /*! + * This method works more or less like the get_faction()-method. + * The main (and important) difference is, that a faction is not + * loaded from a file if it was not found. If it wasn't found, + * an exception is thrown, so beware of messing with it. + * \param faction_filename Filename of the faction we want + * \return Returns the faction if found + */ + Faction* find_faction(std::string faction_filename); private: + + /*! + * This is the (currently) emtpy constructor of this storage. Noting + * is allocated, noting is done. + */ + FactionStorage(); + + /*! + * This it the variable that hold the single instance of the faction + * storage. It will be set to 0 in the corresponding cpp-file. + */ static FactionStorage* _storage_instance; + + /*! + * This variable holds the list of factions that have been loaded. + */ std::list<Faction*> data; }; +#ifdef __cplusplus +} #endif + +#endif /* _OPENGATE_FACTION_STORAGE_ */ Modified: src/common/loader.cpp =================================================================== --- src/common/loader.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/loader.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -25,13 +25,14 @@ #include "loader.h" #include "exception.h" -Loader* Loader::_loader_instance = NULL; +Loader* Loader::_loader_instance = 0; + Loader::Loader() { - root_doc = NULL; - root_context = NULL; + root_doc = 0; + root_context = 0; - /* Init libxml */ + // Init libxml xmlInitParser(); LIBXML_TEST_VERSION; } Modified: src/common/loader.h =================================================================== --- src/common/loader.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/loader.h 2006-10-23 21:55:52 UTC (rev 152) @@ -25,6 +25,10 @@ #ifndef _OPENGATE_LOADER_ #define _OPENGATE_LOADER_ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + #include <libxml/parser.h> #include <libxml/tree.h> #include <libxml/xpath.h> @@ -36,28 +40,100 @@ { #endif +/*! + * \brief This class loads XML-files using libxml2. + * + * This is one of the key-parts of opengate. It is able to load a XML-file + * and provice it's contents to other classes. It also validates the XML-file + * so that only valid content is loaded. It implements the "Singleton"-Design- + * Pattern so we don't have multiple loaders. + * + * \author Christoph Brill + * \version 1.0 + * \date 2006 + */ class Loader { public: - Loader(); + + /*! + * This unloads a possibly loaded document and shuts down the + * libxml2-parser + */ ~Loader(); + + /*! + * This returns the single instance of the loader + * \return the single instance of the loader. + */ inline static Loader* get_instance() { - if (_loader_instance == NULL) { + if (_loader_instance == 0) { _loader_instance = new Loader(); } return _loader_instance; } + + /*! + * This parses a file based on it's filename. It is loaded from the + * filesystem and parsed into a X-Path-Model to read the values + * from there. + * \param filename The filename to load the XML-document from + */ void parse_file(std::string filename); + + /*! + * Reads an option from an XML-document parsed into the X-Path-Model. + * All options are returned as string. + * \param path The name of the value (for example: "/ship/name") + * \return The value of the path as string (for example "Premia") + */ char* get_option(char* path); - std::vector<char*> get_option_array(char* path); //TODO: return array! + + /*! + * Reads an option from an XML-document parsed into the X-Path-Model. + * All options are returned as std::vector of string. + * \param path The name of the value (for example: "/ship/production-center") + * \return The value of the path as std::vector of string + */ + std::vector<char*> get_option_array(char* path); + + /*! + * Get the count of the options found under a X-Path. This can be used + * to read an array with get_option_array. + * \param path The name of the value (for example: "/ship/production-center") + * \return Returns the number of values found at the path, i.e. 3 + */ int get_option_count(char* path); + + /*! + * Sets an option at a given path. This can be used for runtime-modifications of + * the X-Path-Model and later for saving the XML-file. It is currently limited to + * storing non-array-pathes. + * \param path The name of the value (for example: "/ship/name") + * \param value The value for the path as string (for example "Premia") + */ void set_option(char* path, const char* value); private: + + /*! + * This instantiates the loader. It takes care that the document + * is empty. It also initializes libxml2 and verifies the version. + */ + Loader(); + + /*! + * Variable to store the single instance of the Loader + */ static Loader* _loader_instance; - /** the resulting document tree */ + /*! + * The resulting document tree + */ xmlDocPtr root_doc; - /** the x-path context */ + + /*! + * The x-path context + */ xmlXPathContext* root_context; /** @@ -65,10 +141,13 @@ * they exist. */ void clean_parser(); + /** * This method gets an x-path (see http://www.w3schools.com/xpath/xpath_examples.asp for examples) * and evaluates if there is a possible value. If so, the value is returned. Otherwise an exception * is thrown. + * \param path The name of the value (for example: "/ship/name") + * \return Returns the value */ xmlNodeSet* evaluate_xpath(char* path); }; Modified: src/common/objects/ship.cpp =================================================================== --- src/common/objects/ship.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/common/objects/ship.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -29,7 +29,6 @@ #include <vector> Ship::Ship(std::string filename) { - // TODO: intialize the variables _filename = filename; initialize(); } Modified: src/opengate_client.cpp =================================================================== --- src/opengate_client.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/opengate_client.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -70,7 +70,7 @@ cout << "-----------" << std::endl; Network* nw = new Network(); - nw->login("XXX", "YYY"); + nw->login("egore911", "dexter"); /* Loader* loader = new Loader(); loader->parse_file("money.xml"); Modified: src/server/network.h =================================================================== --- src/server/network.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/server/network.h 2006-10-23 21:55:52 UTC (rev 152) @@ -30,6 +30,7 @@ #endif #include <grapple.h> + #include "network_meta.h" class Network { Modified: src/server/network_meta.cpp =================================================================== --- src/server/network_meta.cpp 2006-09-02 09:12:15 UTC (rev 151) +++ src/server/network_meta.cpp 2006-10-23 21:55:52 UTC (rev 152) @@ -22,117 +22,32 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include <network_meta.h> - +#include "network_meta.h" #include <iostream> NetworkMeta::NetworkMeta() { - hostname = "opengate.sourceforge.net"; + hostname = "opacma.ontheserver.de"; + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); + //curl_easy_setopt(curl, CURLOPT_MUTE, 1); } -bool NetworkMeta::perform_request(std::string request, char* buffer) { - - int sd; - struct sockaddr_in sin; - struct sockaddr_in pin; - struct hostent *hp; - - /* go find out about the desired host machine */ - if ((hp = gethostbyname(hostname.c_str())) == 0) { - perror("gethostbyname"); - return false; - } - - /* fill in the socket structure with host information */ - memset(&pin, 0, sizeof(pin)); - pin.sin_family = AF_INET; - pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; - pin.sin_port = htons(80); - - /* grab an Internet domain socket */ - if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - perror("socket"); - return false; - } - - /* connect to PORT on HOST */ - if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) { - perror("connect"); - return false; - } - - /* send a message to the server PORT on machine HOST */ - if (send(sd, request.c_str(), request.length(), 0) == -1) { - perror("send"); - return false; - } - - /* wait for a message to come back from the server */ - if (recv(sd, buffer, BUFFER_SIZE, 0) == -1) { - perror("recv"); - return false; - } - - char* error = strstr(buffer, "404 Not Found\r\n"); - if (error) { - return false; - } - error = strstr(buffer, "400 Bad Request\r\n"); - if (error) { - return false; - } - - return true; +NetworkMeta::~NetworkMeta() { + curl_easy_cleanup(curl); } int NetworkMeta::check_login(std::string username, std::string password) { - char dir[BUFFER_SIZE]; - for (int i = 0; i < BUFFER_SIZE; i++) { - dir[i] = '\0'; - } - std::string request = "GET /game/game_login.php?user=" + username + "&password=" + password + " HTTP/1.1\r\nHost: " + hostname + "\r\n\r\n"; - if (!perform_request(request, dir)) { - return -1; - } - char* content = strstr(dir, "Content-Length: "); - if (content) { - // Apache 2.x + //FIXME: look at http://curl.haxx.se/lxr/source/docs/examples/getinmemory.c - // determine data length - content += strlen("Content-Length: "); - char* data_len_str = strstr(content, "\r\n"); - *data_len_str = '\0'; - int data_len = atoi(content); - *data_len_str = '\r'; + std::string request = "http://" + hostname + "/script/schnittstelle/?Action=login&user=" + username + "&password=" + password; - // determine data - char* data = strstr(content, "\r\n\r\n"); - data += strlen("\r\n\r\n"); - char* data2 = data + data_len; - *data2 = '\0'; - int result = atoi(data); - return result; + std::cout << request << std::endl; - } else { - // Apache 1.3x + curl_easy_setopt(curl, CURLOPT_URL, request.c_str()); + CURLcode res = curl_easy_perform(curl); - content = strstr(dir, "\r\n\r\n"); - - // determine data length - content += strlen("\r\n\r\n"); - char* data_len_str = strstr(content, "\r\n"); - *data_len_str = '\0'; - int data_len = atoi(content); - *data_len_str = '\r'; - - // determine data - data_len_str += strlen("\r\n"); - char* data = strstr(data_len_str, "\r\n"); - *data = '\0'; - int result = atoi(data_len_str); - return result; - } } bool NetworkMeta::verify_version() { Modified: src/server/network_meta.h =================================================================== --- src/server/network_meta.h 2006-09-02 09:12:15 UTC (rev 151) +++ src/server/network_meta.h 2006-10-23 21:55:52 UTC (rev 152) @@ -36,17 +36,20 @@ #include <netdb.h> #include <string.h> #include <string> +#include <curl/curl.h> #define BUFFER_SIZE 512 class NetworkMeta { public: NetworkMeta(); + ~NetworkMeta(); int check_login(std::string username, std::string password); bool verify_version(); private: std::string hostname; bool perform_request(std::string request, char* buffer); + CURL *curl; }; #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |