-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzmqapp.py
65 lines (53 loc) · 2.27 KB
/
zmqapp.py
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
#This file is part of the Kivy Cinema Kiosk Demo.
# Copyright (C) 2010 by
# Thomas Hansen <[email protected]>
# Mathieu Virbel <[email protected]>
#
# The Kivy Cinema Kiosk Demo 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 3 of the License, or
# (at your option) any later version.
#
# The Kivy Cinema Kiosk Demo 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 The Kivy Cinema Kiosk Demo. If not, see <http://www.gnu.org/licenses/>.
import kivy, zmq, inspect
kivy.require('1.0.4')
from kivy.app import App
from kivy.clock import Clock
class ZmqControlledApp(App):
def __init__(self, **kwargs):
super(ZmqControlledApp, self).__init__(**kwargs)
#default port 5489 (kivy)
self.zmq_controller_endp = kwargs.get('zmq_controller_endp', 'tcp://127.0.0.1:5489')
self._zmq_context = zmq.Context()
self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL)
self._zmq_pull_socket.connect(self.zmq_controller_endp)
Clock.schedule_interval(self.pull_zmq_message, 0.1)
def pull_zmq_message(self, *args):
try:
msg = self._zmq_pull_socket.recv_json(zmq.NOBLOCK)
self.process_zmq_message(msg)
except zmq.core.error.ZMQError:
pass
def process_zmq_message(self, msg):
print "received", msg
#methods = dict(inspect.getmembers(self , inspect.ismethod))
#methods[msg]()
class ZmqAppController(object):
def __init__(self, endpoint='tcp://127.0.0.1:5489'):
self.endpoint = endpoint
self._zmq_context = zmq.Context()
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
self._zmq_push_socket.bind(endpoint)
def send(self, msg):
self._zmq_push_socket.send_json(msg)
if __name__ == "__main__":
ctrl = ZmqAppController()
while True:
cmd = input("send message:")
ctrl.send(cmd)