blob: 7b564aa53c557152d6450b6e2237226614e30673 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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 Slee89e2bb82007-03-01 00:20:36 +000019
David Reisse29995e2008-07-31 20:15:17 +000020import errno
Bryan Duxbury69720412012-01-03 17:32:30 +000021import os
Mark Sleecde2b612006-09-03 21:13:07 +000022import socket
David Reiss73af3b72010-08-30 21:57:07 +000023import sys
Mark Sleecde2b612006-09-03 21:13:07 +000024
Bryan Duxbury69720412012-01-03 17:32:30 +000025from TTransport import *
26
27
David Reisse29995e2008-07-31 20:15:17 +000028class TSocketBase(TTransportBase):
29 def _resolveAddr(self):
30 if self._unix_socket is not None:
Bryan Duxbury69720412012-01-03 17:32:30 +000031 return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None,
32 self._unix_socket)]
David Reisse29995e2008-07-31 20:15:17 +000033 else:
Bryan Duxbury69720412012-01-03 17:32:30 +000034 return socket.getaddrinfo(self.host,
35 self.port,
jfarrell8bdff942013-11-25 21:20:28 -050036 self._socket_family,
Bryan Duxbury69720412012-01-03 17:32:30 +000037 socket.SOCK_STREAM,
38 0,
39 socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
Mark Sleecde2b612006-09-03 21:13:07 +000040
David Reisse29995e2008-07-31 20:15:17 +000041 def close(self):
42 if self.handle:
43 self.handle.close()
44 self.handle = None
45
Bryan Duxbury69720412012-01-03 17:32:30 +000046
David Reisse29995e2008-07-31 20:15:17 +000047class TSocket(TSocketBase):
Mark Sleecde2b612006-09-03 21:13:07 +000048 """Socket implementation of TTransport base."""
49
jfarrell3979b862013-11-25 14:47:16 -050050 def __init__(self, host='localhost', port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC):
David Reissc16a8f62007-12-14 23:46:47 +000051 """Initialize a TSocket
52
53 @param host(str) The host to connect to.
54 @param port(int) The (TCP) port to connect to.
55 @param unix_socket(str) The filename of a unix socket to connect to.
56 (host and port will be ignored.)
jfarrell3979b862013-11-25 14:47:16 -050057 @param socket_family(int) The socket family to use with this socket.
David Reissc16a8f62007-12-14 23:46:47 +000058 """
Mark Sleecde2b612006-09-03 21:13:07 +000059 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000060 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000061 self.handle = None
David Reissc16a8f62007-12-14 23:46:47 +000062 self._unix_socket = unix_socket
63 self._timeout = None
jfarrell3979b862013-11-25 14:47:16 -050064 self._socket_family = socket_family
David Reiss0c90f6f2008-02-06 22:18:40 +000065
Mark Slee4f0fed62006-10-02 17:50:08 +000066 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000067 self.handle = h
68
Mark Sleecde2b612006-09-03 21:13:07 +000069 def isOpen(self):
Bryan Duxbury50409112011-03-21 17:59:49 +000070 return self.handle is not None
Aditya Agarwalf954f972007-02-06 01:26:12 +000071
72 def setTimeout(self, ms):
David Reissc16a8f62007-12-14 23:46:47 +000073 if ms is None:
74 self._timeout = None
James Wange168d5e2007-07-24 23:59:51 +000075 else:
Bryan Duxbury69720412012-01-03 17:32:30 +000076 self._timeout = ms / 1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +000077
Bryan Duxbury50409112011-03-21 17:59:49 +000078 if self.handle is not None:
David Reissc16a8f62007-12-14 23:46:47 +000079 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +000080
81 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000082 try:
David Reissc16a8f62007-12-14 23:46:47 +000083 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +000084 for res in res0:
85 self.handle = socket.socket(res[0], res[1])
David Reissc16a8f62007-12-14 23:46:47 +000086 self.handle.settimeout(self._timeout)
Mark Slee22974602007-07-06 22:20:19 +000087 try:
88 self.handle.connect(res[4])
jfarrelld565e2f2015-03-18 21:02:47 -040089 except socket.error as e:
Mark Slee22974602007-07-06 22:20:19 +000090 if res is not res0[-1]:
91 continue
92 else:
93 raise e
94 break
jfarrelld565e2f2015-03-18 21:02:47 -040095 except socket.error as e:
David Reisse29995e2008-07-31 20:15:17 +000096 if self._unix_socket:
97 message = 'Could not connect to socket %s' % self._unix_socket
98 else:
99 message = 'Could not connect to %s:%d' % (self.host, self.port)
Bryan Duxbury69720412012-01-03 17:32:30 +0000100 raise TTransportException(type=TTransportException.NOT_OPEN,
101 message=message)
Mark Sleecde2b612006-09-03 21:13:07 +0000102
Mark Sleecde2b612006-09-03 21:13:07 +0000103 def read(self, sz):
David Reiss73af3b72010-08-30 21:57:07 +0000104 try:
105 buff = self.handle.recv(sz)
jfarrelld565e2f2015-03-18 21:02:47 -0400106 except socket.error as e:
David Reiss73af3b72010-08-30 21:57:07 +0000107 if (e.args[0] == errno.ECONNRESET and
108 (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))):
109 # freebsd and Mach don't follow POSIX semantic of recv
110 # and fail with ECONNRESET if peer performed shutdown.
111 # See corresponding comment and code in TSocket::read()
112 # in lib/cpp/src/transport/TSocket.cpp.
113 self.close()
114 # Trigger the check to raise the END_OF_FILE exception below.
115 buff = ''
116 else:
117 raise
Mark Sleec9676562006-09-05 17:34:52 +0000118 if len(buff) == 0:
Bryan Duxbury69720412012-01-03 17:32:30 +0000119 raise TTransportException(type=TTransportException.END_OF_FILE,
120 message='TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +0000121 return buff
122
123 def write(self, buff):
David Reissa043be32009-05-12 02:17:43 +0000124 if not self.handle:
Bryan Duxbury69720412012-01-03 17:32:30 +0000125 raise TTransportException(type=TTransportException.NOT_OPEN,
126 message='Transport not open')
Mark Sleec9676562006-09-05 17:34:52 +0000127 sent = 0
128 have = len(buff)
129 while sent < have:
130 plus = self.handle.send(buff)
131 if plus == 0:
Bryan Duxbury69720412012-01-03 17:32:30 +0000132 raise TTransportException(type=TTransportException.END_OF_FILE,
133 message='TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +0000134 sent += plus
135 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +0000136
137 def flush(self):
138 pass
Mark Sleec9676562006-09-05 17:34:52 +0000139
Bryan Duxbury69720412012-01-03 17:32:30 +0000140
David Reissd73255d2009-03-24 22:51:02 +0000141class TServerSocket(TSocketBase, TServerTransportBase):
Mark Sleec9676562006-09-05 17:34:52 +0000142 """Socket implementation of TServerTransport base."""
143
jfarrell3979b862013-11-25 14:47:16 -0500144 def __init__(self, host=None, port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC):
Bryan Duxburye0498c92011-01-26 17:54:02 +0000145 self.host = host
Mark Sleec9676562006-09-05 17:34:52 +0000146 self.port = port
David Reisse29995e2008-07-31 20:15:17 +0000147 self._unix_socket = unix_socket
jfarrell3979b862013-11-25 14:47:16 -0500148 self._socket_family = socket_family
Mark Sleec9676562006-09-05 17:34:52 +0000149 self.handle = None
Mark Slee256bdc42007-11-27 08:42:19 +0000150
Mark Sleec9676562006-09-05 17:34:52 +0000151 def listen(self):
David Reisse29995e2008-07-31 20:15:17 +0000152 res0 = self._resolveAddr()
jfarrell3979b862013-11-25 14:47:16 -0500153 socket_family = self._socket_family == socket.AF_UNSPEC and socket.AF_INET6 or self._socket_family
Mark Slee22974602007-07-06 22:20:19 +0000154 for res in res0:
jfarrell3979b862013-11-25 14:47:16 -0500155 if res[0] is socket_family or res is res0[-1]:
Mark Slee22974602007-07-06 22:20:19 +0000156 break
157
David Reisse29995e2008-07-31 20:15:17 +0000158 # We need remove the old unix socket if the file exists and
159 # nobody is listening on it.
160 if self._unix_socket:
161 tmp = socket.socket(res[0], res[1])
162 try:
163 tmp.connect(res[4])
jfarrelld565e2f2015-03-18 21:02:47 -0400164 except socket.error as err:
David Reisse29995e2008-07-31 20:15:17 +0000165 eno, message = err.args
166 if eno == errno.ECONNREFUSED:
167 os.unlink(res[4])
168
Mark Slee22974602007-07-06 22:20:19 +0000169 self.handle = socket.socket(res[0], res[1])
Mark Slee4f0fed62006-10-02 17:50:08 +0000170 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Jake Farrell1290d422011-10-26 12:36:17 +0000171 if hasattr(self.handle, 'settimeout'):
172 self.handle.settimeout(None)
Mark Slee22974602007-07-06 22:20:19 +0000173 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +0000174 self.handle.listen(128)
175
176 def accept(self):
David Reisse29995e2008-07-31 20:15:17 +0000177 client, addr = self.handle.accept()
Mark Sleec9676562006-09-05 17:34:52 +0000178 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +0000179 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +0000180 return result