~ubuntu-branches/ubuntu/saucy/steam/saucy-backports

« back to all changes in this revision

Viewing changes to server/modules/service.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (0.2.4) (3.2.1 trusty-proposed)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
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.
7
 
 *
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.
12
 
 *
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
16
 
 * 
17
 
 * $Id: service.pike,v 1.4 2006/06/10 00:59:04 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: service.pike,v 1.4 2006/06/10 00:59:04 exodusd Exp $";
21
 
 
22
 
inherit "/kernel/module";
23
 
 
24
 
#include <macros.h>
25
 
#include <classes.h>
26
 
#include <database.h>
27
 
#include <exception.h>
28
 
#include <attributes.h>
29
 
#include <events.h>
30
 
 
31
 
class Service {
32
 
  function   send_function;
33
 
  function notify_function;
34
 
  object            socket;
35
 
  string              name;
36
 
 
37
 
  void create(string n, function sf, function nf, object sock) {
38
 
    send_function = sf;
39
 
    notify_function = nf;
40
 
    name = n;
41
 
    socket = sock;
42
 
  }
43
 
  void send(mapping args) {
44
 
    args->name = name;
45
 
    args->user = this_user();
46
 
    send_function(args);
47
 
  }
48
 
  void notify(mixed args) {
49
 
    if ( functionp(notify_function) )
50
 
      notify_function(args);
51
 
  }
52
 
}
53
 
 
54
 
static mapping  mServices;
55
 
static mapping    mEvents;
56
 
static int            _id;
57
 
static mapping mCallbacks;
58
 
 
59
 
void init_module() 
60
 
{
61
 
  mServices  = ([ ]);
62
 
  mEvents    = ([ ]);
63
 
  mCallbacks = ([ ]);
64
 
  _id        =     1;
65
 
}
66
 
 
67
 
void notify_services(int e, mixed ... args)
68
 
{
69
 
  foreach(indices(mEvents), int event) {
70
 
    if ( (event & e) > 0  ) {
71
 
      foreach(mEvents[event], Service s) {
72
 
          s->notify(args);
73
 
      }
74
 
    }
75
 
  }
76
 
}
77
 
 
78
 
static void register_service_event(string name, int event) 
79
 
{
80
 
  if ( !arrayp(mEvents[event]) )
81
 
    mEvents[event] = ({ mServices[name] });
82
 
  else {
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];
89
 
        }
90
 
        return; // do not register twice!!
91
 
      }
92
 
    }
93
 
    mEvents[event] += ({ mServices[name] });
94
 
  }
95
 
  add_global_event(event, notify_services, PHASE_NOTIFY);
96
 
}
97
 
 
98
 
void 
99
 
register_service(function send_function,function notify, string name,void|int|array event)
100
 
{
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 });
109
 
    }
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") );
115
 
 
116
 
 
117
 
    MESSAGE("Service '" + name + "' registered!");    
118
 
    mServices[name] = Service(name, send_function, notify, CALLER);
119
 
    if ( !arrayp(event) ) {
120
 
      event = ({ event });
121
 
    }
122
 
 
123
 
  // todo: unregister service bei logout !
124
 
  for ( int i = 0; i < sizeof(event); i++ )
125
 
    register_service_event(name, event[i]);
126
 
}
127
 
 
128
 
void call_service(string name, mixed args) 
129
 
{
130
 
  Service s = mServices[name];
131
 
  if ( !objectp(s) )
132
 
    steam_error("No such Service: " + name);
133
 
  mapping params = ([ ]);
134
 
  params->params = args;
135
 
  s->send(params);
136
 
}
137
 
 
138
 
Async.Return call_service_async(string name, mixed args)
139
 
{
140
 
  Service s = mServices[name];
141
 
  if ( !objectp(s) )
142
 
    steam_error("No such Service: " + name);
143
 
  Async.Return res = Async.Return();
144
 
  _id++;
145
 
  mCallbacks[_id] = res;
146
 
  mapping params = ([ ]);
147
 
  params->params = args;
148
 
  params->id = _id;
149
 
 
150
 
  res->id = _id;
151
 
  s->send(params);
152
 
  return res;
153
 
}
154
 
 
155
 
void async_result(int id, mixed result)
156
 
{
157
 
  // todo: check for socket
158
 
  object res = mCallbacks[id];
159
 
  if ( objectp(res) ) {
160
 
    res->asyncResult(id, result);
161
 
  }
162
 
 
163
 
}
164
 
 
165
 
void handle_service(object user, object obj, mixed id, mixed res)
166
 
{
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();
170
 
  
171
 
  obj->handle_service(id, res);
172
 
}
173
 
 
174
 
int is_service(mixed name)
175
 
{
176
 
  object service = mServices[name];
177
 
  if ( !objectp(service) || !functionp(service->send_function) )
178
 
    return 0;
179
 
  return 1;
180
 
}
181
 
 
182
 
mapping get_services()
183
 
{
184
 
  return copy_value(mServices);
185
 
}
186
 
 
187
 
string get_identifier() { return "ServiceManager"; }
188