blob: 9e2b3849bed2d73e4f00f1ae4683b7a6f6ef7c50 [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,
36 socket.AF_UNSPEC,
37 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
David Reissc16a8f62007-12-14 23:46:47 +000050 def __init__(self, host='localhost', port=9090, unix_socket=None):
51 """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.)
57 """
Mark Sleecde2b612006-09-03 21:13:07 +000058 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000059 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000060 self.handle = None
David Reissc16a8f62007-12-14 23:46:47 +000061 self._unix_socket = unix_socket
62 self._timeout = None
David Reiss0c90f6f2008-02-06 22:18:40 +000063
Mark Slee4f0fed62006-10-02 17:50:08 +000064 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000065 self.handle = h
66
Mark Sleecde2b612006-09-03 21:13:07 +000067 def isOpen(self):
Bryan Duxbury50409112011-03-21 17:59:49 +000068 return self.handle is not None
Aditya Agarwalf954f972007-02-06 01:26:12 +000069
70 def setTimeout(self, ms):
David Reissc16a8f62007-12-14 23:46:47 +000071 if ms is None:
72 self._timeout = None
James Wange168d5e2007-07-24 23:59:51 +000073 else:
Bryan Duxbury69720412012-01-03 17:32:30 +000074 self._timeout = ms / 1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +000075
Bryan Duxbury50409112011-03-21 17:59:49 +000076 if self.handle is not None:
David Reissc16a8f62007-12-14 23:46:47 +000077 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +000078
79 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000080 try:
David Reissc16a8f62007-12-14 23:46:47 +000081 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +000082 for res in res0:
83 self.handle = socket.socket(res[0], res[1])
David Reissc16a8f62007-12-14 23:46:47 +000084 self.handle.settimeout(self._timeout)
Mark Slee22974602007-07-06 22:20:19 +000085 try:
86 self.handle.connect(res[4])
87 except socket.error, e:
88 if res is not res0[-1]:
89 continue
90 else:
91 raise e
92 break
Mark Slee92195ae2007-02-21 05:16:30 +000093 except socket.error, e:
David Reisse29995e2008-07-31 20:15:17 +000094 if self._unix_socket:
95 message = 'Could not connect to socket %s' % self._unix_socket
96 else:
97 message = 'Could not connect to %s:%d' % (self.host, self.port)
Bryan Duxbury69720412012-01-03 17:32:30 +000098 raise TTransportException(type=TTransportException.NOT_OPEN,
99 message=message)
Mark Sleecde2b612006-09-03 21:13:07 +0000100
Mark Sleecde2b612006-09-03 21:13:07 +0000101 def read(self, sz):
David Reiss73af3b72010-08-30 21:57:07 +0000102 try:
103 buff = self.handle.recv(sz)
104 except socket.error, e:
105 if (e.args[0] == errno.ECONNRESET and
106 (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))):
107 # freebsd and Mach don't follow POSIX semantic of recv
108 # and fail with ECONNRESET if peer performed shutdown.
109 # See corresponding comment and code in TSocket::read()
110 # in lib/cpp/src/transport/TSocket.cpp.
111 self.close()
112 # Trigger the check to raise the END_OF_FILE exception below.
113 buff = ''
114 else:
115 raise
Mark Sleec9676562006-09-05 17:34:52 +0000116 if len(buff) == 0:
Bryan Duxbury69720412012-01-03 17:32:30 +0000117 raise TTransportException(type=TTransportException.END_OF_FILE,
118 message='TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +0000119 return buff
120
121 def write(self, buff):
David Reissa043be32009-05-12 02:17:43 +0000122 if not self.handle:
Bryan Duxbury69720412012-01-03 17:32:30 +0000123 raise TTransportException(type=TTransportException.NOT_OPEN,
124 message='Transport not open')
Mark Sleec9676562006-09-05 17:34:52 +0000125 sent = 0
126 have = len(buff)
127 while sent < have:
128 plus = self.handle.send(buff)
129 if plus == 0:
Bryan Duxbury69720412012-01-03 17:32:30 +0000130 raise TTransportException(type=TTransportException.END_OF_FILE,
131 message='TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +0000132 sent += plus
133 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +0000134
135 def flush(self):
136 pass
Mark Sleec9676562006-09-05 17:34:52 +0000137
Bryan Duxbury69720412012-01-03 17:32:30 +0000138
David Reissd73255d2009-03-24 22:51:02 +0000139class TServerSocket(TSocketBase, TServerTransportBase):
Mark Sleec9676562006-09-05 17:34:52 +0000140 """Socket implementation of TServerTransport base."""
141
Bryan Duxburye0498c92011-01-26 17:54:02 +0000142 def __init__(self, host=None, port=9090, unix_socket=None):
143 self.host = host
Mark Sleec9676562006-09-05 17:34:52 +0000144 self.port = port
David Reisse29995e2008-07-31 20:15:17 +0000145 self._unix_socket = unix_socket
Mark Sleec9676562006-09-05 17:34:52 +0000146 self.handle = None
Mark Slee256bdc42007-11-27 08:42:19 +0000147
Mark Sleec9676562006-09-05 17:34:52 +0000148 def listen(self):
David Reisse29995e2008-07-31 20:15:17 +0000149 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +0000150 for res in res0:
151 if res[0] is socket.AF_INET6 or res is res0[-1]:
152 break
153
David Reisse29995e2008-07-31 20:15:17 +0000154 # We need remove the old unix socket if the file exists and
155 # nobody is listening on it.
156 if self._unix_socket:
157 tmp = socket.socket(res[0], res[1])
158 try:
159 tmp.connect(res[4])
160 except socket.error, err:
161 eno, message = err.args
162 if eno == errno.ECONNREFUSED:
163 os.unlink(res[4])
164
Mark Slee22974602007-07-06 22:20:19 +0000165 self.handle = socket.socket(res[0], res[1])
Mark Slee4f0fed62006-10-02 17:50:08 +0000166 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Jake Farrell1290d422011-10-26 12:36:17 +0000167 if hasattr(self.handle, 'settimeout'):
168 self.handle.settimeout(None)
Mark Slee22974602007-07-06 22:20:19 +0000169 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +0000170 self.handle.listen(128)
171
172 def accept(self):
David Reisse29995e2008-07-31 20:15:17 +0000173 client, addr = self.handle.accept()
Mark Sleec9676562006-09-05 17:34:52 +0000174 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +0000175 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +0000176 return result