File: rpcBase.cpp

package info (click to toggle)
cvsnt 2.5.03.2382-3.3%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 32,288 kB
  • ctags: 24,567
  • sloc: ansic: 136,648; cpp: 102,037; sh: 42,846; asm: 3,495; makefile: 1,763; ada: 1,681; perl: 1,352; pascal: 1,089; cs: 1,008; yacc: 805; python: 453; xml: 263; sql: 210; lisp: 7
file content (242 lines) | stat: -rw-r--r-- 5,992 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
	CVSNT Generic API
    Copyright (C) 2004 Tony Hoyle and March-Hare Software Ltd

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "lib/api_system.h"
#include "cvs_string.h"
#include "cvs_smartptr.h"
#include "Codepage.h"
#include "Xmlapi.h"
#include "rpcBase.h"

CrpcBase::CrpcBase()
{
}

CrpcBase::~CrpcBase()
{
}

CXmlNode *CrpcBase::createNewParams(CXmlTree& tree)
{
	return new CXmlNode(&tree, CXmlNode::XmlTypeNode, "params", NULL);
}

bool CrpcBase::addParam(CXmlNode *params, const char *name, const char *value)
{
	CXmlNode *param;
	
	if(!strcmp(params->GetName(),"params"))
		param = params->NewNode("param", NULL);
	else if(!strcmp(params->GetName(),"struct"))
	{
		param = params->NewNode("member", NULL);
		if(name)
			param->NewNode("name",name);
	}
	else
		param = params; // array (name will also be null)
	CXmlNode *val = param->NewNode("value",NULL);
	val->NewNode("string",value);
	return true;
}

bool CrpcBase::addParam(CXmlNode *params, const char *name, int value)
{
	char tmp[32];
	snprintf(tmp,sizeof(tmp),"%d",value);
	CXmlNode *param;
	
	if(!strcmp(params->GetName(),"params"))
		param = params->NewNode("param", NULL);
	else if(!strcmp(params->GetName(),"struct"))
	{
		param = params->NewNode("member", NULL);
		if(name)
			param->NewNode("name",name);
	}
	else
		param = params; // array (name will also be null)
	CXmlNode *val = param->NewNode("value",NULL);
	val->NewNode("i4",tmp);
	return true;
}

bool CrpcBase::addParam(CXmlNode *params, const char *name, rpcObject *obj)
{
	CXmlNode *param;
	
	if(!strcmp(params->GetName(),"params"))
		param = params->NewNode("param", NULL);
	else if(!strcmp(params->GetName(),"struct"))
	{
		param = params->NewNode("member", NULL);
		if(name)
			param->NewNode("name",name);
	}
	else
		param = params; // array (name will also be null)
	CXmlNode *val = param->NewNode("value",NULL);
	return obj->Marshall(val);
}

bool CrpcBase::rpcInt(CXmlNode *param, const char *name, int& value)
{
	cvs::string fnd;
	CXmlNode *val, *obj;

	if(!strcmp(param->GetName(),"param")) 
		val=param->getChild(0);
	else
		val = param;
	if(val && !strcmp(val->GetName(),"struct"))
	{
		if(name)
		{
			cvs::sprintf(fnd,64,"member[@name='%s']",name);
			val = val->Lookup(fnd.c_str());
			if(!val)
				return false;
		}
		else
			val = param->getChild(0);
		val = val->Lookup("value");
	}
	if(!val || strcmp(val->GetName(),"value"))
		return false;
	obj = val->getChild(0);
	if(!obj || strcmp(obj->GetName(),"i4"))
		return false;
	value = atoi(obj->GetValue());
	return true;
}

bool CrpcBase::rpcString(CXmlNode *param, const char *name, cvs::string& value)
{
	cvs::string fnd;
	CXmlNode *val, *obj;

	if(!strcmp(param->GetName(),"param")) 
		val=param->getChild(0);
	else
		val = param;
	if(val && !strcmp(val->GetName(),"struct"))
	{
		if(name)
		{
			cvs::sprintf(fnd,64,"member[@name='%s']",name);
			val = val->Lookup(fnd.c_str());
			if(!val)
				return false;
		}
		else
			val = param->getChild(0);
		val = val->Lookup("value");
	}
	if(!val || strcmp(val->GetName(),"value"))
		return false;
	obj = val->getChild(0);
	if(!obj || strcmp(obj->GetName(),"string"))
		return false;
	value = obj->GetValue();
	return true;
}

bool CrpcBase::rpcArray(CXmlNode* param, const char *name, CXmlNode*& node)
{
	CXmlNode *val;
	if(!strcmp(param->GetName(),"param"))
		val=param->getChild(0);
	else
		val = param;
	if(!val || strcmp(val->GetName(),"array"))
		return false;

	if(!node)
	{
		CXmlNode *data = val->getChild(0);
		if(!data || strcmp(data->GetName(),"data"))
			return false;
		node = data->getChild(0);
		return true;
	}
	else
	{
		node = node->getParent()->Next();
		if(!node || strcmp(node->GetName(),"data"))
			return false;
		node = node->getChild(0);
		return true;
	}
}

bool CrpcBase::rpcObj(CXmlNode* param, const char *name, rpcObject *rpcObj)
{
	cvs::string fnd;
	CXmlNode *val, *obj;

	if(!strcmp(param->GetName(),"param")) 
		val=param->getChild(0);
	else
		val = param;
	if(val && !strcmp(val->GetName(),"struct"))
	{
		if(name)
		{
			cvs::sprintf(fnd,64,"member[@name='%s']",name);
			val = val->Lookup(fnd.c_str());
			if(!val)
				return false;
		}
		else
			val = param->getChild(0);
		val = val->Lookup("value");
	}
	if(!val || strcmp(val->GetName(),"value"))
		return false;
	obj = val->getChild(0);
	if(!obj || strcmp(obj->GetName(),"struct"))
		return false;
	return rpcObj->Marshall(obj);
}

CXmlNode *CrpcBase::rpcFault(CXmlTree& tree, int err, const char *error)
{
	CXmlNode *fault = new CXmlNode(&tree, CXmlNode::XmlTypeNode, "fault", NULL);
	CXmlNode *value = fault->NewNode("value", NULL);
	CXmlNode *struc = value->NewNode("struct", NULL);
	addParam(struc,"faultCode",err);
	addParam(struc,"faultString",error);
	return fault;
}

CXmlNode *CrpcBase::rpcResponse(CXmlNode* result)
{
	CXmlNode *response = new CXmlNode(result->getTree(), CXmlNode::XmlTypeNode, "methodResponse", NULL);
	CXmlNode *params = response->NewNode("params",NULL);
	params->Paste(result);
	return response;
}

CXmlNode *CrpcBase::rpcCall(const char *method, CXmlNode *param)
{
	CXmlNode *methodNode = new CXmlNode(param->getTree(), CXmlNode::XmlTypeNode, "methodCall", NULL);
	methodNode->NewNode("methodName",method);
	CXmlNode *params = methodNode->NewNode("params",NULL);
	params->Paste(param);
	return methodNode;
}