tbevent.py 24.5 KB
Newer Older
1
# This file was created automatically by SWIG 1.3.29.
2 3
# Don't modify this file, modify the SWIG interface instead.
# This file is compatible with both classic and new-style classes.
Timothy Stack's avatar
Timothy Stack committed
4

5
import _tbevent
6 7 8 9
import new
new_instancemethod = new.instancemethod
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
10
    if (name == "this"):
11 12
        if type(value).__name__ == 'PySwigObject':
            self.__dict__[name] = value
13 14 15
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
16 17 18 19 20 21 22
    if (not static) or hasattr(self,name):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)
23 24

def _swig_getattr(self,class_type,name):
25
    if (name == "thisown"): return self.this.own()
26 27 28 29
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError,name

30 31 32 33 34
def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

35 36 37 38 39 40 41
import types
try:
    _object = types.ObjectType
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0
Timothy Stack's avatar
Timothy Stack committed
42
del types
43 44 45 46 47 48 49 50


MAXHOSTNAMELEN = _tbevent.MAXHOSTNAMELEN
class event_handle(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, event_handle, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, event_handle, name)
51
    __repr__ = _swig_repr
52 53
    __swig_setmethods__["server"] = _tbevent.event_handle_server_set
    __swig_getmethods__["server"] = _tbevent.event_handle_server_get
Timothy Stack's avatar
Timothy Stack committed
54
    if _newclass:server = property(_tbevent.event_handle_server_get, _tbevent.event_handle_server_set)
55 56
    __swig_setmethods__["status"] = _tbevent.event_handle_status_set
    __swig_getmethods__["status"] = _tbevent.event_handle_status_get
Timothy Stack's avatar
Timothy Stack committed
57
    if _newclass:status = property(_tbevent.event_handle_status_get, _tbevent.event_handle_status_set)
58 59
    __swig_setmethods__["keydata"] = _tbevent.event_handle_keydata_set
    __swig_getmethods__["keydata"] = _tbevent.event_handle_keydata_get
Timothy Stack's avatar
Timothy Stack committed
60
    if _newclass:keydata = property(_tbevent.event_handle_keydata_get, _tbevent.event_handle_keydata_set)
61 62
    __swig_setmethods__["keylen"] = _tbevent.event_handle_keylen_set
    __swig_getmethods__["keylen"] = _tbevent.event_handle_keylen_get
Timothy Stack's avatar
Timothy Stack committed
63
    if _newclass:keylen = property(_tbevent.event_handle_keylen_get, _tbevent.event_handle_keylen_set)
64 65 66
    __swig_setmethods__["do_loop"] = _tbevent.event_handle_do_loop_set
    __swig_getmethods__["do_loop"] = _tbevent.event_handle_do_loop_get
    if _newclass:do_loop = property(_tbevent.event_handle_do_loop_get, _tbevent.event_handle_do_loop_set)
67 68
    __swig_setmethods__["connect"] = _tbevent.event_handle_connect_set
    __swig_getmethods__["connect"] = _tbevent.event_handle_connect_get
Timothy Stack's avatar
Timothy Stack committed
69
    if _newclass:connect = property(_tbevent.event_handle_connect_get, _tbevent.event_handle_connect_set)
70 71
    __swig_setmethods__["disconnect"] = _tbevent.event_handle_disconnect_set
    __swig_getmethods__["disconnect"] = _tbevent.event_handle_disconnect_get
Timothy Stack's avatar
Timothy Stack committed
72
    if _newclass:disconnect = property(_tbevent.event_handle_disconnect_get, _tbevent.event_handle_disconnect_set)
73 74
    __swig_setmethods__["mainloop"] = _tbevent.event_handle_mainloop_set
    __swig_getmethods__["mainloop"] = _tbevent.event_handle_mainloop_get
Timothy Stack's avatar
Timothy Stack committed
75
    if _newclass:mainloop = property(_tbevent.event_handle_mainloop_get, _tbevent.event_handle_mainloop_set)
76 77
    __swig_setmethods__["notify"] = _tbevent.event_handle_notify_set
    __swig_getmethods__["notify"] = _tbevent.event_handle_notify_get
Timothy Stack's avatar
Timothy Stack committed
78
    if _newclass:notify = property(_tbevent.event_handle_notify_get, _tbevent.event_handle_notify_set)
