33from mmi import send_array , recv_array
44from bmi .api import IBmi
55
6+
67class MMIClient (IBmi ):
7- def __init__ (self , zmq_address ):
8+ def __init__ (self , zmq_address , poll_timeout = 10000 ):
89 """
910 Constructor
1011 """
@@ -15,6 +16,16 @@ def __init__(self, zmq_address):
1516 self .socket = context .socket (zmq .REQ )
1617 self .socket .connect (zmq_address )
1718
19+ self .poll = zmq .Poller ()
20+ self .poll .register (self .socket , zmq .POLLIN )
21+
22+ self .poll_timeout = poll_timeout
23+
24+ def _close_sockets (self ):
25+ self .socket .setsockopt (zmq .LINGER , 0 )
26+ self .socket .close ()
27+ self .poll .unregister (self .socket )
28+
1829 # from here: BMI commands that get translated to MMI.
1930 def initialize (self , configfile = None ):
2031 """
@@ -24,10 +35,11 @@ def initialize(self, configfile=None):
2435 method = "initialize"
2536
2637 A = None
27- metadata = {method : configfile }
38+ metadata = {method : configfile }
2839
2940 send_array (self .socket , A , metadata )
30- A , metadata = recv_array (self .socket )
41+ A , metadata = recv_array (
42+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
3143
3244 def finalize (self ):
3345 """
@@ -37,10 +49,11 @@ def finalize(self):
3749 method = "finalize"
3850
3951 A = None
40- metadata = {method : - 1 }
52+ metadata = {method : - 1 }
4153
4254 send_array (self .socket , A , metadata )
43- A , metadata = recv_array (self .socket )
55+ A , metadata = recv_array (
56+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
4457
4558 def get_var_count (self ):
4659 """
@@ -50,10 +63,11 @@ def get_var_count(self):
5063 method = "get_var_count"
5164
5265 A = None
53- metadata = {method : - 1 }
66+ metadata = {method : - 1 }
5467
5568 send_array (self .socket , A , metadata )
56- A , metadata = recv_array (self .socket )
69+ A , metadata = recv_array (
70+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
5771
5872 return metadata [method ]
5973
@@ -65,10 +79,11 @@ def get_var_name(self, i):
6579 method = "get_var_name"
6680
6781 A = None
68- metadata = {method : i }
82+ metadata = {method : i }
6983
7084 send_array (self .socket , A , metadata )
71- A , metadata = recv_array (self .socket )
85+ A , metadata = recv_array (
86+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
7287
7388 return metadata [method ]
7489
@@ -80,10 +95,11 @@ def get_var_type(self, name):
8095 method = "get_var_type"
8196
8297 A = None
83- metadata = {method : name }
98+ metadata = {method : name }
8499
85100 send_array (self .socket , A , metadata )
86- A , metadata = recv_array (self .socket )
101+ A , metadata = recv_array (
102+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
87103
88104 return metadata [method ]
89105
@@ -95,10 +111,11 @@ def get_var_rank(self, name):
95111 method = "get_var_rank"
96112
97113 A = None
98- metadata = {method : name }
114+ metadata = {method : name }
99115
100116 send_array (self .socket , A , metadata )
101- A , metadata = recv_array (self .socket )
117+ A , metadata = recv_array (
118+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
102119
103120 return metadata [method ]
104121
@@ -110,10 +127,11 @@ def get_var_shape(self, name):
110127 method = "get_var_shape"
111128
112129 A = None
113- metadata = {method : rank }
130+ metadata = {method : name }
114131
115132 send_array (self .socket , A , metadata )
116- A , metadata = recv_array (self .socket )
133+ A , metadata = recv_array (
134+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
117135
118136 return metadata [method ]
119137
@@ -125,10 +143,11 @@ def get_var(self, name):
125143 method = "get_var"
126144
127145 A = None
128- metadata = {method : name }
146+ metadata = {method : name }
129147
130148 send_array (self .socket , A , metadata )
131- A , metadata = recv_array (self .socket )
149+ A , metadata = recv_array (
150+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
132151
133152 return A
134153
@@ -140,10 +159,11 @@ def set_var(self, name, var):
140159 method = "set_var"
141160
142161 A = var
143- metadata = {method : name }
162+ metadata = {method : name }
144163
145164 send_array (self .socket , A , metadata )
146- A , metadata = recv_array (self .socket )
165+ A , metadata = recv_array (
166+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
147167
148168 def get_start_time (self ):
149169 """
@@ -153,10 +173,11 @@ def get_start_time(self):
153173 method = "get_start_time"
154174
155175 A = None
156- metadata = {method : - 1 }
176+ metadata = {method : - 1 }
157177
158178 send_array (self .socket , A , metadata )
159- A , metadata = recv_array (self .socket )
179+ A , metadata = recv_array (
180+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
160181
161182 return metadata [method ]
162183
@@ -168,10 +189,11 @@ def get_end_time(self):
168189 method = "get_end_time"
169190
170191 A = None
171- metadata = {method : - 1 }
192+ metadata = {method : - 1 }
172193
173194 send_array (self .socket , A , metadata )
174- A , metadata = recv_array (self .socket )
195+ A , metadata = recv_array (
196+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
175197
176198 return metadata [method ]
177199
@@ -183,10 +205,11 @@ def get_current_time(self):
183205 method = "get_current_time"
184206
185207 A = None
186- metadata = {method : - 1 }
208+ metadata = {method : - 1 }
187209
188210 send_array (self .socket , A , metadata )
189- A , metadata = recv_array (self .socket )
211+ A , metadata = recv_array (
212+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
190213
191214 return metadata [method ]
192215
@@ -198,10 +221,11 @@ def update(self, dt):
198221 method = "update"
199222
200223 A = None
201- metadata = {method : dt }
224+ metadata = {method : dt }
202225
203226 send_array (self .socket , A , metadata )
204- A , metadata = recv_array (self .socket )
227+ A , metadata = recv_array (
228+ self .socket , poll = self .poll , poll_timeout = self .poll_timeout )
205229
206230 # TODO: Do we really need these two?
207231 def inq_compound (self , name ):
@@ -227,6 +251,6 @@ def remote(self, action):
227251 metadata = {method : action }
228252
229253 send_array (self .socket , A , metadata )
230- A , metadata = recv_array (self .socket )
254+ A , metadata = recv_array (self .socket , poll = self . poll )
231255
232256 return metadata [method ]
0 commit comments