Szymon Wróblewski avatar Szymon Wróblewski committed 632f345

fixing bugs

Comments (0)

Files changed (2)

 SEND_BUFFER_SIZE = 256 * 1024
 
 
+def create_server(address, peer_count=0, channel_limit=0,
+                  incoming_bandwidth=0, outgoing_bandwidth=0):
+    return Host(address, peer_count, channel_limit,
+                incoming_bandwidth, outgoing_bandwidth)
+
+
+def create_client(address, channel_count=0, data=0,
+                  incoming_bandwidth=0, outgoing_bandwidth=0):
+    host = Host(None, 0, 0, incoming_bandwidth, outgoing_bandwidth)
+    peer = host.connect(address, channel_count, data)
+    return host, peer
+
+
 class Host(object):
     def __init__(self, address=None, peer_count=0, channel_limit=0,
                  incoming_bandwidth=0, outgoing_bandwidth=0):
         self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         if address is not None:
-            address = socket.gethostbyname(address[0]), address[1:]
+            address = socket.gethostbyname(address[0]), address[1]
             s.bind(address)
         self.address = address
         # disable blocking read / write
         self.outgoing_bandwidth = outgoing_bandwidth
         self.bandwidth_throttle_epoch = 0
         self.recalculate_bandwidth_limits = 0
-        self.mtu = self.DEFAULT_MTU
+        self.mtu = DEFAULT_MTU
         self.commands = []
         self.command_count = 0
         self.buffer = bytearray()
     def __del__(self):
         for peer in self.peers:
             peer.reset()
+        self.socket.shutdown(socket.SHUT_RDWR)
         self.socket.close()
 
     def __repr__(self):
         return '%s(%s, %d, %d, %d, %d)' % (
-            self.__name__, self.address, self.peer_count, self.channel_limit,
+            self.__class__.__name__, self.address,
+            self.peer_count, self.channel_limit,
             self.incoming_bandwidth, self.outgoing_bandwidth)
 
     def __str__(self):
-        return '%s %s sent: %d recv: %d' % (
-            self.__name__, self.address,
+        return '%s %s:%d sent: %d recv: %d' % (
+            self.__class__.__name__, self.address[0], self.address[1],
             self.total_sent_packets, self.total_received_packets)
 
     def connect(self, address, channel_count=0, data=0):
-        for peer in self.peers:
+        for i, peer in enumerate(self.peers):
             if peer.state == State.DISCONNECTED:
                 break
-        else:  # No empty slot for new peer
-            return
-        return self.peers.append(Peer(self, address, channel_count, data))
+        else:
+            if len(self.peers) > self.peer_count:
+                # no empty slot for new peer
+                return  # TODO: raise exception
+            else:
+                peer = Peer(self, address, channel_count, data)
+                self.peers.append(peer)
+                return peer
+        # reuse slot
+        self.peers[i] = peer = Peer(self, address, channel_count, data)
+        return peer
-class State(object):
+class State:
     (DISCONNECTED,
     CONNECTING,
     ACKNOWLEDGING_CONNECT,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.