79 80
    __swig_setmethods__["subscribe"] = _tbevent.event_handle_subscribe_set
    __swig_getmethods__["subscribe"] = _tbevent.event_handle_subscribe_get
Timothy Stack's avatar
Timothy Stack committed
81
    if _newclass:subscribe = property(_tbevent.event_handle_subscribe_get, _tbevent.event_handle_subscribe_set)
82 83 84
    __swig_setmethods__["unsubscribe"] = _tbevent.event_handle_unsubscribe_set
    __swig_getmethods__["unsubscribe"] = _tbevent.event_handle_unsubscribe_get
    if _newclass:unsubscribe = property(_tbevent.event_handle_unsubscribe_get, _tbevent.event_handle_unsubscribe_set)
85 86 87 88 89 90 91 92
    def __init__(self, *args): 
        this = _tbevent.new_event_handle(*args)
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _tbevent.delete_event_handle
    __del__ = lambda self : None;
event_handle_swigregister = _tbevent.event_handle_swigregister
event_handle_swigregister(event_handle)
93 94 95 96 97 98

class event_notification(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, event_notification, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, event_notification, name)
99 100 101 102
    __repr__ = _swig_repr
    __swig_setmethods__["pubsub_notification"] = _tbevent.event_notification_pubsub_notification_set
    __swig_getmethods__["pubsub_notification"] = _tbevent.event_notification_pubsub_notification_get
    if _newclass:pubsub_notification = property(_tbevent.event_notification_pubsub_notification_get, _tbevent.event_notification_pubsub_notification_set)
103 104
    __swig_setmethods__["has_hmac"] = _tbevent.event_notification_has_hmac_set
    __swig_getmethods__["has_hmac"] = _tbevent.event_notification_has_hmac_get
Timothy Stack's avatar
Timothy Stack committed
105
    if _newclass:has_hmac = property(_tbevent.event_notification_has_hmac_get, _tbevent.event_notification_has_hmac_set)
106 107 108 109 110 111 112 113
    def __init__(self, *args): 
        this = _tbevent.new_event_notification(*args)
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _tbevent.delete_event_notification
    __del__ = lambda self : None;
event_notification_swigregister = _tbevent.event_notification_swigregister
event_notification_swigregister(event_notification)
114 115 116 117 118 119

class address_tuple(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, address_tuple, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, address_tuple, name)
120
    __repr__ = _swig_repr
121 122
    __swig_setmethods__["site"] = _tbevent.address_tuple_site_set
    __swig_getmethods__["site"] = _tbevent.address_tuple_site_get
Timothy Stack's avatar
Timothy Stack committed
123
    if _newclass:site = property(_tbevent.address_tuple_site_get, _tbevent.address_tuple_site_set)
124 125
    __swig_setmethods__["expt"] = _tbevent.address_tuple_expt_set
    __swig_getmethods__["expt"] = _tbevent.address_tuple_expt_get
Timothy Stack's avatar
Timothy Stack committed
126
    if _newclass:expt = property(_tbevent.address_tuple_expt_get, _tbevent.address_tuple_expt_set)
127 128
    __swig_setmethods__["group"] = _tbevent.address_tuple_group_set
    __swig_getmethods__["group"] = _tbevent.address_tuple_group_get
Timothy Stack's avatar
Timothy Stack committed
129
    if _newclass:group = property(_tbevent.address_tuple_group_get, _tbevent.address_tuple_group_set)
130 131
    __swig_setmethods__["host"] = _tbevent.address_tuple_host_set
    __swig_getmethods__["host"] = _tbevent.address_tuple_host_get
Timothy Stack's avatar
Timothy Stack committed
132
    if _newclass:host = property(_tbevent.address_tuple_host_get, _tbevent.address_tuple_host_set)
133 134
    __swig_setmethods__["objtype"] = _tbevent.address_tuple_objtype_set
    __swig_getmethods__["objtype"] = _tbevent.address_tuple_objtype_get
Timothy Stack's avatar
Timothy Stack committed
135
    if _newclass:objtype = property(_tbevent.address_tuple_objtype_get, _tbevent.address_tuple_objtype_set)
136 137
    __swig_setmethods__["objname"] = _tbevent.address_tuple_objname_set
    __swig_getmethods__["objname"] = _tbevent.address_tuple_objname_get
Timothy Stack's avatar
Timothy Stack committed
138
    if _newclass:objname = property(_tbevent.address_tuple_objname_get, _tbevent.address_tuple_objname_set)
