1
|
|
|
""" |
2
|
|
|
Enarksh |
3
|
|
|
|
4
|
|
|
Copyright 2013-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
import zmq |
9
|
|
|
|
10
|
|
|
import enarksh |
11
|
|
|
from enarksh.controller.message.NodeActionMessage import NodeActionMessage |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class NodeActionClient: |
15
|
|
|
""" |
16
|
|
|
A client for requesting the controller for a node action. |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
20
|
|
|
def __init__(self, io): |
21
|
|
|
""" |
22
|
|
|
Object constructor. |
23
|
|
|
|
24
|
|
|
:param enarksh.style.EnarkshStyle.EnarkshStyle io: The output decorator. |
25
|
|
|
""" |
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
|
|
|
self._io = io |
41
|
|
|
""" |
42
|
|
|
The output decorator. |
43
|
|
|
|
44
|
|
|
:type: enarksh.style.EnarkshStyle.EnarkshStyle |
45
|
|
|
""" |
46
|
|
|
|
47
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
48
|
|
|
def main(self, uri, act_id): |
49
|
|
|
""" |
50
|
|
|
The main function of node_action. |
51
|
|
|
|
52
|
|
|
:param str uri: The URI of the (trigger) node that must be triggered. |
53
|
|
|
:param int act_id: The ID of the requested action. |
54
|
|
|
""" |
55
|
|
|
# Initialize ZMQ. |
56
|
|
|
self._zmq_init() |
57
|
|
|
|
58
|
|
|
# Compose the message for the controller. |
59
|
|
|
message = NodeActionMessage(uri, act_id, False, False) |
60
|
|
|
|
61
|
|
|
# Send the message to the controller. |
62
|
|
|
self._zmq_controller.send_pyobj(message) |
63
|
|
|
|
64
|
|
|
# Await the response from the controller. |
65
|
|
|
response = self._zmq_controller.recv_pyobj() |
66
|
|
|
|
67
|
|
|
if response['ret'] == 0: |
68
|
|
|
self._io.log_verbose(response['message']) |
69
|
|
|
else: |
70
|
|
|
self._io.error(response['message']) |
71
|
|
|
|
72
|
|
|
return response['ret'] |
73
|
|
|
|
74
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
75
|
|
|
def _zmq_init(self): |
76
|
|
|
""" |
77
|
|
|
Initializes ZMQ. |
78
|
|
|
""" |
79
|
|
|
self._zmq_context = zmq.Context() |
80
|
|
|
|
81
|
|
|
# Create socket for communicating with the controller. |
82
|
|
|
self._zmq_controller = self._zmq_context.socket(zmq.REQ) |
|
|
|
|
83
|
|
|
self._zmq_controller.connect(enarksh.CONTROLLER_LOCKSTEP_END_POINT) |
84
|
|
|
|
85
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
86
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.