Permalink
Browse files

fix indentation

  • Loading branch information...
1 parent 88074c8 commit 8c4c8891c90421ed15f03c632ba4dc517397832f @laurentluce committed Jul 25, 2012
Showing with 205 additions and 204 deletions.
  1. +130 −129 cars/client.py
  2. +75 −73 cars/server.py
  3. +0 −2 cars/test.x
View
@@ -9,160 +9,161 @@
from twisted.internet.protocol import Protocol, ClientFactory
class TrafficClient(object):
- """
- Client
- """
- def __init__(self):
"""
- Constructor
- """
- # init logging facility: log to client.log
- logging.basicConfig(filename='client.log', level=logging.DEBUG, filemode='w+')
- logging.debug('Traffic client init')
- # init deferred object to handle callbacks and failures
- self.deferred = defer.Deferred()
- # init factory to create TrafficProtocol protocol instances
- self.factory = TrafficClientFactory(self.deferred)
- # list of cars
- self.cars = []
- # keep track of servers replying so we know when the overall work
- # is finished
- self.addr_count = 0
- # list of servers to get cars list from
- self.addresses = [('localhost', 8000), ('localhost', 8000)]
+ Client
+ """
+ def __init__(self):
+ """
+ Constructor
+ """
+ # init logging facility: log to client.log
+ logging.basicConfig(filename='client.log', level=logging.DEBUG,
+ filemode='w+')
+ logging.debug('Traffic client init')
+ # init deferred object to handle callbacks and failures
+ self.deferred = defer.Deferred()
+ # init factory to create TrafficProtocol protocol instances
+ self.factory = TrafficClientFactory(self.deferred)
+ # list of cars
+ self.cars = []
+ # keep track of servers replying so we know when the overall work
+ # is finished
+ self.addr_count = 0
+ # list of servers to get cars list from
+ self.addresses = [('localhost', 8000), ('localhost', 8000)]
- def get_cars(self, host, port):
- """
- Connect to server to retrieve list of cars
-
- @param host server's hostname
- @param port server's port
- """
- logging.debug('Get cars: %s - %d' % (host, port))
- reactor.connectTCP(host, port, self.factory)
-
- def got_cars(self, cars):
- """
- Callback when cars retrieval is successful
-
- @param cars data returned by server
- """
- logging.debug('Got cars: %s' % cars)
- self.cars.extend(cars)
+ def get_cars(self, host, port):
+ """
+ Connect to server to retrieve list of cars
+
+ @param host server's hostname
+ @param port server's port
+ """
+ logging.debug('Get cars: %s - %d' % (host, port))
+ reactor.connectTCP(host, port, self.factory)
+
+ def got_cars(self, cars):
+ """
+ Callback when cars retrieval is successful
+
+ @param cars data returned by server
+ """
+ logging.debug('Got cars: %s' % cars)
+ self.cars.extend(cars)
+
+ def get_cars_failed(self, err):
+ """
+ Callback when retrieval from server failed. Log error.
+
+ @param err server error
+ """
+ logging.debug('Get cars failed: %s' % err)
+
+ def cars_done(self, cars):
+ """
+ Callback when retrieval operation is finished for all servers.
+ Log cars list and stop Twisted reactor loop which is listening to events
+ """
+ self.addr_count += 1
+ if self.addr_count == len(self.addresses):
+ logging.debug('Cars done: %s' % self.cars)
+ reactor.stop()
+
+ def update_cars(self):
+ """
+ Retrieve list of cars from all servers. Set callbacks to handle
+ success and failure.
+ """
+ logging.debug('Update cars')
+ for address in self.addresses:
+ host, port = address
+ self.get_cars(host, port)
+ self.deferred.addCallbacks(self.got_cars, self.get_cars_failed)
+ self.deferred.addBoth(self.cars_done)
- def get_cars_failed(self, err):
+class TrafficProtocol(Protocol):
"""
- Callback when retrieval from server failed. Log error.
-
- @param err server error
+ Protocol class to handle data between the client and the server.
"""
- logging.debug('Get cars failed: %s' % err)
- def cars_done(self, cars):
- """
- Callback when retrieval operation is finished for all servers.
- Log cars list and stop Twisted reactor loop which is listening to events
- """
- self.addr_count += 1
- if self.addr_count == len(self.addresses):
- logging.debug('Cars done: %s' % self.cars)
- reactor.stop()
+ data = ''
- def update_cars(self):
- """
- Retrieve list of cars from all servers. Set callbacks to handle
- success and failure.
- """
- logging.debug('Update cars')
- for address in self.addresses:
- host, port = address
- self.get_cars(host, port)
- self.deferred.addCallbacks(self.got_cars, self.get_cars_failed)
- self.deferred.addBoth(self.cars_done)
+ def dataReceived(self, data):
+ """
+ Callback when some data is received from server.
-class TrafficProtocol(Protocol):
- """
- Protocol class to handle data between the client and the server.
- """
+ @param data data received from server
+ """
+ logging.debug('Data received: %s' % data)
+ self.data += data
- data = ''
+ def connectionLost(self, reason):
+ """
+ Callback when connection is lost with server. At that point, the
+ cars have been receieved.
- def dataReceived(self, data):
- """
- Callback when some data is received from server.
+ @param reason failure object
+ """
+ logging.debug('Connection lost: %s' % reason)
+ self.cars = []
+ for c in self.data.split('.'):
+ self.cars.append(c)
+ self.carsReceived(self.cars)
- @param data data received from server
- """
- logging.debug('Data received: %s' % data)
- self.data += data
+ def carsReceived(self, cars):
+ """
+ Called when the cars data are received.
- def connectionLost(self, reason):
- """
- Callback when connection is lost with server. At that point, the
- cars have been receieved.
+ @param cars data received from the server
+ """
+ logging.debug('Cars received: %s' % cars)
+ self.factory.get_cars_finished(cars)
- @param reason failure object
- """
- logging.debug('Connection lost: %s' % reason)
- self.cars = []
- for c in self.data.split('.'):
- self.cars.append(c)
- self.carsReceived(self.cars)
- def carsReceived(self, cars):
+class TrafficClientFactory(ClientFactory):
"""
- Called when the cars data are received.
-
- @param cars data received from the server
+ Factory to create TrafficProtocol protocol instances
"""
- logging.debug('Cars received: %s' % cars)
- self.factory.get_cars_finished(cars)
+ protocol = TrafficProtocol
-class TrafficClientFactory(ClientFactory):
- """
- Factory to create TrafficProtocol protocol instances
- """
+ def __init__(self, deferred):
+ """
+ Constructor.
- protocol = TrafficProtocol
+ @param deferred callbacks to handle completion and failures
+ """
+ logging.debug('Traffic client factory init: %s', deferred)
+ self.deferred = deferred
- def __init__(self, deferred):
- """
- Constructor.
+ def get_cars_finished(self, cars):
+ """
+ Callback when the cars data is retrieved from the server successfully
- @param deferred callbacks to handle completion and failures
- """
- logging.debug('Traffic client factory init: %s', deferred)
- self.deferred = deferred
-
- def get_cars_finished(self, cars):
- """
- Callback when the cars data is retrieved from the server successfully
+ @param cars data received from the server
+ """
+ logging.debug('Get cars finished: %s', cars)
+ if self.deferred:
+ d, self.deferred = self.deferred, None
+ d.callback(cars)
- @param cars data received from the server
- """
- logging.debug('Get cars finished: %s', cars)
- if self.deferred:
- d, self.deferred = self.deferred, None
- d.callback(cars)
+ def clientConnectionFailed(self, connector, reason):
+ """
+ Callback when connection fails
- def clientConnectionFailed(self, connector, reason):
- """
- Callback when connection fails
-
- @param connector connection object.
- @param reason failure object
- """
- logging.debug('Client connection failed: %s - %s' % (connector, reason))
- if self.deferred:
- d, self.deferred = self.deferred, None
- d.errback(reason)
+ @param connector connection object.
+ @param reason failure object
+ """
+ logging.debug('Client connection failed: %s - %s' % (connector, reason))
+ if self.deferred:
+ d, self.deferred = self.deferred, None
+ d.errback(reason)
def main():
- client = TrafficClient()
- client.update_cars()
- reactor.run()
+ client = TrafficClient()
+ client.update_cars()
+ reactor.run()
if __name__ == '__main__':
- main()
+ main()
Oops, something went wrong.

0 comments on commit 8c4c889

Please sign in to comment.