Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 20 | from TTransport import * |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 21 | import os |
| 22 | import errno |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | import socket |
| 24 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 25 | class TSocketBase(TTransportBase): |
| 26 | def _resolveAddr(self): |
| 27 | if self._unix_socket is not None: |
| 28 | return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, self._unix_socket)] |
| 29 | else: |
| 30 | return socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 31 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 32 | def close(self): |
| 33 | if self.handle: |
| 34 | self.handle.close() |
| 35 | self.handle = None |
| 36 | |
| 37 | class TSocket(TSocketBase): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 38 | """Socket implementation of TTransport base.""" |
| 39 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 40 | def __init__(self, host='localhost', port=9090, unix_socket=None): |
| 41 | """Initialize a TSocket |
| 42 | |
| 43 | @param host(str) The host to connect to. |
| 44 | @param port(int) The (TCP) port to connect to. |
| 45 | @param unix_socket(str) The filename of a unix socket to connect to. |
| 46 | (host and port will be ignored.) |
| 47 | """ |
| 48 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 49 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 50 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 51 | self.handle = None |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 52 | self._unix_socket = unix_socket |
| 53 | self._timeout = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 54 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 55 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 56 | self.handle = h |
| 57 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 58 | def isOpen(self): |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 59 | return self.handle != None |
| 60 | |
| 61 | def setTimeout(self, ms): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 62 | if ms is None: |
| 63 | self._timeout = None |
James Wang | e168d5e | 2007-07-24 23:59:51 +0000 | [diff] [blame] | 64 | else: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 65 | self._timeout = ms/1000.0 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 66 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 67 | if (self.handle != None): |
| 68 | self.handle.settimeout(self._timeout) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 69 | |
| 70 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 71 | try: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 72 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 73 | for res in res0: |
| 74 | self.handle = socket.socket(res[0], res[1]) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 75 | self.handle.settimeout(self._timeout) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 76 | try: |
| 77 | self.handle.connect(res[4]) |
| 78 | except socket.error, e: |
| 79 | if res is not res0[-1]: |
| 80 | continue |
| 81 | else: |
| 82 | raise e |
| 83 | break |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 84 | except socket.error, e: |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 85 | if self._unix_socket: |
| 86 | message = 'Could not connect to socket %s' % self._unix_socket |
| 87 | else: |
| 88 | message = 'Could not connect to %s:%d' % (self.host, self.port) |
| 89 | raise TTransportException(TTransportException.NOT_OPEN, message) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 90 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 91 | def read(self, sz): |
| 92 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 93 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 94 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 95 | return buff |
| 96 | |
| 97 | def write(self, buff): |
David Reiss | a043be3 | 2009-05-12 02:17:43 +0000 | [diff] [blame] | 98 | if not self.handle: |
| 99 | raise TTransportException(TTransportException.NOT_OPEN, 'Transport not open') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 100 | sent = 0 |
| 101 | have = len(buff) |
| 102 | while sent < have: |
| 103 | plus = self.handle.send(buff) |
| 104 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 105 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 106 | sent += plus |
| 107 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 108 | |
| 109 | def flush(self): |
| 110 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 111 | |
David Reiss | d73255d | 2009-03-24 22:51:02 +0000 | [diff] [blame] | 112 | class TServerSocket(TSocketBase, TServerTransportBase): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 113 | """Socket implementation of TServerTransport base.""" |
| 114 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 115 | def __init__(self, port=9090, unix_socket=None): |
| 116 | self.host = None |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 117 | self.port = port |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 118 | self._unix_socket = unix_socket |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 119 | self.handle = None |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 120 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 121 | def listen(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 122 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 123 | for res in res0: |
| 124 | if res[0] is socket.AF_INET6 or res is res0[-1]: |
| 125 | break |
| 126 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 127 | # We need remove the old unix socket if the file exists and |
| 128 | # nobody is listening on it. |
| 129 | if self._unix_socket: |
| 130 | tmp = socket.socket(res[0], res[1]) |
| 131 | try: |
| 132 | tmp.connect(res[4]) |
| 133 | except socket.error, err: |
| 134 | eno, message = err.args |
| 135 | if eno == errno.ECONNREFUSED: |
| 136 | os.unlink(res[4]) |
| 137 | |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 138 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 139 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 140 | if hasattr(self.handle, 'set_timeout'): |
| 141 | self.handle.set_timeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 142 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 143 | self.handle.listen(128) |
| 144 | |
| 145 | def accept(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 146 | client, addr = self.handle.accept() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 147 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 148 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 149 | return result |