medved/lib/net/Remote.py

52 lines
1.2 KiB
Python
Raw Normal View History

2018-04-02 22:41:10 +00:00
# pylint: disable=E1101
2018-04-02 23:47:50 +00:00
# ^
# fags
# v
2018-04-02 22:41:10 +00:00
import zmq
import threading
from Config import cnf
from lib.net import Message
from lib import Logger
2018-04-02 23:47:50 +00:00
"""This should be reworked to loadable format. Protos may be different, just like listeners"""
2018-04-02 22:41:10 +00:00
class Remote:
2018-04-02 23:47:50 +00:00
"""Used to run remote Actions.. meh"""
2018-04-02 22:41:10 +00:00
def __init__(self, ip, port):
self._send_lock = threading.Lock()
self._ip = ip
self._port = port
self._url = "tcp://%s:%s" % (ip, port)
self._logger = Logger("Remote %s" % self._url)
self._z_ctx = None
self._z_sck = None
def connect(self):
self._z_ctx = zmq.Context()
self._z_sck = self._z_ctx.socket(zmq.REQ)
def run(self, action, data=None):
msg = Message()
msg.set('action', action)
msg.set('data', data)
rep = self._send_msg(msg)
if rep.get('status') == False:
self._logger.info(rep.get('data'))
return rep
def _send_msg(self, msg):
self._logger.debug(msg.data())
self._send_lock.acquire()
self._z_sck.connect(self._url)
self._z_sck.send(msg.dump())
reply = Message.load(self._z_sck.recv())
self._send_lock.release()
self._logger.debug(reply.data())
return reply