1 |
|
/* |
2 |
|
* pythonvm.c - A Python VM Implementation |
3 |
|
* |
4 |
|
* Copyright (C) 2003 netFluid Technology Ltd |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
8 |
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* This program is distributed in the hope that it will be useful, |
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU General Public License |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
*/ |
20 |
|
|
21 |
|
#if HAVE_UNISTD_H |
22 |
|
#include <unistd.h> |
23 |
|
#endif |
24 |
|
|
25 |
|
#include <python2.2/Python.h> |
26 |
|
|
27 |
|
#include "pythonvm.h" |
28 |
|
#include "dgreadenv.h" |
29 |
|
|
30 |
|
#include <gwtypes.h> |
31 |
|
#include <gwlog.h> |
32 |
|
|
33 |
|
static PyObject *pyModule = NULL; |
34 |
|
static PyObject *pyResult = NULL; |
35 |
|
static char *response = NULL; |
36 |
|
|
37 |
|
#ifdef __cplusplus |
38 |
|
extern "C" { |
39 |
|
#endif |
40 |
|
|
41 |
|
/* #define DBG gw_logf */ |
42 |
|
#define DBG if(0) gw_logf |
43 |
|
|
44 |
|
/* ------------------------------------------------------------------------- */ |
45 |
|
|
46 |
|
int |
47 |
|
pythonvm_entry(int argc, char *argv[]) |
48 |
|
{ |
49 |
|
char *filename = argv[0]; |
50 |
|
char *param = argv[1]; |
51 |
|
char *wsname = argv[2]; |
52 |
|
char *class = argv[3]; |
53 |
|
char *method = argv[4]; |
54 |
|
|
55 |
|
PyObject *pyParam = NULL; |
56 |
|
PyObject *pyWSName = NULL; |
57 |
|
PyObject *pyClass = NULL; |
58 |
|
PyObject *pyMethod = NULL; |
59 |
|
PyObject *pyFileName = NULL; |
60 |
|
PyObject *pyDGEEInvoke = NULL; |
61 |
|
PyObject *pyArgs = NULL; |
62 |
|
PyObject *pyDict = NULL; |
63 |
|
PyObject *pyFunc = NULL; |
64 |
|
|
65 |
|
gw_logf( LOG_FLOW, "pythonvm_entry() - IN" ); |
66 |
|
|
67 |
|
Py_Initialize(); |
68 |
|
pyParam = PyString_FromString(argv[1]); |
69 |
|
pyWSName = PyString_FromString(argv[2]); |
70 |
|
pyClass = PyString_FromString(argv[3]); |
71 |
|
pyMethod = PyString_FromString(argv[4]); |
72 |
|
|
73 |
|
/*PyRun_SimpleString("import sys\nprint sys.path");*/ |
74 |
|
/* filename to pass to invoke wrapper */ |
75 |
|
pyFileName = PyString_FromString(filename); |
76 |
|
|
77 |
|
/* wrapper module to load which serialises WS output */ |
78 |
|
pyDGEEInvoke = PyString_FromString("dgeeinvoke"); |
79 |
|
pyModule = PyImport_Import(pyDGEEInvoke); |
80 |
|
Py_DECREF(pyDGEEInvoke); |
81 |
|
|
82 |
|
gw_logf( LOG_FLOW, "Module loaded" ); |
83 |
|
if (pyModule != NULL) |
84 |
|
{ |
85 |
|
/* the code is loaded */ |
86 |
|
gw_logf( LOG_FLOW, "-=+ Execute Webservice Now +=- " ); |
87 |
|
|
88 |
|
/* get dictionary of items in dgeeinvoke module */ |
89 |
|
pyDict = PyModule_GetDict(pyModule); |
90 |
|
/* pyDict is a borrowed reference */ |
91 |
|
|
92 |
|
/* get the function */ |
93 |
|
pyFunc = PyDict_GetItemString(pyDict, "invokeDGEE"); |
94 |
|
/* pyFunc: Borrowed reference */ |
95 |
|
|
96 |
|
|
97 |
|
/* build the arguments list */ |
98 |
|
/* 4 here because last element is tuple */ |
99 |
|
pyArgs = PyTuple_New(5); |
100 |
|
PyTuple_SetItem(pyArgs, 0, pyParam); |
101 |
|
PyTuple_SetItem(pyArgs, 1, pyWSName); |
102 |
|
PyTuple_SetItem(pyArgs, 2, pyClass); |
103 |
|
PyTuple_SetItem(pyArgs, 3, pyMethod); |
104 |
|
PyTuple_SetItem(pyArgs, 4, PyTuple_New(0)); |
105 |
|
|
106 |
|
|
107 |
|
/* gw_logf( LOG_FLOW, sprintf("pyFunc = %x", &pyFunc));*/ |
108 |
|
if (pyFunc && PyCallable_Check(pyFunc)) |
109 |
|
{ |
110 |
|
/* This 'function' is callable */ |
111 |
|
gw_logf( LOG_FLOW, "Calling function" ); |
112 |
|
|
113 |
|
pyResult = PyObject_CallObject(pyFunc, pyArgs); |
114 |
|
if (pyResult==NULL) |
115 |
|
{ |
116 |
|
gw_logf( LOG_ERROR, "Function call failed!"); |
117 |
|
} |
118 |
|
else |
119 |
|
{ |
120 |
|
gw_logf( LOG_FLOW, "Function called" ); |
121 |
|
response = PyString_AsString( pyResult); |
122 |
|
} |
123 |
|
|
124 |
|
/* Do all the funky calling stuff in here */ |
125 |
|
/* pDict and pFunc are borrowed and must NOT be Py_DECREF-ed */ |
126 |
|
} |
127 |
|
else |
128 |
|
{ |
129 |
|
if (PyErr_Occurred()) |
130 |
|
PyErr_Print(); |
131 |
|
gw_logf(LOG_ERROR, sprintf("Cannot find func \"%s\"\n", method)); |
132 |
|
} |
133 |
|
} |
134 |
|
else |
135 |
|
{ |
136 |
|
if (PyErr_Occurred()) |
137 |
|
{ |
138 |
|
PyErr_Print(); |
139 |
|
} |
140 |
|
} |
141 |
|
Py_DECREF(pyModule); |
142 |
|
|
143 |
|
gw_logf( LOG_FLOW, "pythonvm_entry() - OUT" ); |
144 |
|
|
145 |
|
return 0; |
146 |
|
} |
147 |
|
|
148 |
|
/* ------------------------------------------------------------------------- */ |
149 |
|
|
150 |
|
char * |
151 |
|
pythonvm_get_reply() |
152 |
|
{ |
153 |
|
gw_logf( LOG_FLOW, "pythonvm_get_reply() - IN" ); |
154 |
|
gw_logf( LOG_FLOW, "-=+ Get Reply Data +=- " ); |
155 |
|
gw_logf( LOG_FLOW, "pythonvm_get_reply() - OUT" ); |
156 |
|
|
157 |
|
/*return "Some Reply Data";*/ |
158 |
|
return response; |
159 |
|
} |
160 |
|
|
161 |
|
/* ------------------------------------------------------------------------- */ |
162 |
|
|
163 |
|
void |
164 |
|
pythonvm_cleanup() |
165 |
|
{ |
166 |
|
gw_logf( LOG_FLOW, "pythonvm_cleanup() - IN" ); |
167 |
|
gw_logf( LOG_FLOW, "-=+ Cleanup any Resources +=- " ); |
168 |
|
|
169 |
|
Py_Finalize(); |
170 |
|
Py_Exit(0); |
171 |
|
gw_logf( LOG_FLOW, "pythonvm_cleanup() - OUT" ); |
172 |
|
} |
173 |
|
|
174 |
|
/* ------------------------------------------------------------------------- */ |
175 |
|
|
176 |
|
#ifdef __cplusplus |
177 |
|
}; |
178 |
|
#endif |