#!/usr/bin/env python
from __future__ import absolute_import
import sys

import tornado.ioloop
import tornado.tcpserver

from options import get_args
from tchannel.tornado.connection import TornadoConnection


class MyServer(tornado.tcpserver.TCPServer):
    def handle_stream(self, stream, address):
        tchannel_connection = TornadoConnection(
            connection=stream
        )

        print("Received request from %s:%d" % address)

        print("Waiting for TChannel handshake...")
        tchannel_connection.await_handshake(headers={
            'host_port': '%s:%s' % address,
            'process_name': sys.argv[0],
        }, callback=self.handshake_complete)

    def handshake_complete(self, connection):
        print(
            "Successfully completed handshake with %s" %
            connection.remote_process_name
        )
        connection.handle_calls(self.handle_call)

    def handle_call(self, context, connection):
        """Handle a TChannel CALL_REQ message."""
        if not context:
            print("All done with connection")
            return

        print("Received message: %s" % context.message)
        connection.handle_calls(self.handle_call)


if __name__ == '__main__':
    args = get_args()
    server = MyServer()
    server.listen(args.port)
    print("Listening on port %d..." % args.port)
    tornado.ioloop.IOLoop.instance().start()

@@ -143,16 +143,66 @@
 et_args%0A
+from tchannel.messages import CallResponseMessage%0A
 from tch
@@ -1191,16 +1191,622 @@
 message)
+%0A%0A        if context.message.arg_1:%0A            response = CallResponseMessage()%0A            response.flags = 0%0A            response.code = 200%0A            response.span_id = 0%0A            response.parent_id = 0%0A            response.trace_id = 0%0A            response.traceflags = 0%0A            response.headers = %7B'currently': 'broken'%7D%0A            response.checksum_type = 0%0A            response.checksum = 0%0A            response.arg_1 = context.message.arg_1%0A            response.arg_2 = 'hello world'%0A            response.arg_3 = context.message.arg_3%0A%0A            connection.frame_and_write(response)%0A%0A
 %0A       

make tornado example send responses