1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: service.pike,v 1.4 2006/06/10 00:59:04 exodusd Exp $
20
constant cvs_version="$Id: service.pike,v 1.4 2006/06/10 00:59:04 exodusd Exp $";
22
inherit "/kernel/module";
27
#include <exception.h>
28
#include <attributes.h>
32
function send_function;
33
function notify_function;
37
void create(string n, function sf, function nf, object sock) {
43
void send(mapping args) {
45
args->user = this_user();
48
void notify(mixed args) {
49
if ( functionp(notify_function) )
50
notify_function(args);
54
static mapping mServices;
55
static mapping mEvents;
57
static mapping mCallbacks;
67
void notify_services(int e, mixed ... args)
69
foreach(indices(mEvents), int event) {
70
if ( (event & e) > 0 ) {
71
foreach(mEvents[event], Service s) {
78
static void register_service_event(string name, int event)
80
if ( !arrayp(mEvents[event]) )
81
mEvents[event] = ({ mServices[name] });
83
for ( int i = 0; i < sizeof(mEvents[event]); i++ ) {
84
object service = mEvents[event][i];
85
if ( objectp(service) ) {
86
if ( service->name == name ) {
87
// overwrite existing service
88
mEvents[event][i] = mServices[name];
90
return; // do not register twice!!
93
mEvents[event] += ({ mServices[name] });
95
add_global_event(event, notify_services, PHASE_NOTIFY);
99
register_service(function send_function,function notify, string name,void|int|array event)
101
array allowed_ips = ({ "127.0.0.1" });
102
mixed server_ip = _Server->query_config("ip");
103
if ( stringp(server_ip) && sizeof(server_ip)>0 )
104
allowed_ips += ({ server_ip });
105
mixed config_allowed_ips = _Server->query_config("trusted_hosts");
106
if ( stringp(config_allowed_ips) ) {
107
foreach ( config_allowed_ips / " ", string ip )
108
if ( sizeof(ip)>0 ) allowed_ips += ({ ip });
110
if ( this_user() != USER("root")
111
&& search( allowed_ips, CALLER->get_ip() )<0 )
112
steam_error("Invalid call - cannot register non-local services !\n"+
113
"CALLER: %O, SERVER: %O",
114
CALLER->get_ip(), _Server->query_config("ip") );
117
MESSAGE("Service '" + name + "' registered!");
118
mServices[name] = Service(name, send_function, notify, CALLER);
119
if ( !arrayp(event) ) {
123
// todo: unregister service bei logout !
124
for ( int i = 0; i < sizeof(event); i++ )
125
register_service_event(name, event[i]);
128
void call_service(string name, mixed args)
130
Service s = mServices[name];
132
steam_error("No such Service: " + name);
133
mapping params = ([ ]);
134
params->params = args;
138
Async.Return call_service_async(string name, mixed args)
140
Service s = mServices[name];
142
steam_error("No such Service: " + name);
143
Async.Return res = Async.Return();
145
mCallbacks[_id] = res;
146
mapping params = ([ ]);
147
params->params = args;
155
void async_result(int id, mixed result)
157
// todo: check for socket
158
object res = mCallbacks[id];
159
if ( objectp(res) ) {
160
res->asyncResult(id, result);
165
void handle_service(object user, object obj, mixed id, mixed res)
167
if ( CALLER->get_ip() != "127.0.0.1" && CALLER->get_ip() != _Server->query_config("ip") )
168
steam_error("Invalid call - cannot callback non-local services !");
169
object ouid = this_user();
171
obj->handle_service(id, res);
174
int is_service(mixed name)
176
object service = mServices[name];
177
if ( !objectp(service) || !functionp(service->send_function) )
182
mapping get_services()
184
return copy_value(mServices);
187
string get_identifier() { return "ServiceManager"; }