ginsfsm / ginsfsm / protocols / sockjs / server / transports / xhrstreaming.py

The default branch has multiple heads

# -*- coding: utf-8 -*-
"""
    Xhr-Streaming transport implementation
"""
from pyramid.view import view_config
from pyramid.security import authenticated_userid

from ginsfsm.gobj import GObj
from ginsfsm.protocols.http.server.c_http_clisrv import ResponseInterrupt
from ginsfsm.protocols.sockjs.server.transports import streamingbase

#----------------------------------------------------------------#
#                   GXhrStreaming GClass
#----------------------------------------------------------------#
GXHRSTREAMING_GCONFIG = {
    'sockjs_server': [None, None, 0, None, ""],
}


class GXhrStreaming(GObj):
    """  GStreaming GObj.
    """
    def __init__(self):
        GObj.__init__(self, {}, GXHRSTREAMING_GCONFIG)

    def start_up(self):
        """ Initialization zone.
        """


#----------------------------------------------------------------#
#                   GXhrStreaming Views
#----------------------------------------------------------------#
@view_config(
    context=GXhrStreaming,
    name='',
    attr='options',
    request_method='OPTIONS',
)
@view_config(
    context=GXhrStreaming,
    name='',
    attr='post',
    request_method='POST',
)
class XhrStreamingTransport(streamingbase.StreamingTransportBase):
    name = 'xhr_streaming'

    def post(self):
        #response = self.set_response(self.request.response)
        #response = self.set_response(self)  # Response is self.
        response = self
        response.headers = ()  # important trick: clean default webob headers.

        self.sid = self.context.parent.re_matched_name  # session_id
        if self.context.sockjs_server.per_user:
            self.sid = (authenticated_userid(self.request), self.sid)

        #try: esto es de pyramid_sockjs
        #    session = manager.get(sid, create, request=request)
        #except KeyError:
        #    return HTTPNotFound(headers=(session_cookie(request),))
        #request.environ['wsgi.sockjs_session'] = session

        # Start response
        self.preflight()
        self.handle_session_cookie()
        self.disable_cache()
        response.content_type = 'application/javascript'
        response.charset = 'UTF-8'

        self.ginsfsm_channel = self.request.environ['ginsfsm.channel']

        return self  # Webob will execute __call__

    def __call__(self, environ, start_response):
        start_response(self.status, self._abs_headerlist(environ))
        self.ginsfsm_channel.write('h' * 2048 + '\n')
        self.ginsfsm_channel.flush()

        if not self._attach_session(self.sid, False):
            self.finish()
            return ()

        if self.session:
            self.ginsfsm_channel.flush()
            #self.session.flush()
        raise ResponseInterrupt()

    def send_pack(self, message, binary=False):
        if binary:
            raise Exception('binary not supported for XhrStreamingTransport')

        print ">>>>>>>>>>>>>>>>>>>>>>> SEND_PACK:", message
        self.active = False  # TODO si chequeo active fallan los tests

        try:
            self.notify_sent(len(message))

            self.write(message + '\n')
            self.flush(callback=self.send_complete)
        except:
            # If connection dropped, make sure we close offending session instead
            # of propagating error all way up.
            self.session.delayed_close()
            self._detach()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.