File: system.cpp

package info (click to toggle)
bayonne 2.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 11,180 kB
  • ctags: 3,662
  • sloc: cpp: 38,791; sh: 9,323; ansic: 1,391; makefile: 485; perl: 471; java: 405; cs: 402; php: 354; python: 293
file content (204 lines) | stat: -rw-r--r-- 4,512 bytes parent folder | download
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
// Copyright (C) 2005 Open Source Telecom Corporation.
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include <engine.h>
#include <cc++/slog.h>
#include <cc++/socket.h>

namespace moduleXMLRPC {
using namespace ost;
using namespace std;

typedef	Bayonne::rpcint_t rpcint_t;

static void system_identity(BayonneRPC *rpc)
{
	if(rpc->getCount())
		rpc->sendFault(3, "Invalid Parameters");
	else
		rpc->buildResponse("s", "Bayonne/" VERSION);
}

static void system_status(BayonneRPC *rpc)
{
	time_t now, started;

	if(rpc->getCount())
	{
		rpc->sendFault(3, "Invalid Parameters");
		return;
	}

	time(&now);
	started = now - Bayonne::uptime();
	rpc->buildResponse("(titissi)",
		"date", now, 
		"date_int", (rpcint_t)now,
		"started", started,
		"started_int", (rpcint_t)started,
		"name", "Bayonne",
		"version", VERSION,
		"methods_known", (rpcint_t)Bayonne::RPCNode::getCount()
	);
}

static void system_methodHelp(BayonneRPC *rpc)
{
	Bayonne::RPCNode *node = Bayonne::RPCNode::getFirst();
	Bayonne::RPCDefine *methods;
	char *prefix, *method;

	if(rpc->getCount() != 1)
	{
		rpc->sendFault(3, "Invalid Parameters");
		return;
	}

	prefix = (char *)rpc->getIndexed(1);
	if(!prefix || !*prefix)
	{
		rpc->sendFault(4, "Invalid Method Argument");
		return;
	}
	method = strchr(prefix, '.');
	if(method)
		*(method++) = 0;
	else
	{
		rpc->sendFault(4, "Invalid Method Argument");
		return;
	}

	while(node)
	{
		if(!stricmp(node->getPrefix(), prefix))
			methods = node->getMethods();
		else
			methods = NULL;
		while(methods && methods->name)
		{
			if(!stricmp(methods->name, method))
			{
				rpc->buildResponse("s", methods->help);
				return;
			}
			++methods;
		}
		node = node->getNext();
	}
	rpc->sendFault(4, "Unknown Method");
}

static void system_methodSignature(BayonneRPC *rpc)
{
	Bayonne::RPCNode *node = Bayonne::RPCNode::getFirst();
	Bayonne::RPCDefine *methods;
	char *prefix, *method;
	unsigned count = 0;

	if(rpc->getCount() != 1)
	{
		rpc->sendFault(3, "Invalid Parameters");
		return;
	}

	prefix = (char *)rpc->getIndexed(1);
	if(!prefix || !*prefix)
	{
		rpc->sendFault(4, "Invalid Method Argument");
		return;
	}
	method = strchr(prefix, '.');
	if(method)
		*(method++) = 0;
	else
	{
		rpc->sendFault(4, "Invalid Method Argument");
		return;
	}

	while(node)
	{
		if(!stricmp(node->getPrefix(), prefix))
			methods = node->getMethods();
		else
			methods = NULL;
		while(methods && methods->name)
		{
			if(!stricmp(methods->name, method))
			{
				if(!count)
					rpc->buildResponse("[");

				rpc->buildResponse("!s", methods->signature);
				++count;
			}
			++methods;
		}
		node = node->getNext();
	}
	if(!count)		
		rpc->sendFault(4, "Unknown Method");
	else
		rpc->buildResponse("]");
}


static void system_listMethods(BayonneRPC *rpc)
{
	Bayonne::RPCNode *node = Bayonne::RPCNode::getFirst();
	Bayonne::RPCDefine *methods;
	char buffer[65];

	if(rpc->getCount())
	{
		rpc->sendFault(3, "Invalid Parameters");
		return;
	}

	rpc->buildResponse("[");

	while(node)
	{
		methods = node->getMethods();
		while(methods->name)
		{
			snprintf(buffer, sizeof(buffer), "%s.%s",
				node->getPrefix(), methods->name);
			rpc->buildResponse("!s", buffer);				
			++methods;
		}
		node = node->getNext();
	}
	rpc->buildResponse("]");
}

static Bayonne::RPCDefine dispatch[] = {
	{"identity", system_identity, 
		"Identify server type and version", "string"}, 
	{"listMethods", system_listMethods, 
		"List server methods", "array"},
	{"methodHelp", system_methodHelp, 
		"Get help text for method", "string, string"},
	{"methodSignature", system_methodSignature, 
		"Get paramater signature for specified method", "array, string"},
	{"status", system_status,
		"Return server status information", "struct"},
	{NULL, NULL, NULL, NULL}};

static Bayonne::RPCNode xmlrpc_system("system", dispatch);

}; // namespace