139 140
    __swig_setmethods__["eventtype"] = _tbevent.address_tuple_eventtype_set
    __swig_getmethods__["eventtype"] = _tbevent.address_tuple_eventtype_get
Timothy Stack's avatar
Timothy Stack committed
141
    if _newclass:eventtype = property(_tbevent.address_tuple_eventtype_get, _tbevent.address_tuple_eventtype_set)
142 143
    __swig_setmethods__["scheduler"] = _tbevent.address_tuple_scheduler_set
    __swig_getmethods__["scheduler"] = _tbevent.address_tuple_scheduler_get
Timothy Stack's avatar
Timothy Stack committed
144
    if _newclass:scheduler = property(_tbevent.address_tuple_scheduler_get, _tbevent.address_tuple_scheduler_set)
145 146 147
    __swig_setmethods__["timeline"] = _tbevent.address_tuple_timeline_set
    __swig_getmethods__["timeline"] = _tbevent.address_tuple_timeline_get
    if _newclass:timeline = property(_tbevent.address_tuple_timeline_get, _tbevent.address_tuple_timeline_set)
148 149 150 151 152 153 154 155
    def __init__(self, *args): 
        this = _tbevent.new_address_tuple(*args)
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _tbevent.delete_address_tuple
    __del__ = lambda self : None;
address_tuple_swigregister = _tbevent.address_tuple_swigregister
address_tuple_swigregister(address_tuple)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

ADDRESSTUPLE_ALL = _tbevent.ADDRESSTUPLE_ALL
OBJECTTYPE_TESTBED = _tbevent.OBJECTTYPE_TESTBED
OBJECTTYPE_TRAFGEN = _tbevent.OBJECTTYPE_TRAFGEN
address_tuple_alloc = _tbevent.address_tuple_alloc
address_tuple_free = _tbevent.address_tuple_free
EVENT_HOST_ANY = _tbevent.EVENT_HOST_ANY
EVENT_NULL = _tbevent.EVENT_NULL
EVENT_TEST = _tbevent.EVENT_TEST
EVENT_SCHEDULE = _tbevent.EVENT_SCHEDULE
EVENT_TRAFGEN_START = _tbevent.EVENT_TRAFGEN_START
EVENT_TRAFGEN_STOP = _tbevent.EVENT_TRAFGEN_STOP
event_register = _tbevent.event_register
event_register_withkeyfile = _tbevent.event_register_withkeyfile
event_register_withkeydata = _tbevent.event_register_withkeydata
171 172
event_register_withkeyfile_withretry = _tbevent.event_register_withkeyfile_withretry
event_register_withkeydata_withretry = _tbevent.event_register_withkeydata_withretry
173 174 175 176
event_unregister = _tbevent.event_unregister
c_event_poll = _tbevent.c_event_poll
c_event_poll_blocking = _tbevent.c_event_poll_blocking
dont_use_this_function_because_it_does_not_work = _tbevent.dont_use_this_function_because_it_does_not_work
177
event_stop_main = _tbevent.event_stop_main
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
event_notify = _tbevent.event_notify
event_schedule = _tbevent.event_schedule
event_notification_alloc = _tbevent.event_notification_alloc
event_notification_free = _tbevent.event_notification_free
event_notification_clone = _tbevent.event_notification_clone
event_notification_get_double = _tbevent.event_notification_get_double
event_notification_get_int32 = _tbevent.event_notification_get_int32
event_notification_get_int64 = _tbevent.event_notification_get_int64
event_notification_get_opaque = _tbevent.event_notification_get_opaque
c_event_notification_get_string = _tbevent.c_event_notification_get_string
event_notification_put_double = _tbevent.event_notification_put_double
event_notification_put_int32 = _tbevent.event_notification_put_int32
event_notification_put_int64 = _tbevent.event_notification_put_int64
event_notification_put_opaque = _tbevent.event_notification_put_opaque
event_notification_put_string = _tbevent.event_notification_put_string
event_notification_remove = _tbevent.event_notification_remove
c_event_subscribe = _tbevent.c_event_subscribe
195 196 197 198
event_subscribe_auth = _tbevent.event_subscribe_auth
event_async_subscribe = _tbevent.event_async_subscribe
event_unsubscribe = _tbevent.event_unsubscribe
event_async_unsubscribe = _tbevent.event_async_unsubscribe
199
event_notification_insert_hmac = _tbevent.event_notification_insert_hmac
200
event_set_idle_period = _tbevent.event_set_idle_period
201
event_set_failover = _tbevent.event_set_failover
Timothy Stack's avatar
 
