blob: 5e58825f27a994f17766d4a4ce6a1a4622a80413 [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
24
David Reisse29995e2008-07-31 20:15:17 +000025class 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 Sleecde2b612006-09-03 21:13:07 +000031
David Reisse29995e2008-07-31 20:15:17 +000032 def close(self):
33 if self.handle:
34 self.handle.close()
35 self.handle = None
36
37class TSocket(TSocketBase):
Mark Sleecde2b612006-09-03 21:13:07 +000038 """Socket implementation of TTransport base."""
39
David Reissc16a8f62007-12-14 23:46:47 +000040 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 Sleecde2b612006-09-03 21:13:07 +000049 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000050 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000051 self.handle = None
David Reissc16a8f62007-12-14 23:46:47 +000052 self._unix_socket = unix_socket
53 self._timeout = None
David Reiss0c90f6f2008-02-06 22:18:40 +000054
Mark Slee4f0fed62006-10-02 17:50:08 +000055 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000056 self.handle = h
57
Mark Sleecde2b612006-09-03 21:13:07 +000058 def isOpen(self):
Aditya Agarwalf954f972007-02-06 01:26:12 +000059 return self.handle != None
60
61 def setTimeout(self, ms):
David Reissc16a8f62007-12-14 23:46:47 +000062 if ms is None:
63 self._timeout = None
James Wange168d5e2007-07-24 23:59:51 +000064 else:
David Reissc16a8f62007-12-14 23:46:47 +000065 self._timeout = ms/1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +000066
David Reissc16a8f62007-12-14 23:46:47 +000067 if (self.handle != None):
68 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +000069
70 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000071 try:
David Reissc16a8f62007-12-14 23:46:47 +000072 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +000073 for res in res0:
74 self.handle = socket.socket(res[0], res[1])
David Reissc16a8f62007-12-14 23:46:47 +000075 self.handle.settimeout(self._timeout)
Mark Slee22974602007-07-06 22:20:19 +000076 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 Slee92195ae2007-02-21 05:16:30 +000084 except socket.error, e:
David Reisse29995e2008-07-31 20:15:17 +000085 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 Sleecde2b612006-09-03 21:13:07 +000090
Mark Sleecde2b612006-09-03 21:13:07 +000091 def read(self, sz):
92 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000093 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000094 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000095 return buff
96
97 def write(self, buff):
David Reissa043be32009-05-12 02:17:43 +000098 if not self.handle:
99 raise TTransportException(TTransportException.NOT_OPEN, 'Transport not open')
Mark Sleec9676562006-09-05 17:34:52 +0000100 sent = 0
101 have = len(buff)
102 while sent < have:
103 plus = self.handle.send(buff)
104 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +0000105 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +0000106 sent += plus
107 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +0000108
109 def flush(self):
110 pass
Mark Sleec9676562006-09-05 17:34:52 +0000111
David Reissd73255d2009-03-24 22:51:02 +0000112class TServerSocket(TSocketBase, TServerTransportBase):
Mark Sleec9676562006-09-05 17:34:52 +0000113 """Socket implementation of TServerTransport base."""
114
David Reisse29995e2008-07-31 20:15:17 +0000115 def __init__(self, port=9090, unix_socket=None):
116 self.host = None
Mark Sleec9676562006-09-05 17:34:52 +0000117 self.port = port
David Reisse29995e2008-07-31 20:15:17 +0000118 self._unix_socket = unix_socket
Mark Sleec9676562006-09-05 17:34:52 +0000119 self.handle = None
Mark Slee256bdc42007-11-27 08:42:19 +0000120
Mark Sleec9676562006-09-05 17:34:52 +0000121 def listen(self):
David Reisse29995e2008-07-31 20:15:17 +0000122 res0 = self._resolveAddr()
Mark Slee22974602007-07-06 22:20:19 +0000123 for res in res0:
124 if res[0] is socket.AF_INET6 or res is res0[-1]:
125 break
126
David Reisse29995e2008-07-31 20:15:17 +0000127 # 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 Slee22974602007-07-06 22:20:19 +0000138 self.handle = socket.socket(res[0], res[1])
Mark Slee4f0fed62006-10-02 17:50:08 +0000139 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
140 if hasattr(self.handle, 'set_timeout'):
141 self.handle.set_timeout(None)
Mark Slee22974602007-07-06 22:20:19 +0000142 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +0000143 self.handle.listen(128)
144
145 def accept(self):
David Reisse29995e2008-07-31 20:15:17 +0000146 client, addr = self.handle.accept()
Mark Sleec9676562006-09-05 17:34:52 +0000147 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +0000148 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +0000149 return result