Changeset 442

Show
Ignore:
Timestamp:
05/07/08 15:26:10 (8 months ago)
Author:
mgoldman
Message:

Clarifications and modifications per PeBenito?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/trunk-pmd-intproto/network/server.py

    r427 r442  
    2020                client_sock and a (host,port) structure for actions that take place 
    2121                after an accept() """ 
    22         def __init__(self, host='', port=50000, backlog=5, accept_fun=null_op,  
     22        def __init__(self, host='', port=50000, backlog=5, accept_func=null_op,  
    2323                          info2key=info2ip): 
    2424                self.host = host 
     
    2626                self.backlog = backlog 
    2727                self.server = None 
     28                self.clients = {} 
     29                self.accept_func = accept_func 
     30                self.info2key = info2key 
    2831                self.open_socket() 
    29                 self.clients = {} 
    30                 self.accept_fun = accept_fun 
    31                 self.info2key = info2key 
    3232         
    3333        def __del__(self): 
     
    3737 
    3838        def open_socket(self): 
     39                ''' Handles opening of the server socket ''' 
    3940                try: 
    4041                        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     
    5556                        res.append((client, addr)) 
    5657                        self.clients[client] = addr 
    57                         self.accept_fun(client, addr) 
     58                        self.accept_func(client, addr) 
    5859                return res 
    5960 
     
    6465 
    6566        def accept_and_wait(self, timeout = None): 
     67                ''' Wraps select.  If a client tries to connect, service 
     68                    That client and go back to waiting for input from connected 
     69                        clients ''' 
    6670                cl = self.clients.keys() 
    6771                cl.append(self.server) 
     
    7882                    Accepts all waiting clients, then processes data 
    7983                    from clients with input waiting. """ 
    80                 while self.accept_client(0).__len__(): 
    81                         pass 
    8284                for c in self.waiting_clients(0): 
    8385                        try: 
     
    8890         
    8991        def get_clients(self): 
    90                 while self.accept_client(0).__len__(): 
    91                         pass 
    9292                return self.clients 
    9393 
    9494        def shutdown(self): 
     95                ''' Orderly shutdown all the listening client sockets in prep 
     96                        for exit ''' 
    9597                [c.shutdown(socket.SHUT_RDWR) for c in self.clients.keys()] 
    9698                [c.close() for c in self.clients.keys()] 
     
    98100         
    99101        def send_all(self, msg): 
    100                 print "sending all" 
     102                ''' Send a message to all clients we know about. ''' 
    101103                [pickle.dump(msg, c.makefile()) for c in self.clients.keys()] 
    102                 print "sent all" 
    103104         
    104105        def remove(self, socket): 
     106                ''' remove a client from managment by the server ''' 
    105107                try: 
    106108                        print "removal" 
     
    121123                l = filter(lambda (c, i): self.info2key(i) == key, self.clients.items()) 
    122124                return [(c, pickle.load(c.makefile())) for (c, i) in l] 
    123                          
    124  
    125 # class UDPServer: 
    126 #       def __init__(self, host='', port=50000): 
    127 #               self.host = host 
    128 #               self.port = port 
    129 #               self.server = None 
    130  
    131  
    132 cont = True 
    133  
    134 def handle_client(client, addr): 
    135         recvd = client.recv(1024) 
    136         if(len(recvd) == 0): 
    137                 client.shutdown(socket.SHUT_RDWR) 
    138                 client.close() 
    139                 return 
    140         if recvd[0:4] == "quit": 
    141                 globals()["cont"] = False 
    142                 return 
    143         client.send(recvd) 
    144  
    145 if __name__ == "__main__": 
    146         print "Starting an echo server.\n" 
    147         s = Server("", 50000) 
    148         while cont: 
    149                 s.service_clients(handle_client) 
    150         s.shutdown()