Completed
Branch master (477316)
by Michael
08:56
created

thrift.transport.TSocket   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 177
Duplicated Lines 81.92 %

Importance

Changes 0
Metric Value
wmc 37
eloc 115
dl 145
loc 177
rs 9.44
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
#
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
#
19
20
import errno
21
import os
22
import socket
23
import sys
24
25
from .TTransport import *
26
27
28
class TSocketBase(TTransportBase):
29
  def _resolveAddr(self):
30
    if self._unix_socket is not None:
31
      return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None,
32
               self._unix_socket)]
33
    else:
34
      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)
40
41
  def close(self):
42
    if self.handle:
43
      self.handle.close()
44
      self.handle = None
45
46
47
class TSocket(TSocketBase):
48
  """Socket implementation of TTransport base."""
49
50
  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
    """
58
    self.host = host
59
    self.port = port
60
    self.handle = None
61
    self._unix_socket = unix_socket
62
    self._timeout = None
63
64
  def setHandle(self, h):
65
    self.handle = h
66
67
  def isOpen(self):
68
    return self.handle is not None
69
70
  def setTimeout(self, ms):
71
    if ms is None:
72
      self._timeout = None
73
    else:
74
      self._timeout = ms / 1000.0
75
76
    if self.handle is not None:
77
      self.handle.settimeout(self._timeout)
78
79
  def open(self):
80
    try:
81
      res0 = self._resolveAddr()
82
      for res in res0:
83
        self.handle = socket.socket(res[0], res[1])
84
        self.handle.settimeout(self._timeout)
85
        try:
86
          self.handle.connect(res[4])
87
        except socket.error as e:
88
          if res is not res0[-1]:
89
            continue
90
          else:
91
            raise e
92
        break
93
    except socket.error as e:
94
      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)
98
      raise TTransportException(type=TTransportException.NOT_OPEN,
99
                                message=message)
100
101
  def read(self, sz):
102
    try:
103
      buff = self.handle.recv(sz)
104
    except socket.error as 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
116
    if len(buff) == 0:
117
      raise TTransportException(type=TTransportException.END_OF_FILE,
118
                                message='TSocket read 0 bytes')
119
    return buff
120
121
  def write(self, buff):
122
    if not self.handle:
123
      raise TTransportException(type=TTransportException.NOT_OPEN,
124
                                message='Transport not open')
125
    sent = 0
126
    have = len(buff)
127
    while sent < have:
128
      plus = self.handle.send(buff)
129
      if plus == 0:
130
        raise TTransportException(type=TTransportException.END_OF_FILE,
131
                                  message='TSocket sent 0 bytes')
132
      sent += plus
133
      buff = buff[plus:]
134
135
  def flush(self):
136
    pass
137
138
139
class TServerSocket(TSocketBase, TServerTransportBase):
140
  """Socket implementation of TServerTransport base."""
141
142
  def __init__(self, host=None, port=9090, unix_socket=None):
143
    self.host = host
144
    self.port = port
145
    self._unix_socket = unix_socket
146
    self.handle = None
147
148
  def listen(self):
149
    res0 = self._resolveAddr()
150
    for res in res0:
151
      if res[0] is socket.AF_INET6 or res is res0[-1]:
152
        break
153
154
    # 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 as err:
161
        eno, message = err.args
162
        if eno == errno.ECONNREFUSED:
163
          os.unlink(res[4])
164
165
    self.handle = socket.socket(res[0], res[1])
166
    self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
167
    if hasattr(self.handle, 'settimeout'):
168
      self.handle.settimeout(None)
169
    self.handle.bind(res[4])
170
    self.handle.listen(128)
171
172
  def accept(self):
173
    client, addr = self.handle.accept()
174
    result = TSocket()
175
    result.setHandle(client)
176
    return result
177