blob: 085a5eef09b668d663bde6f81f2148c9a50371d5 [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
Mark Sleecde2b612006-09-03 21:13:07 +000020from TTransport import *
David Reisse29995e2008-07-31 20:15:17 +000021import os
22import errno
Mark Sleecde2b612006-09-03 21:13:07 +000023import socket
David Reiss73af3b72010-08-30 21:57:07 +000024import sys
Mark Sleecde2b612006-09-03 21:13:07 +000025
David Reisse29995e2008-07-31 20:15:17 +000026class 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 Sleecde2b612006-09-03 21:13:07 +000032
David Reisse29995e2008-07-31 20:15:17 +000033 def close(self):
34 if self.handle:
35 self.handle.close()
36 self.handle = None
37
38class TSocket(TSocketBase):
Mark Sleecde2b612006-09-03 21:13:07 +000039 """Socket implementation of TTransport base."""
40
David Reissc16a8f62007-12-14 23:46:47 +000041 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 Sleecde2b612006-09-03 21:13:07 +000050 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000051 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000052 self.handle = None
David Reissc16a8f62007-12-14 23:46:47 +000053 self._unix_socket = unix_socket
54 self._timeout = None
David Reiss0c90f6f2008-02-06 22:18:40 +000055
Mark Slee4f0fed62006-10-02 17:50:08 +000056 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000057 self.handle = h
58
Mark Sleecde2b612006-09-03 21:13:07 +000059 def isOpen(self):
Aditya Agarwalf954f972007-02-06 01:26:12 +000060 return self.handle != None
61
62 def setTimeout(self, ms):
David Reissc16a8f62007-12-14 23:46:47 +000063 if ms is None:
64 self._timeout = None
James Wange168d5e2007-07-24 23:59:51 +000065 else:
David Reissc16a8f62007-12-14 23:46:47 +000066 self._timeout = ms/1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +000067
David Reissc16a8f62007-12-14 23:46:47 +000068 if (self.handle != None):
69 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +000070
71 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000072 try:
David Reissc16a8f62007-12-14 23:46:47 +000073 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +000074 for res in res0:
75 self.handle = socket.socket(res[0], res[1])
David Reissc16a8f62007-12-14 23:46:47 +000076 self.handle.settimeout(self._timeout)
Mark Slee22974602007-07-06 22:20:19 +000077 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 Slee92195ae2007-02-21 05:16:30 +000085 except socket.error, e:
David Reisse29995e2008-07-31 20:15:17 +000086 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 Fernandezbff2a352009-09-24 10:22:00 +000090 raise TTransportException(type=TTransportException.NOT_OPEN, message=message)
Mark Sleecde2b612006-09-03 21:13:07 +000091
Mark Sleecde2b612006-09-03 21:13:07 +000092 def read(self, sz):
David Reiss73af3b72010-08-30 21:57:07 +000093 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 Sleec9676562006-09-05 17:34:52 +0000107 if len(buff) == 0:
Esteve Fernandezbff2a352009-09-24 10:22:00 +0000108 raise TTransportException(type=TTransportException.END_OF_FILE, message='TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +0000109 return buff
110
111 def write(self, buff):
David Reissa043be32009-05-12 02:17:43 +0000112 if not self.handle:
Esteve Fernandezbff2a352009-09-24 10:22:00 +0000113 raise TTransportException(type=TTransportException.NOT_OPEN, message='Transport not open')
Mark Sleec9676562006-09-05 17:34:52 +0000114 sent = 0
115 have = len(buff)
116 while sent < have:
117 plus = self.handle.send(buff)
118 if plus == 0:
Esteve Fernandezbff2a352009-09-24 10:22:00 +0000119 raise TTransportException(type=TTransportException.END_OF_FILE, message='TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +0000120 sent += plus
121 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +0000122
123 def flush(self):
124 pass
Mark Sleec9676562006-09-05 17:34:52 +0000125
David Reissd73255d2009-03-24 22:51:02 +0000126class TServerSocket(TSocketBase, TServerTransportBase):
Mark Sleec9676562006-09-05 17:34:52 +0000127 """Socket implementation of TServerTransport base."""
128
Bryan Duxburye0498c92011-01-26 17:54:02 +0000129 def __init__(self, host=None, port=9090, unix_socket=None):
130 self.host = host
Mark Sleec9676562006-09-05 17:34:52 +0000131 self.port = port
David Reisse29995e2008-07-31 20:15:17 +0000132 self._unix_socket = unix_socket
Mark Sleec9676562006-09-05 17:34:52 +0000133 self.handle = None
Mark Slee256bdc42007-11-27 08:42:19 +0000134
Mark Sleec9676562006-09-05 17:34:52 +0000135 def listen(self):
David Reisse29995e2008-07-31 20:15:17 +0000136 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +0000137 for res in res0:
138 if res[0] is socket.AF_INET6 or res is res0[-1]:
139 break
140
David Reisse29995e2008-07-31 20:15:17 +0000141 # 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 Slee22974602007-07-06 22:20:19 +0000152 self.handle = socket.socket(res[0], res[1])
Mark Slee4f0fed62006-10-02 17:50:08 +0000153 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
154 if hasattr(self.handle, 'set_timeout'):
155 self.handle.set_timeout(None)
Mark Slee22974602007-07-06 22:20:19 +0000156 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +0000157 self.handle.listen(128)
158
159 def accept(self):
David Reisse29995e2008-07-31 20:15:17 +0000160 client, addr = self.handle.accept()
Mark Sleec9676562006-09-05 17:34:52 +0000161 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +0000162 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +0000163 return result