Timothy Stack committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
event_arg_get = _tbevent.event_arg_get
event_arg_dup = _tbevent.event_arg_dup
EA_TAG_DONE = _tbevent.EA_TAG_DONE
EA_Site = _tbevent.EA_Site
EA_Experiment = _tbevent.EA_Experiment
EA_Group = _tbevent.EA_Group
EA_Host = _tbevent.EA_Host
EA_Type = _tbevent.EA_Type
EA_Name = _tbevent.EA_Name
EA_Event = _tbevent.EA_Event
EA_Arguments = _tbevent.EA_Arguments
EA_ArgInteger = _tbevent.EA_ArgInteger
EA_ArgFloat = _tbevent.EA_ArgFloat
EA_ArgString = _tbevent.EA_ArgString
EA_When = _tbevent.EA_When
217 218
event_notification_create_v = _tbevent.event_notification_create_v
event_notification_create = _tbevent.event_notification_create
Timothy Stack's avatar
 
Timothy Stack committed
219 220
event_do_v = _tbevent.event_do_v
event_do = _tbevent.event_do
221 222
xmalloc = _tbevent.xmalloc
xrealloc = _tbevent.xrealloc
223
make_timestamp = _tbevent.make_timestamp
224 225 226 227 228
class timeval(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, timeval, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, timeval, name)
229
    __repr__ = _swig_repr
230 231 232 233 234 235
    __swig_setmethods__["tv_sec"] = _tbevent.timeval_tv_sec_set
    __swig_getmethods__["tv_sec"] = _tbevent.timeval_tv_sec_get
    if _newclass:tv_sec = property(_tbevent.timeval_tv_sec_get, _tbevent.timeval_tv_sec_set)
    __swig_setmethods__["tv_usec"] = _tbevent.timeval_tv_usec_set
    __swig_getmethods__["tv_usec"] = _tbevent.timeval_tv_usec_get
    if _newclass:tv_usec = property(_tbevent.timeval_tv_usec_get, _tbevent.timeval_tv_usec_set)
236 237 238 239 240 241 242 243
    def __init__(self, *args): 
        this = _tbevent.new_timeval(*args)
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _tbevent.delete_timeval
    __del__ = lambda self : None;
timeval_swigregister = _tbevent.timeval_swigregister
timeval_swigregister(timeval)
244

245 246 247 248 249
class callback_data(_object):
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, callback_data, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, callback_data, name)
250
    __repr__ = _swig_repr
251 252
    __swig_setmethods__["callback_notification"] = _tbevent.callback_data_callback_notification_set
    __swig_getmethods__["callback_notification"] = _tbevent.callback_data_callback_notification_get
Timothy Stack's avatar
Timothy Stack committed
253
    if _newclass:callback_notification = property(_tbevent.callback_data_callback_notification_get, _tbevent.callback_data_callback_notification_set)
254 255
    __swig_setmethods__["next"] = _tbevent.callback_data_next_set
    __swig_getmethods__["next"] = _tbevent.callback_data_next_get
Timothy Stack's avatar
Timothy Stack committed
256
    if _newclass:next = property(_tbevent.callback_data_next_get, _tbevent.callback_data_next_set)
257 258 259 260 261 262 263 264
    def __init__(self, *args): 
        this = _tbevent.new_callback_data(*args)
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _tbevent.delete_callback_data
    __del__ = lambda self : None;
callback_data_swigregister = _tbevent.callback_data_swigregister
callback_data_swigregister(callback_data)
Timothy Stack's avatar
Timothy Stack committed
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
allocate_callback_data = _tbevent.allocate_callback_data
free_callback_data = _tbevent.free_callback_data
dequeue_callback_data = _tbevent.dequeue_callback_data
enqueue_callback_data = _tbevent.enqueue_callback_data
perl_stub_callback = _tbevent.perl_stub_callback
stub_event_subscribe = _tbevent.stub_event_subscribe
event_notification_get_string = _tbevent.event_notification_get_string
event_notification_get_site = _tbevent.event_notification_get_site
event_notification_get_expt = _tbevent.event_notification_get_expt
event_notification_get_group = _tbevent.event_notification_get_group
event_notification_get_host = _tbevent.event_notification_get_host
event_notification_get_objtype = _tbevent.event_notification_get_objtype
event_notification_get_objname = _tbevent.event_notification_get_objname
event_notification_get_eventtype = _tbevent.event_notification_get_eventtype
event_notification_get_arguments = _tbevent.event_notification_get_arguments
event_notification_set_arguments = _tbevent.event_notification_set_arguments
event_notification_get_sender = _tbevent.event_notification_get_sender
event_notification_set_sender = _tbevent.event_notification_set_sender
284

