Add asyncore connection dispatcher and asynchat session handler

This commit is contained in:
Stein Magnus Jodal 2009-12-23 19:42:18 +01:00
parent 9bfb9c3511
commit 0d5eab31ea
7 changed files with 92 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
*.swp

View File

@ -82,3 +82,13 @@ Spotify Premium account), ask for a search query, list all your playlists with
tracks, play 10s from a random song from the search result, pause for two
seconds, play for five more seconds, and quit.
Running mopidy
--------------
To start mopidy, go to the root of the mopidy project, then simply run::
python mopidy
To stop mopidy, press ``CTRL+C``.

0
mopidy/__init__.py Normal file
View File

35
mopidy/__main__.py Normal file
View File

@ -0,0 +1,35 @@
import asyncore
import logging
import os
import sys
sys.path.insert(0,
os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
from mopidy import settings
from mopidy.server import MpdServer
def main():
_setup_logging(2)
MpdServer()
print 'Please connect to %s port %s using a MPD client.' % (
settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT)
asyncore.loop()
def _setup_logging(verbosity_level):
if verbosity_level == 0:
level = logging.WARNING
elif verbosity_level == 2:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(
format=settings.CONSOLE_LOG_FORMAT,
level=level,
)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.exit('\nInterrupted by user')

25
mopidy/server.py Normal file
View File

@ -0,0 +1,25 @@
import asyncore
import logging
import socket
from mopidy import settings
from mopidy.session import MpdSession
logger = logging.getLogger('server')
class MpdServer(asyncore.dispatcher):
def __init__(self, handler_class=MpdSession):
asyncore.dispatcher.__init__(self)
self.handler_class = handler_class
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT))
self.listen(1)
def handle_accept(self):
(client_socket, client_address) = self.accept()
logger.info('Connection from: [%s]:%s', *client_address)
self.handler_class(client_socket, client_address)
def handle_close(self):
self.close()

17
mopidy/session.py Normal file
View File

@ -0,0 +1,17 @@
import asynchat
import logging
logger = logging.getLogger('session')
class MpdSession(asynchat.async_chat):
def __init__(self, client_socket, client_address):
asynchat.async_chat.__init__(self, sock=client_socket)
self.input_buffer = []
self.set_terminator('\n')
def collect_incoming_data(self, data):
self.input_buffer.append(data)
def found_terminator(self):
logger.debug('Input: %s', ''.join(self.input_buffer))
self.input_buffer = []

3
mopidy/settings.py Normal file
View File

@ -0,0 +1,3 @@
CONSOLE_LOG_FORMAT = '%(levelname)-8s %(asctime)s\n %(message)s'
MPD_SERVER_HOSTNAME = 'localhost'
MPD_SERVER_PORT = 6600