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