285 286 287 288 289 290
cvar = _tbevent.cvar

# -*- python -*-
#
# CODE PAST THIS POINT WAS NOT AUTOMATICALLY GENERATED BY SWIG
#
291 292 293 294 295
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
# All rights reserved.
#
296 297 298 299 300
# For now, this has to get cat'ed onto the end of tbevent.py, since it
# doesn't seem possible to get SWIG to just pass it through into the
# output file
#

301
import sys
302 303 304 305 306 307 308 309 310 311 312 313
import time

class NotificationWrapper:
    """
    Wrapper class for event_notification structures.  Mostly just adds setter
    and getter methods.
    """

    def __init__(self, handle, notification):
        """
        Construct a NotificationWrapper that wraps the given objects.
        
Timothy Stack's avatar
Timothy Stack committed
314 315
        @param handle The event_handle used to create notification.
        @param notification The event_notification structure to wrap.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        """
        self.handle = handle
        self.notification = notification
        return

    def __del__(self):
        """
        Deconstruct the object by free'ing the wrapped notification.
        """
        event_notification_free(self.handle, self.notification)
        return

    # For the rest of these, consult the C header file, event.h.

    def getSite(self):
        return event_notification_get_site(self.handle, self.notification)

    def getExpt(self):
        return event_notification_get_expt(self.handle, self.notification)

    def getGroup(self):
        return event_notification_get_group(self.handle, self.notification)

    def getHost(self):
        return event_notification_get_host(self.handle, self.notification)

    def getObjType(self):
        return event_notification_get_objtype(self.handle, self.notification)

    def getObjName(self):
        return event_notification_get_objname(self.handle, self.notification)

    def getEventType(self):
        return event_notification_get_eventtype(self.handle, self.notification)

    def getArguments(self):
        return event_notification_get_arguments(self.handle, self.notification)

    def setArguments(self, args):
        return event_notification_set_arguments(self.handle,
                                                self.notification,
                                                args)

359 360 361
    def getTimeline(self, args):
        return event_notification_get_timeline(self.handle, self.notification)

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    def getSender(self):
        return event_notification_get_sender(self.handle, self.notification)

    def setSender(self, sender):
        return event_notification_set_sender(self.handle,
                                             self.notification,
                                             sender)

    pass


class CallbackIterator:
    """
    Python iterator for the callback list created by the SWIG stubs.
    """

    def __init__(self, handle):
        """
        Construct an iterator with the given arguments.

Timothy Stack's avatar
Timothy Stack committed
382
        @param handle The event_handle being polled.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
        """
        self.last = None
        self.handle = handle
        return

    def __del__(self):
        """
        Deconstruct the iterator.
        """
        if self.last:
            free_callback_data(self.last)
            self.last = None
            pass
        return

    def __iter__(self):
        return self

    def next(self):
        """
        Return the next object in the sequence or raise StopIteration if there
        are no more.  The returned object is a wrapped notification.
        """
        if self.last:
            free_callback_data(self.last)
            self.last = None
            pass
        self.last = dequeue_callback_data()
        if not self.last:
            raise StopIteration
        
        return NotificationWrapper(self.handle,
                                   self.last.callback_notification)

    pass


420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
class EventError:
    """
    Base class for event related exceptions.
    """
    def __init__(self, code):
        self.args = code,
        return
    
    pass


class EventTimedOutError(EventError):
    """
    Exception raised when the run() method of EventClient timed out.
    """
    def __init__(self, timeout):
        self.args = timeout,
        return
    
    pass


# Ugh, elvin likes to crash if we unregister all of the handles, so we keep
# a dummy one around just to keep things happy.
_hack_handle = None

446 447 448 449
class EventClient:
    """
    Event client class, mostly just wraps the SWIG'd versions of the functions.
    """
450
    
Timothy Stack's avatar
 
Timothy Stack committed
451
    def __init__(self, server=None, port=None, url=None, keyfile=None):
