David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | import zmq |
| 20 | from cStringIO import StringIO |
| 21 | from thrift.transport.TTransport import TTransportBase, CReadableTransport |
| 22 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 23 | |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 24 | class TZmqClient(TTransportBase, CReadableTransport): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 25 | def __init__(self, ctx, endpoint, sock_type): |
| 26 | self._sock = ctx.socket(sock_type) |
| 27 | self._endpoint = endpoint |
| 28 | self._wbuf = StringIO() |
| 29 | self._rbuf = StringIO() |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 30 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 31 | def open(self): |
| 32 | self._sock.connect(self._endpoint) |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 33 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 34 | def read(self, size): |
| 35 | ret = self._rbuf.read(size) |
| 36 | if len(ret) != 0: |
| 37 | return ret |
| 38 | self._read_message() |
| 39 | return self._rbuf.read(size) |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | def _read_message(self): |
| 42 | msg = self._sock.recv() |
| 43 | self._rbuf = StringIO(msg) |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 44 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | def write(self, buf): |
| 46 | self._wbuf.write(buf) |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | def flush(self): |
| 49 | msg = self._wbuf.getvalue() |
| 50 | self._wbuf = StringIO() |
| 51 | self._sock.send(msg) |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 52 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | # Implement the CReadableTransport interface. |
| 54 | @property |
| 55 | def cstringio_buf(self): |
| 56 | return self._rbuf |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 57 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 58 | # NOTE: This will probably not actually work. |
| 59 | def cstringio_refill(self, prefix, reqlen): |
| 60 | while len(prefix) < reqlen: |
| 61 | self.read_message() |
| 62 | prefix += self._rbuf.getvalue() |
| 63 | self._rbuf = StringIO(prefix) |
| 64 | return self._rbuf |