EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PyCallable.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: Zhur
24 */
25 
26 #include "eve-server.h"
27 
28 #include "PyCallable.h"
29 
31 : m_serviceDispatch(nullptr)
32 {
33 }
34 
36 {
37 }
38 
39 PyResult PyCallable::Call(const std::string &method, PyCallArgs &args) {
40  //call the dispatcher, capturing the result.
41  try {
42  PyResult res(m_serviceDispatch->Dispatch(method, args));
43 
44  if (is_log_enabled(SERVICE__CALL_TRACE)) {
45  _log(SERVICE__CALL_TRACE, "Call %s returned:", method.c_str());
46  res.ssResult->Dump(SERVICE__CALL_TRACE, " ");
47  }
48  return res;
49  } catch(PyException &e) {
50  if (is_log_enabled(SERVICE__CALL_ERROR)) {
51  _log(SERVICE__CALL_ERROR, "Call %s threw exception:", method.c_str());
52  e.ssException->Dump(SERVICE__CALL_ERROR, " ");
53  }
54  throw;
55  }
56 }
57 
58 
60 : client(c),
61  tuple(tup)
62 {
63  // PyIncRef( tup );
64  for (PyDict::const_iterator cur = dict->begin(); cur != dict->end(); cur++) {
65  if (!cur->first->IsString()) {
66  _log(SERVICE__ERROR, "Non-string key in call named arguments. Skipping.");
67  cur->first->Dump(SERVICE__ERROR, " ");
68  continue;
69  }
70  PyIncRef( cur->second );
71  byname[ cur->first->AsString()->content() ] = cur->second;
72  }
73 }
74 
77  for (auto cur : byname)
78  PySafeDecRef( cur.second );
79 }
80 
81 void PyCallArgs::Dump(LogType type) const {
82  if (!is_log_enabled(type))
83  return;
84 
85  _log(type, " Call Arguments:");
86  tuple->Dump(type, " ");
87  if (!byname.empty()) {
88  _log(type, " Named Arguments:");
89  for (auto cur : byname) {
90  _log(type, " %s", cur.first.c_str());
91  cur.second->Dump(type, " ");
92  }
93  }
94 }
95 
96 /* PyResult */
97 PyResult::PyResult() : ssResult( nullptr ), ssNamedResult( nullptr ) {}
99 : ssResult(result != nullptr ? result : PyStatic.NewNone()),
100 ssNamedResult( nullptr )
101 {}
102 PyResult::PyResult(PyRep* result, PyDict* namedResult)
103 : ssResult(result != nullptr ? result : PyStatic.NewNone()),
104 ssNamedResult(namedResult)
105 {}
106 
107 PyResult::PyResult( const PyResult& oth ) : ssResult( nullptr ), ssNamedResult( nullptr ) { *this = oth; }
109 
111 {
113  if (oth.ssResult != nullptr ) {
114  ssResult = oth.ssResult;
115  } else {
116  ssResult = PyStatic.NewNone();
117  }
119 
123 
124  return *this;
125 }
Base Python wire object.
Definition: PyRep.h:66
PyDict * ssNamedResult
Definition: PyCallable.h:66
CallDispatcher * m_serviceDispatch
Definition: PyCallable.h:90
#define _log(type, fmt,...)
Definition: logsys.h:124
#define PySafeIncRef(op)
Definition: PyRep.h:60
Python's dictionary.
Definition: PyRep.h:719
std::map< std::string, PyRep * > byname
Definition: PyCallable.h:51
Python tuple.
Definition: PyRep.h:567
void Dump(FILE *into, const char *pfx) const
Dumps object to file.
Definition: PyRep.cpp:84
* args
#define is_log_enabled(type)
Definition: logsys.h:78
PyRep * ssResult
Definition: PyCallable.h:65
Base class for exceptions that can be converted to python objects.
Definition: PyExceptions.h:39
PyResult & operator=(const PyResult &oth)
Definition: PyCallable.cpp:110
virtual PyResult Dispatch(const std::string &method_name, PyCallArgs &call)=0
#define PyStatic
Definition: PyRep.h:1209
virtual ~PyCallable()
Definition: PyCallable.cpp:35
Definition: Client.h:66
PyCallArgs(Client *c, PyTuple *tup, PyDict *dict)
Definition: PyCallable.cpp:59
#define PyIncRef(op)
Definition: PyRep.h:56
const_iterator begin() const
Definition: PyRep.h:766
LogType
Definition: logsys.h:59
storage_type::const_iterator const_iterator
Definition: PyRep.h:750
void Dump(LogType type) const
Definition: PyCallable.cpp:81
PyRep * ssException
Definition: PyExceptions.h:48
const_iterator end() const
Definition: PyRep.h:767
#define PySafeDecRef(op)
Definition: PyRep.h:61
virtual PyResult Call(const std::string &method, PyCallArgs &args)
Definition: PyCallable.cpp:39
PyTuple * tuple
Definition: PyCallable.h:50