# -*- coding: utf-8 -*-

"""TCP server handling connections from other MURRAY-Peers"""

import SocketServer, time, thread, globalv, xml_to_core
import net, logger

class MurrayTCPServer(SocketServer.StreamRequestHandler):
        
    to_core = xml_to_core.ReadXml()
    
    """TCP-Server handling incoming MURRAY-messages. 
    Extends StreamRequestHandler overwriting its handle() method to hand all
    incoming messages to the xml-and net module"""
    def handle(self):
        """modified handle()-Method hands over the data to the XML-Parser"""
        input = self.rfile.read()
        try:
            args_core = self.to_core.incoming(input)
            args_core['receivedfrom'] = self.client_address
            # thread.start_new_thread(net.incoming, (args_core['type'], args_core))
            net.incoming(args_core['type'], args_core)
        except:
            pass
    
if __name__=='__main__':
    PORT = 4711
    Controller().startServer()