Permalink
Browse files

First implementation steps.

  • Loading branch information...
1 parent 8223688 commit 5e805497feb7aad85386cc6996faa406a70c2c79 @mrjoes committed Dec 7, 2011
View
@@ -0,0 +1,10 @@
+*.swp
+*.swo
+*.pyc
+*.*~
+pyenv
+#*#
+build
+source/_static*
+source/_templates*
+make.bat
View
@@ -0,0 +1,14 @@
+Copyright (c) 2011, Serge. S. Koval. <http://in-square.net/>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
View
@@ -0,0 +1,2 @@
+Tornado>=2.1.1
+simplejson>=2.1.0
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+try:
+ from setuptools import setup, find_packages
+except ImportError:
+ from distribute_setup import use_setuptools
+ use_setuptools()
+ from setuptools import setup, find_packages
+
+try:
+ license = open('LICENSE').read()
+except:
+ license = None
+
+try:
+ readme = open('README.rst').read()
+except:
+ readme = None
+
+setup(
+ name='sockjs-tornado',
+ version='0.0.1',
+ author='Serge S. Koval',
+ author_email='serge.koval@gmail.com',
+ packages=['sockjs.tornado'],
+ namespace_packages=['sockjs'],
+ scripts=[],
+ url='http://github.com/MrJoes/sockjs-tornado/',
+ license=license,
+ description='SockJS pyton server implementation on top of Tornado framework',
+ long_description=readme,
+ requires=['simplejson', 'tornado'],
+ install_requires=[
+ 'simplejson >= 2.1.0',
+ 'tornado >= 2.0.0'
+ ]
+)
View
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
@@ -0,0 +1,12 @@
+import pkgutil
+import pprint
+
+print 'demopkg1.__path__ before:'
+pprint.pprint(__path__)
+print
+
+__path__ = pkgutil.extend_path(__path__, __name__)
+
+print 'demopkg1.__path__ after:'
+pprint.pprint(__path__)
+print
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright: (c) 2011 by the Serge S. Koval, see AUTHORS for more details.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""
+ sockjs.tornado.flashserver
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ This module implements customized PeriodicCallback from tornado with
+ support of the sliding window.
+"""
+import time
+import logging
+
+
+class Callback(object):
+ """Custom implementation of the Tornado.Callback with support
+ of callback timeout delays.
+ """
+ def __init__(self, callback, callback_time, io_loop):
+ """Constructor.
+
+ `callback`
+ Callback function
+ `callback_time`
+ Callback timeout value (in milliseconds)
+ `io_loop`
+ io_loop instance
+ """
+ self.callback = callback
+ self.callback_time = callback_time
+ self.io_loop = io_loop
+ self._running = False
+
+ self.next_run = None
+
+ def calculate_next_run(self):
+ """Caltulate next scheduled run"""
+ return time.time() + self.callback_time / 1000.0
+
+ def start(self, timeout=None):
+ """Start callbacks"""
+ self._running = True
+
+ if timeout is None:
+ timeout = self.calculate_next_run()
+
+ self.io_loop.add_timeout(timeout, self._run)
+
+ def stop(self):
+ """Stop callbacks"""
+ self._running = False
+
+ def delay(self):
+ """Delay callback"""
+ self.next_run = self.calculate_next_run()
+
+ def _run(self):
+ if not self._running:
+ return
+
+ # Support for shifting callback window
+ if self.next_run is not None and time.time() < self.next_run:
+ self.start(self.next_run)
+ self.next_run = None
+ return
+
+ next_call = None
+ try:
+ next_call = self.callback()
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except:
+ logging.error("Error in periodic callback", exc_info=True)
+
+ if self._running:
+ self.start(next_call)
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright: (c) 2011 by the Serge S. Koval, see AUTHORS for more details.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""
+ sockjs.tornado.proto
+ ~~~~~~~~~~~~~~~~~~~~
+
+ SockJS protocol related functions
+"""
+try:
+ import simplejson as json
+ json_decimal_args = {"use_decimal": True}
+except ImportError:
+ import json
+ import decimal
+
+ class DecimalEncoder(json.JSONEncoder):
+ def default(self, o):
+ if isinstance(o, decimal.Decimal):
+ return float(o)
+ return super(DecimalEncoder, self).default(o)
+ json_decimal_args = {"cls": DecimalEncoder}
+
+CONNECT = 'o'
+DISCONNECT = 'c'
+MESSAGE = 'm'
+HEARTBEAT = 'h'
+
+
+def disconnect(code, reason):
+ return 'c[%d,"%s"]' % (code, reason)
+
+
+def encode_messages(messages):
+ return 'a%s' % json.dumps(messages)
+
+
+def json_dumps(msg):
+ """Dump object as a json string
+
+ `msg`
+ Object to dump
+ """
+ return json.dumps(msg)
+
+
+def json_load(msg):
+ """Load json-encoded object
+
+ `msg`
+ json encoded object
+ """
+ return json.loads(msg)
Oops, something went wrong.

0 comments on commit 5e80549

Please sign in to comment.