Skip to content

Commit

Permalink
Replaced tornado with Twisted
Browse files Browse the repository at this point in the history
  • Loading branch information
ereOn committed Feb 3, 2015
1 parent 9a8eed0 commit 9b8cfa3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
14 changes: 10 additions & 4 deletions pyfreelan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
Entry points for pyfreelan.
"""

from tornado.ioloop import IOLoop
import logging

from twisted.internet import reactor

from .server import HTTPServer

LOGGER = logging.getLogger(__name__)


def server_main():
"""
The server entry point.
"""
logging.basicConfig()
configuration = {}

HTTPServer(configuration=configuration)
IOLoop.instance().start()
HTTPServer(reactor=reactor, configuration=configuration)
reactor.run()


def client_main():
"""
The client entry point.
"""
IOLoop.instance().start()
logging.basicConfig()
reactor.run()
15 changes: 8 additions & 7 deletions pyfreelan/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
pyfreelan server interface.
"""

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from twisted.web.server import Site
from twisted.web.wsgi import WSGIResource

from .application import APP


class HTTPServer(HTTPServer):
class HTTPServer(object):
"""
A HTTP server.
"""

def __init__(self, configuration):
def __init__(self, reactor, configuration):
"""
Initialize the HTTP server with the specified `configuration`.
:param reactor: The reactor to bind to.
:param configuration: The configuration as given by the FreeLAN core.
"""
super(HTTPServer, self).__init__(WSGIContainer(APP))

self.resource = WSGIResource(reactor, reactor.getThreadPool(), APP)
self.site = Site(self.resource)
#TODO: Pick the listen port in the configuration.
self.listen(12000)
reactor.listenTCP(12000, self.site, interface="0.0.0.0")
4 changes: 4 additions & 0 deletions pyfreelan/server/application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
from flask import Flask

APP = Flask('pyfreelan')

@APP.route('/')
def index():
return "lol"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'tests',
]),
install_requires=[
'tornado >= 4.0, < 5',
'Twisted>=15.0.0, < 16.0.0',
'Flask == 0.10.1',
],
entry_points={
Expand Down
14 changes: 7 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

from unittest import TestCase

from tornado.ioloop import IOLoop
from pyfreelan.main import (
client_main,
server_main,
)


class MainTests(TestCase):
@mock.patch('tornado.ioloop.IOLoop.instance')
def test_pyfreelan_server_launches_an_ioloop(self, instance_mock):
@mock.patch('twisted.internet.reactor.run')
def test_pyfreelan_server_launches_fine(self, reactor_mock):
server_main()
instance_mock.assert_called_with()
reactor_mock.assert_called_with()

@mock.patch('tornado.ioloop.IOLoop.instance')
def test_pyfreelan_client_launches_an_ioloop(self, instance_mock):
@mock.patch('twisted.internet.reactor.run')
def test_pyfreelan_client_launches_fine(self, reactor_mock):
client_main()
instance_mock.assert_called_with()
reactor_mock.assert_called_with()

0 comments on commit 9b8cfa3

Please sign in to comment.