452 453 454
        """
        Construct an EventClient object.

Timothy Stack's avatar
Timothy Stack committed
455 456 457
        @param url The server name in url'ish form (e.g. elvin://boss)
        @param server The name of the server.
        @param port The server port number.
458
        """
459 460
        global _hack_handle
        
461 462 463 464 465 466 467 468 469 470
        if url:
            if not url.startswith("elvin:"):
                raise ValueError, "malformed url: " + url
            pass
        else:
            if not server:
                raise ValueError, "url or server must be given"
            url = "elvin://" + server
            if port and len(port) > 0:
                url = url + ":" + port
471
                pass
472
            pass
Timothy Stack's avatar
 
Timothy Stack committed
473 474 475 476 477 478 479 480

        if keyfile:
            self.handle = event_register_withkeyfile(url, 0, keyfile)
            pass
        else:
            self.handle = event_register(url, 0)
            pass
        
481 482 483 484 485 486 487 488 489
        self.timeout = 0
        
        if not _hack_handle:
            # Open a handle for the sole purpose of keeping the event library
            # from calling the elvin cleanup function, because elvin likes to
            # segfault when it has been init'd/clean'd multiple times.
            _hack_handle = event_register(url, 0)
            pass
        
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
        return

    def __del__(self):
        """
        Deconstruct this object by disconnecting from the server.
        """
        event_unregister(self.handle)
        self.handle = None
        return

    def _callbacks(self):
        """
        Return an iterator that traverses the list of callbacks generated by
        the SWIG wrapper.
        """
        return CallbackIterator(self.handle)

    def handle_event(self, ev):
        """
        Default implementation of the event handling method.  Should be
        overridden by subclasses.
511 512

        @return None to continue processing events, any other value to stop.
513
        """
514
        return None
515 516 517 518 519

    def subscribe(self, tuple):
        """
        Subscribe to some events.

Timothy Stack's avatar
Timothy Stack committed
520
        @param tuple The address tuple describing the subscription.
521 522 523 524 525
        """
        return stub_event_subscribe(self.handle, tuple.this)

    def create_notification(self, tuple):
        """
Timothy Stack's avatar
Timothy Stack committed
526
        @return A notification that is bound to this client.
527 528 529 530 531 532 533 534 535 536 537
        """
        return NotificationWrapper(self.handle,
                                   event_notification_alloc(self.handle,
                                                            tuple.this))

    def notify(self, en):
        """
        Send a notification.
        """
        return event_notify(self.handle, en.notification)

538 539 540 541 542 543
    def schedule(self, en, tv):
        """
        Schedule a notification.
        """
        return event_schedule(self.handle, en.notification, tv)

544 545 546 547 548 549 550
    def set_timeout(self, timeout):
        """
        @param timeout The timeout, in seconds, for the run() loop.
        """
        self.timeout = timeout * 1000
        return

551 552 553
    def run(self):
        """
        Main loop used to wait for and process events.
554 555

        @return The not None value returned by handle_event.
556
        """
557 558 559
        retval = None
        while not retval:
            rc = c_event_poll_blocking(self.handle, self.timeout)
560 561
            if rc != 0:
                sys.stderr.write("c_event_poll_blocking: " + str(rc) + "\n")
562
                raise EventError, rc
563 564
            else:
                for ev in self._callbacks():
565 566 567 568 569 570 571 572 573 574 575 576 577 578
                    retval = self.handle_event(ev)
                    if retval:
                        # Not None return value, stop the bus.
                        break
                    pass
                else:
                    if self.timeout != 0:
                        # We're making a bit of an assumption here that no
                        # callbacks means a timeout occurred, oh well.
                        raise EventTimedOutError, self.timeout
                    pass
                
                if not retval:
                    time.sleep(0.1) # Forcefully slow down the poll.
579 580 581
                    pass
                pass
            pass
582
        return retval
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    def poll(self):
        """
        Polling interface; returns events to caller, one at a time.
        """

        while True:
            try:
                #
                # First see if anything not yet delivered.
                #
                ev = CallbackIterator(self.handle).next()
                return ev;
            except StopIteration, e:
                pass
            
            rc = c_event_poll_blocking(self.handle, 0)        
            if rc != 0:
                sys.stderr.write("c_event_poll_blocking: " + str(rc) + "\n")
                raise IOError, "Reading events"
            pass
604 605
    pass