mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 11:09:56 +00:00
Added examples.
This commit is contained in:
parent
0a3a7b5a70
commit
2e6c27f665
3 changed files with 149 additions and 0 deletions
44
examples/rpc_async.py
Normal file
44
examples/rpc_async.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
"""
|
||||
SleekXMPP: The Sleek XMPP Library
|
||||
Copyright (C) 2011 Dann Martens
|
||||
This file is part of SleekXMPP.
|
||||
|
||||
See the file LICENSE for copying permission.
|
||||
"""
|
||||
|
||||
from sleekxmpp.plugins.xep_0009.remote import Endpoint, remote, Remote, \
|
||||
ANY_ALL, Future
|
||||
import time
|
||||
|
||||
class Boomerang(Endpoint):
|
||||
|
||||
def FQN(self):
|
||||
return 'boomerang'
|
||||
|
||||
@remote
|
||||
def throw(self):
|
||||
print "Duck!"
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
session = Remote.new_session('kangaroo@xmpp.org/rpc', '*****')
|
||||
|
||||
session.new_handler(ANY_ALL, Boomerang)
|
||||
|
||||
boomerang = session.new_proxy('kangaroo@xmpp.org/rpc', Boomerang)
|
||||
|
||||
callback = Future()
|
||||
|
||||
boomerang.async(callback).throw()
|
||||
|
||||
time.sleep(10)
|
||||
|
||||
session.close()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
53
examples/rpc_client_side.py
Normal file
53
examples/rpc_client_side.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
"""
|
||||
SleekXMPP: The Sleek XMPP Library
|
||||
Copyright (C) 2011 Dann Martens
|
||||
This file is part of SleekXMPP.
|
||||
|
||||
See the file LICENSE for copying permission.
|
||||
"""
|
||||
|
||||
from sleekxmpp.plugins.xep_0009.remote import Endpoint, remote, Remote, \
|
||||
ANY_ALL
|
||||
import threading
|
||||
import time
|
||||
|
||||
class Thermostat(Endpoint):
|
||||
|
||||
def FQN(self):
|
||||
return 'thermostat'
|
||||
|
||||
def __init(self, initial_temperature):
|
||||
self._temperature = initial_temperature
|
||||
self._event = threading.Event()
|
||||
|
||||
@remote
|
||||
def set_temperature(self, temperature):
|
||||
return NotImplemented
|
||||
|
||||
@remote
|
||||
def get_temperature(self):
|
||||
return NotImplemented
|
||||
|
||||
@remote(False)
|
||||
def release(self):
|
||||
return NotImplemented
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
session = Remote.new_session('operator@xmpp.org/rpc', '*****')
|
||||
|
||||
thermostat = session.new_proxy('thermostat@xmpp.org/rpc', Thermostat)
|
||||
|
||||
print("Current temperature is %s" % thermostat.get_temperature())
|
||||
|
||||
thermostat.set_temperature(20)
|
||||
|
||||
time.sleep(10)
|
||||
|
||||
session.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
52
examples/rpc_server_side.py
Normal file
52
examples/rpc_server_side.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
"""
|
||||
SleekXMPP: The Sleek XMPP Library
|
||||
Copyright (C) 2011 Dann Martens
|
||||
This file is part of SleekXMPP.
|
||||
|
||||
See the file LICENSE for copying permission.
|
||||
"""
|
||||
|
||||
from sleekxmpp.plugins.xep_0009.remote import Endpoint, remote, Remote, \
|
||||
ANY_ALL
|
||||
import threading
|
||||
|
||||
class Thermostat(Endpoint):
|
||||
|
||||
def FQN(self):
|
||||
return 'thermostat'
|
||||
|
||||
def __init(self, initial_temperature):
|
||||
self._temperature = initial_temperature
|
||||
self._event = threading.Event()
|
||||
|
||||
@remote
|
||||
def set_temperature(self, temperature):
|
||||
print("Setting temperature to %s" % temperature)
|
||||
self._temperature = temperature
|
||||
|
||||
@remote
|
||||
def get_temperature(self):
|
||||
return self._temperature
|
||||
|
||||
@remote(False)
|
||||
def release(self):
|
||||
self._event.set()
|
||||
|
||||
def wait_for_release(self):
|
||||
self._event.wait()
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
session = Remote.new_session('sleek@xmpp.org/rpc', '*****')
|
||||
|
||||
thermostat = session.new_handler(ANY_ALL, Thermostat, 18)
|
||||
|
||||
thermostat.wait_for_release()
|
||||
|
||||
session.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Reference in a new issue