1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import os |
|
|
|
|
9
|
|
|
import sys |
|
|
|
|
10
|
|
|
import traceback |
|
|
|
|
11
|
|
|
|
12
|
|
|
import zmq |
13
|
|
|
|
14
|
|
|
import enarksh |
15
|
|
|
from enarksh.controller.message.NodeActionMessage import NodeActionMessage |
16
|
|
|
from enarksh.controller.message.ScheduleDefinitionMessage import ScheduleDefinitionMessage |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class NodeActionClient: |
20
|
|
|
""" |
21
|
|
|
A client for requesting the controller for a node action. |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
25
|
|
|
def __init__(self): |
26
|
|
|
self._zmq_context = None |
27
|
|
|
""" |
28
|
|
|
The ZMQ context. |
29
|
|
|
|
30
|
|
|
:type: Context |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
self._zmq_controller = None |
34
|
|
|
""" |
35
|
|
|
The socket for communicating with the controller. |
36
|
|
|
|
37
|
|
|
:type: zmq.sugar.socket.Socket |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
41
|
|
|
def main(self, uri, act_id): |
42
|
|
|
""" |
43
|
|
|
The main function of node_action. |
44
|
|
|
|
45
|
|
|
:param str uri: The URI of the (trigger) node that must be triggered. |
46
|
|
|
:param int act_id: The ID of the requested action. |
47
|
|
|
""" |
48
|
|
|
# Initialize ZMQ. |
49
|
|
|
self._zmq_init() |
50
|
|
|
|
51
|
|
|
# Compose the message for the controller. |
52
|
|
|
message = NodeActionMessage(uri, act_id, False, False) |
53
|
|
|
|
54
|
|
|
# Send the message to the controller. |
55
|
|
|
self._zmq_controller.send_pyobj(message) |
56
|
|
|
|
57
|
|
|
# Await the response from the controller. |
58
|
|
|
response = self._zmq_controller.recv_pyobj() |
59
|
|
|
|
60
|
|
|
print(response['message'], end='') |
61
|
|
|
if response['message'] and response['message'][-1:] != '\n': |
62
|
|
|
print() |
63
|
|
|
|
64
|
|
|
print(response) |
65
|
|
|
|
66
|
|
|
return response['ret'] |
67
|
|
|
|
68
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
69
|
|
|
def _zmq_init(self): |
|
|
|
|
70
|
|
|
self._zmq_context = zmq.Context() |
71
|
|
|
|
72
|
|
|
# Create socket for communicating with the controller. |
73
|
|
|
self._zmq_controller = self._zmq_context.socket(zmq.REQ) |
|
|
|
|
74
|
|
|
self._zmq_controller.connect(enarksh.CONTROLLER_LOCKSTEP_END_POINT) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
78
|
|
|
|