EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
APIServer.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Aknor Jaden, adapted from ImageServer.h authored by caytchen
24 */
25 
26 #include "eve-server.h"
27 
28 #include "EVEServerConfig.h"
36 #include "apiserver/APIServer.h"
38 
39 const char *const APIServer::FallbackURL = "http://api.eveonline.com/";
40 
42 {
43  runonce = false;
44  std::stringstream urlBuilder;
45  urlBuilder << "http://" << sConfig.net.apiServer << ":" << (sConfig.net.apiServerPort) << "/";
46  _url = urlBuilder.str();
47 }
48 
50 {
51  if( !runonce )
52  {
53  m_APIServiceManagers.insert(std::make_pair("base", new APIServiceManager(services)));
54  m_APIServiceManagers.insert(std::make_pair("account", new APIAccountManager(services)));
55  m_APIServiceManagers.insert(std::make_pair("admin", new APIAdminManager(services)));
56  m_APIServiceManagers.insert(std::make_pair("char", new APICharacterManager(services)));
57  m_APIServiceManagers.insert(std::make_pair("corp", new APICorporationManager(services)));
58  m_APIServiceManagers.insert(std::make_pair("eve", new APIEveSystemManager(services)));
59  m_APIServiceManagers.insert(std::make_pair("map", new APIMapManager(services)));
60  m_APIServiceManagers.insert(std::make_pair("object", new APIActiveObjectManager(services)));
61  m_APIServiceManagers.insert(std::make_pair("server", new APIServerManager(services)));
62  }
63 
64  runonce = true;
65 }
66 
67 std::tr1::shared_ptr<std::vector<char> > APIServer::GetXML(const APICommandCall * pAPICommandCall)
68 {
69  //if( m_APIServiceManagers.find(pAPICommandCall->at(0).first) != m_APIServiceManagers.end() )
70  if( pAPICommandCall->find( "service" ) == pAPICommandCall->end() )
71  {
72  sLog.Error( "APIserver::GetXML()", "Cannot find 'service' specifier in pAPICommandCall packet" );
73  return std::tr1::shared_ptr<std::vector<char> >(new std::vector<char>() );
74  //return std::tr1::shared_ptr<std::string>(new std::string(""));
75  }
76 
77  if( m_APIServiceManagers.find(pAPICommandCall->find( "service" )->second) != m_APIServiceManagers.end() )
78  {
79  // Get reference to service manager object and call ProcessCall() with the pAPICommandCall packet
80  //m_xmlString = m_APIServiceManagers.find("base")->second->ProcessCall(pAPICommandCall);
81  m_xmlString = m_APIServiceManagers.find( pAPICommandCall->find( "service" )->second )->second->ProcessCall( pAPICommandCall );
82 
83  // Convert the std::string to the std::vector<char>:
84  std::tr1::shared_ptr<std::vector<char> > ret = std::tr1::shared_ptr<std::vector<char> >(new std::vector<char>());
85  unsigned long len = m_xmlString->length();
86  for(size_t i=0; i<m_xmlString->length(); i++)
87  ret->push_back(m_xmlString->at(i));
88 
89  return ret;
90  }
91  else
92  {
93  // Service call not found, so return NULL:
94  return std::tr1::shared_ptr<std::vector<char> >(new std::vector<char>() );
95  //return NULL;
96  //m_xmlString = m_APIServiceManagers.find("base")->second->ProcessCall(pAPICommandCall);
97  }
98 }
99 
100 std::string& APIServer::url()
101 {
102  return _url;
103 }
104 
106 {
107  _ioThread = std::unique_ptr<boost::asio::detail::thread>(new boost::asio::detail::thread(std::bind(&APIServer::RunInternal, this)));
108 }
109 
111 {
112  _io->stop();
113  _ioThread->join();
114 }
115 
117 {
118  _io = std::unique_ptr<boost::asio::io_context>(new boost::asio::io_context());
119  _listener = std::unique_ptr<APIServerListener>(new APIServerListener(*_io));
120  _io->run();
121 }
122 
123 APIServer::Lock::Lock(boost::asio::detail::mutex& mutex)
124  : _mutex(mutex)
125 {
126  _mutex.lock();
127 }
128 
130 {
131  _mutex.unlock();
132 }
static const char *const FallbackURL
Definition: APIServer.h:58
#define sConfig
A macro for easier access to the singleton.
std::string & url()
Definition: APIServer.cpp:100
std::map< std::string, APIServiceManager * > m_APIServiceManagers
Definition: APIServer.h:73
std::tr1::shared_ptr< std::vector< char > > GetXML(const APICommandCall *pAPICommandCall)
Definition: APIServer.cpp:67
void Run()
Definition: APIServer.cpp:105
Handles listening for new clients.
std::unique_ptr< boost::asio::detail::thread > _ioThread
Definition: APIServer.h:63
std::map< std::string, std::string > APICommandCall
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
Generic Base Class used to derive classes for specific service handlers (character, corporation, etc)
bool runonce
Definition: APIServer.h:69
std::unique_ptr< boost::asio::io_context > _io
Definition: APIServer.h:64
void CreateServices(const PyServiceMgr &services)
Definition: APIServer.cpp:49
void RunInternal()
Definition: APIServer.cpp:116
std::string _url
Definition: APIServer.h:66
void Stop()
Definition: APIServer.cpp:110
std::tr1::shared_ptr< std::string > m_xmlString
Definition: APIServer.h:71
Lock(boost::asio::detail::mutex &mutex)
Definition: APIServer.cpp:123
boost::asio::detail::mutex & _mutex
Definition: APIServer.h:81
std::unique_ptr< APIServerListener > _listener
Definition: APIServer.h:65