Browse Source

minor improvements in the client

digital 7 years ago
parent
commit
893085538c
1 changed files with 19 additions and 36 deletions
  1. 19 36
      network/__init__.py

+ 19 - 36
network/__init__.py

@@ -243,7 +243,8 @@ class Server(object):
     # set to true when the server shuts down, for instance after a
     # fatal exception
     exit_event = False
-    def __init__(self,
+    def __init__(
+                self,
                 host,
                 port,
                 handler_class,
@@ -423,62 +424,50 @@ class Server(object):
 
 class Client(threading.Thread):
     """docstring for Client"""
-    is_connecting = False
-    is_connected = False
     status = "uninitialized"
-    def __init__(self,
+    exit_event = False
+    def __init__(
+                self,
                 host,
                 port=None,
                 af_family="AF_INET",
                 handle_data_func=None,
                 error_handler=None,
                 block_size=1024,
-                ):
-        self.super_class = super(Client, self)
-        self.super_class.__init__()
+            ):
+        super()__init__()
         self.name = "Client"
-        self.exit_event = False
         self.host = host
         self.port = port
         self.af_family = af_family
         self.block_size = block_size
         self.handle_data_func = handle_data_func
-        self.is_connected = False
         self.error_handler = error_handler
-        # self.socket = self.make_socket()
         self.socket = None
         self.status = "disconnected"
+
     def connect(self):
         self.status = "connecting"
         self.socket = self.make_socket()
-        lclient.info(
-            "connecting to socket '{}' of type {}".format(
-                self.host,
-                self.af_family
-            )
-        )
+        lclient.info("connecting to socket '{}' of type {}"
+            .format(self.host,self.af_family))
         try:
             if self.af_family == "AF_INET":
                 self.socket.connect((self.host, self.port))
             elif self.af_family == "AF_UNIX":
-                if os.path.exists(self.host):
-                    self.socket.connect(self.host)
-                else:
-                    lclient.warn("File not found. Aborting.")
-                    return
-            self.is_connected = True
+                self.socket.connect(self.host)
             self.status = "connected"
             lclient.info("connected")
             return True
         except Exception as e:
             lclient.debug(e, exc_info=True)
             if type(e) is ConnectionRefusedError:
-                lclient.info("failed to connect to socket '{}'".format(self.host))
+                lclient.info(
+                    "failed to connect to socket '{}'".format(self.host))
                 self.disconnect()
                 return False
     def disconnect(self):
         lclient.info("disconnecting from socket '{}'".format(self.host))
-        self.is_connected = False
         self.status = "disconnected"
         if self.socket:
             try:
@@ -530,7 +519,6 @@ class Client(threading.Thread):
                 1
             )
             if self.socket in exc_confirmed:
-                self.is_connected = False
                 lclient.warning("socket is expected to corrupt, exiting")
                 self.disconnect()
                # self.stop()
@@ -554,7 +542,6 @@ class Client(threading.Thread):
                 except Exception as e:
                     lclient.error(e, exc_info=True)
                     if type(e) is OSError:
-                        self.is_connected = False
                         lclient.warn("connection broken, exiting")
                         self.disconnect()
                         # self.stop()
@@ -579,7 +566,6 @@ class Client(threading.Thread):
             lcchat.info("Client: "+msg)
             self.socket.send(msg_encoded)
         except Exception as e:
-            self.is_connected = False
             lclient.error(e, exc_info=True)
             self.status = "shutdown"
     def setup(self):
@@ -598,27 +584,24 @@ class Client(threading.Thread):
 
 class AsyncClient(object):
     """docstring for Client"""
-    is_connecting = False
-    is_connected = False
+    block_size = 1024
     status = "uninitialized"
-    def __init__(self,
+    exit_event = False
+    def __init__(
+                self,
                 host,
                 port=None,
                 af_family="AF_INET",
                 handle_data_func=None,
                 error_handler=None,
                 block_size=1024,
-                ):
-        self.super_class = super(AsyncClient, self)
-        self.super_class.__init__()
-        self.name = "Client"
-        self.exit_event = False
+            ):
+        super().__init__()
         self.host = host
         self.port = port
         self.af_family = af_family
         self.block_size = block_size
         self.handle_data_func = handle_data_func
-        self.is_connected = False
         self.error_handler = error_handler
         self.socket = None
         self.status = "disconnected"