From 3fe276f32a2d9e42d1e3974d9ec0e5af5b4969c7 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sat, 4 Jun 2011 00:54:02 +0200 Subject: [PATCH] Refactor MpdSession --- mopidy/frontends/mpd/session.py | 42 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/mopidy/frontends/mpd/session.py b/mopidy/frontends/mpd/session.py index 42058eb7..7226e0eb 100644 --- a/mopidy/frontends/mpd/session.py +++ b/mopidy/frontends/mpd/session.py @@ -25,39 +25,35 @@ class MpdSession(asynchat.async_chat): self.dispatcher = MpdDispatcher(session=self) def start(self): - """Start a new client session.""" - self.send_response(u'OK MPD %s' % VERSION) + """Called by asynchat when a new client connects.""" + self.send_response([u'OK MPD %s' % VERSION]) def collect_incoming_data(self, data): - """Collect incoming data into buffer until a terminator is found.""" + """Called by asynchat when new data arrives.""" self.input_buffer.append(data) def found_terminator(self): - """Handle request when a terminator is found.""" + """Called by asynchat when a terminator is found in incoming data.""" data = ''.join(self.input_buffer).strip() self.input_buffer = [] try: - request = data.decode(ENCODING) - logger.debug(u'Input from [%s]:%s: %s', self.client_address, - self.client_port, indent(request)) - self.handle_request(request) + self.send_response(self.handle_request(data)) except UnicodeDecodeError as e: logger.warning(u'Received invalid data: %s', e) def handle_request(self, request): - """Handle request using the MPD command handlers.""" - response = self.dispatcher.handle_request(request) + """Handle the request using the MPD command handlers.""" + request = request.decode(ENCODING) + logger.debug(u'Request from [%s]:%s: %s', self.client_address, + self.client_port, indent(request)) + return self.dispatcher.handle_request(request) + + def send_response(self, response): + """Format a response from the MPD command handlers and send it to the client.""" if response is not None: - self.handle_response(response) - - def handle_response(self, response): - """Handle response from the MPD command handlers.""" - self.send_response(LINE_TERMINATOR.join(response)) - - def send_response(self, output): - """Send a response to the client.""" - logger.debug(u'Output to [%s]:%s: %s', self.client_address, - self.client_port, indent(output)) - output = u'%s%s' % (output, LINE_TERMINATOR) - data = output.encode(ENCODING) - self.push(data) + response = LINE_TERMINATOR.join(response) + logger.debug(u'Response to [%s]:%s: %s', self.client_address, + self.client_port, indent(response)) + response = u'%s%s' % (response, LINE_TERMINATOR) + data = response.encode(ENCODING) + self.push(data)