"""
Generated by Statechart compiler by Glenn De Jonghe and Joeri Exelmans

Date:   Tue Jan 19 08:40:10 2016

Model author: Yentl Van Tendeloo
Model name:   MvK Server interface
Model description:
MvK Server interface that receives events through the Socket2Event layer.
        Incoming messages are processed as HTTP requests and are forwarded to the MvK.
"""

from python_runtime.statecharts_core import *
import urlparse
import urllib
import sys
import traceback
import json

from modelverse_state.main import ModelverseState

# package "MvK Server interface"

class Server(RuntimeClassBase):
	def __init__(self, controller, bootfile):
		RuntimeClassBase.__init__(self, controller)
		
		self.semantics.big_step_maximality = StatechartSemantics.TakeMany
		self.semantics.internal_event_lifeline = StatechartSemantics.Queue
		self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
		self.semantics.priority = StatechartSemantics.SourceParent
		self.semantics.concurrency = StatechartSemantics.Single
		
		# Call user defined constructor
		Server.user_defined_constructor(self, bootfile)
	
	def user_defined_constructor(self, bootfile):
		self.socket = None
		
		self.processing_association_name = None
		self.processing_data = None
		self.queue = []
		self.mvs = ModelverseState(bootfile)
		self.mvs_operations = {
		       "CN": self.mvs.create_node,
		       "CE": self.mvs.create_edge,
		       "CNV": self.mvs.create_nodevalue,
		       "CD": self.mvs.create_dict,
		
		       "RV": self.mvs.read_value,
		       "RO": self.mvs.read_outgoing,
		       "RI": self.mvs.read_incoming,
		       "RE": self.mvs.read_edge,
		       "RD": self.mvs.read_dict,
		       "RDN": self.mvs.read_dict_node,
		       "RDE": self.mvs.read_dict_edge,
		       "RRD": self.mvs.read_reverse_dict,
		       "RR": self.mvs.read_root,
		       "RDK": self.mvs.read_dict_keys,
		
		       "DE": self.mvs.delete_edge,
		       "DN": self.mvs.delete_node,
		
		       "DG": self.mvs.dump,
		    }
	
	def user_defined_destructor(self):
		pass
	
	
	# User defined method
	def process_message(self, operation_name, parameters):
		op = self.mvs_operations[operation_name]
		return op(*parameters)
	
	def initializeStatechart(self):
		self.current_state[self.Root] = []
		self.current_state[self.Root_main] = []
		self.current_state[self.Root_main_queue] = []
		self.current_state[self.Root_main_processor] = []
		self.current_state[self.Root_main_server] = []
		self.current_state[self.Root_main_close_socket] = []
		
		# Enter default state
		self.enterDefault_Root_main()
	
	# Unique IDs for all statechart nodes
	Root = 0
	Root_main = 1
	Root_main_queue = 2
	Root_main_processor = 3
	Root_main_server = 4
	Root_main_close_socket = 5
	Root_main_queue_queue = 6
	Root_main_processor_wait_for_input = 7
	Root_main_processor_process_input = 8
	Root_main_server_init = 9
	Root_main_server_binding = 10
	Root_main_server_listening = 11
	Root_main_server_accepting = 12
	Root_main_close_socket_close = 13
	
	# Statechart enter/exit action method(s)
	
	def enter_Root_main(self):
		self.current_state[self.Root].append(self.Root_main)
	
	def exit_Root_main(self):
		self.exit_Root_main_queue()
		self.exit_Root_main_processor()
		self.exit_Root_main_server()
		self.exit_Root_main_close_socket()
		self.current_state[self.Root] = []
	
	def enter_Root_main_queue(self):
		self.current_state[self.Root_main].append(self.Root_main_queue)
	
	def exit_Root_main_queue(self):
		if self.Root_main_queue_queue in self.current_state[self.Root_main_queue]:
			self.exit_Root_main_queue_queue()
		self.current_state[self.Root_main] = []
	
	def enter_Root_main_processor(self):
		self.current_state[self.Root_main].append(self.Root_main_processor)
	
	def exit_Root_main_processor(self):
		if self.Root_main_processor_wait_for_input in self.current_state[self.Root_main_processor]:
			self.exit_Root_main_processor_wait_for_input()
		if self.Root_main_processor_process_input in self.current_state[self.Root_main_processor]:
			self.exit_Root_main_processor_process_input()
		self.current_state[self.Root_main] = []
	
	def enter_Root_main_server(self):
		self.current_state[self.Root_main].append(self.Root_main_server)
	
	def exit_Root_main_server(self):
		if self.Root_main_server_init in self.current_state[self.Root_main_server]:
			self.exit_Root_main_server_init()
		if self.Root_main_server_binding in self.current_state[self.Root_main_server]:
			self.exit_Root_main_server_binding()
		if self.Root_main_server_listening in self.current_state[self.Root_main_server]:
			self.exit_Root_main_server_listening()
		if self.Root_main_server_accepting in self.current_state[self.Root_main_server]:
			self.exit_Root_main_server_accepting()
		self.current_state[self.Root_main] = []
	
	def enter_Root_main_close_socket(self):
		self.current_state[self.Root_main].append(self.Root_main_close_socket)
	
	def exit_Root_main_close_socket(self):
		if self.Root_main_close_socket_close in self.current_state[self.Root_main_close_socket]:
			self.exit_Root_main_close_socket_close()
		self.current_state[self.Root_main] = []
	
	def enter_Root_main_queue_queue(self):
		self.current_state[self.Root_main_queue].append(self.Root_main_queue_queue)
	
	def exit_Root_main_queue_queue(self):
		self.current_state[self.Root_main_queue] = []
	
	def enter_Root_main_processor_wait_for_input(self):
		self.current_state[self.Root_main_processor].append(self.Root_main_processor_wait_for_input)
	
	def exit_Root_main_processor_wait_for_input(self):
		self.current_state[self.Root_main_processor] = []
	
	def enter_Root_main_processor_process_input(self):
		self.current_state[self.Root_main_processor].append(self.Root_main_processor_process_input)
	
	def exit_Root_main_processor_process_input(self):
		self.current_state[self.Root_main_processor] = []
	
	def enter_Root_main_server_init(self):
		self.big_step.outputEvent(Event("create_socket", "socket_out", []))
		self.current_state[self.Root_main_server].append(self.Root_main_server_init)
	
	def exit_Root_main_server_init(self):
		self.current_state[self.Root_main_server] = []
	
	def enter_Root_main_server_binding(self):
		self.big_step.outputEvent(Event("bind_socket", "socket_out", [self.socket, ('', 8000)]))
		self.current_state[self.Root_main_server].append(self.Root_main_server_binding)
	
	def exit_Root_main_server_binding(self):
		self.current_state[self.Root_main_server] = []
	
	def enter_Root_main_server_listening(self):
		self.big_step.outputEvent(Event("listen_socket", "socket_out", [self.socket]))
		self.current_state[self.Root_main_server].append(self.Root_main_server_listening)
	
	def exit_Root_main_server_listening(self):
		self.current_state[self.Root_main_server] = []
	
	def enter_Root_main_server_accepting(self):
		self.big_step.outputEvent(Event("accept_socket", "socket_out", [self.socket]))
		self.current_state[self.Root_main_server].append(self.Root_main_server_accepting)
	
	def exit_Root_main_server_accepting(self):
		self.current_state[self.Root_main_server] = []
	
	def enter_Root_main_close_socket_close(self):
		self.current_state[self.Root_main_close_socket].append(self.Root_main_close_socket_close)
	
	def exit_Root_main_close_socket_close(self):
		self.current_state[self.Root_main_close_socket] = []
	
	# Statechart enter/exit default method(s)
	
	def enterDefault_Root_main(self):
		self.enter_Root_main()
		self.enterDefault_Root_main_queue()
		self.enterDefault_Root_main_processor()
		self.enterDefault_Root_main_server()
		self.enterDefault_Root_main_close_socket()
	
	def enterDefault_Root_main_queue(self):
		self.enter_Root_main_queue()
		self.enter_Root_main_queue_queue()
	
	def enterDefault_Root_main_processor(self):
		self.enter_Root_main_processor()
		self.enter_Root_main_processor_wait_for_input()
	
	def enterDefault_Root_main_server(self):
		self.enter_Root_main_server()
		self.enter_Root_main_server_init()
	
	def enterDefault_Root_main_close_socket(self):
		self.enter_Root_main_close_socket()
		self.enter_Root_main_close_socket_close()
	
	# Statechart transitions
	
	def generateCandidatesChildren_Root(self):
		if self.current_state[self.Root][0] == self.Root_main:
			return self.generateCandidates_Root_main()
		return False
	
	def generateCandidates_Root(self):
		if not self.combo_step.isArenaChanged(self.Root):
			return self.generateCandidatesChildren_Root()
		else:
			return True
	
	def generateCandidatesChildren_Root_main(self):
		branch_done = False
		branch_done = (self.generateCandidates_Root_main_queue() or branch_done)
		branch_done = (self.generateCandidates_Root_main_processor() or branch_done)
		branch_done = (self.generateCandidates_Root_main_server() or branch_done)
		branch_done = (self.generateCandidates_Root_main_close_socket() or branch_done)
		return branch_done
	
	def generateCandidates_Root_main(self):
		if not self.combo_step.isArenaChanged(self.Root_main):
			return self.generateCandidatesChildren_Root_main()
		else:
			return True
	
	def generateCandidatesChildren_Root_main_queue(self):
		if self.current_state[self.Root_main_queue][0] == self.Root_main_queue_queue:
			return self.generateCandidates_Root_main_queue_queue()
		return False
	
	def generateCandidates_Root_main_queue(self):
		if not self.combo_step.isArenaChanged(self.Root_main_queue):
			return self.generateCandidatesChildren_Root_main_queue()
		else:
			return True
	
	def generateCandidatesCurrent_Root_main_queue_queue(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "HTTP_output":
				self.small_step.addCandidate(self.transition_Root_main_queue_queue_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_main_queue_queue(self):
		if not self.combo_step.isArenaChanged(self.Root_main_queue_queue):
			return self.generateCandidatesCurrent_Root_main_queue_queue()
		else:
			return True
	
	def transition_Root_main_queue_queue_1(self, parameters):
		association_name = parameters[0]
		data = parameters[1]
		self.exit_Root_main_queue_queue()
		self.queue.append((association_name, data))
		self.combo_step.setArenaChanged(self.Root_main_queue)
		self.enter_Root_main_queue_queue()
	
	def generateCandidatesChildren_Root_main_processor(self):
		if self.current_state[self.Root_main_processor][0] == self.Root_main_processor_wait_for_input:
			return self.generateCandidates_Root_main_processor_wait_for_input()
		elif self.current_state[self.Root_main_processor][0] == self.Root_main_processor_process_input:
			return self.generateCandidates_Root_main_processor_process_input()
		return False
	
	def generateCandidates_Root_main_processor(self):
		if not self.combo_step.isArenaChanged(self.Root_main_processor):
			return self.generateCandidatesChildren_Root_main_processor()
		else:
			return True
	
	def generateCandidatesCurrent_Root_main_processor_wait_for_input(self):
		enabled_events = self.getEnabledEvents()
		if len(self.queue) > 0:
			self.small_step.addCandidate(self.transition_Root_main_processor_wait_for_input_1, [])
			return True
		return False
	
	def generateCandidates_Root_main_processor_wait_for_input(self):
		if not self.combo_step.isArenaChanged(self.Root_main_processor_wait_for_input):
			return self.generateCandidatesCurrent_Root_main_processor_wait_for_input()
		else:
			return True
	
	def transition_Root_main_processor_wait_for_input_1(self, parameters):
		self.exit_Root_main_processor_wait_for_input()
		self.processing_association_name, self.processing_data = self.queue.pop(0)
		try:
		    self.processing_data = json.loads(self.processing_data["requests"])
		except:
		    print("Error in deserialization: " + str(self.processing_data))
		self.combo_step.setArenaChanged(self.Root_main_processor)
		self.enter_Root_main_processor_process_input()
	
	def generateCandidatesCurrent_Root_main_processor_process_input(self):
		enabled_events = self.getEnabledEvents()
		self.small_step.addCandidate(self.transition_Root_main_processor_process_input_1, [])
		return True
		return False
	
	def generateCandidates_Root_main_processor_process_input(self):
		if not self.combo_step.isArenaChanged(self.Root_main_processor_process_input):
			return self.generateCandidatesCurrent_Root_main_processor_process_input()
		else:
			return True
	
	def transition_Root_main_processor_process_input_1(self, parameters):
		self.exit_Root_main_processor_process_input()
		reply = []
		print(self.processing_data)
		for op, params in self.processing_data:
		    try:
		        result = self.process_message(op, params)
		        try:
		            reply.append(result)
		        except:
		            traceback.print_exc()
		            reply.append([None, 496])
		    except:
		        traceback.print_exc()
		        reply.append([None, 497])
		self.big_step.outputEventOM(Event("narrow_cast", None, [self, self.processing_association_name, Event("HTTP_input", None, [{'data': json.dumps(reply)}])]))
		self.combo_step.setArenaChanged(self.Root_main_processor)
		self.enter_Root_main_processor_wait_for_input()
	
	def generateCandidatesChildren_Root_main_server(self):
		if self.current_state[self.Root_main_server][0] == self.Root_main_server_init:
			return self.generateCandidates_Root_main_server_init()
		elif self.current_state[self.Root_main_server][0] == self.Root_main_server_binding:
			return self.generateCandidates_Root_main_server_binding()
		elif self.current_state[self.Root_main_server][0] == self.Root_main_server_listening:
			return self.generateCandidates_Root_main_server_listening()
		elif self.current_state[self.Root_main_server][0] == self.Root_main_server_accepting:
			return self.generateCandidates_Root_main_server_accepting()
		return False
	
	def generateCandidates_Root_main_server(self):
		if not self.combo_step.isArenaChanged(self.Root_main_server):
			return self.generateCandidatesChildren_Root_main_server()
		else:
			return True
	
	def generateCandidatesCurrent_Root_main_server_init(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "created_socket") and (e.port == "socket_in"):
				self.small_step.addCandidate(self.transition_Root_main_server_init_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_main_server_init(self):
		if not self.combo_step.isArenaChanged(self.Root_main_server_init):
			return self.generateCandidatesCurrent_Root_main_server_init()
		else:
			return True
	
	def transition_Root_main_server_init_1(self, parameters):
		socket = parameters[0]
		self.exit_Root_main_server_init()
		self.socket = socket
		self.combo_step.setArenaChanged(self.Root_main_server)
		self.enter_Root_main_server_binding()
	
	def generateCandidatesCurrent_Root_main_server_binding(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "bound_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				if self.socket == socket:
					self.small_step.addCandidate(self.transition_Root_main_server_binding_1, e.parameters)
					return True
		return False
	
	def generateCandidates_Root_main_server_binding(self):
		if not self.combo_step.isArenaChanged(self.Root_main_server_binding):
			return self.generateCandidatesCurrent_Root_main_server_binding()
		else:
			return True
	
	def transition_Root_main_server_binding_1(self, parameters):
		socket = parameters[0]
		self.exit_Root_main_server_binding()
		self.combo_step.setArenaChanged(self.Root_main_server)
		self.enter_Root_main_server_listening()
	
	def generateCandidatesCurrent_Root_main_server_listening(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "listened_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				if self.socket == socket:
					self.small_step.addCandidate(self.transition_Root_main_server_listening_1, e.parameters)
					return True
		return False
	
	def generateCandidates_Root_main_server_listening(self):
		if not self.combo_step.isArenaChanged(self.Root_main_server_listening):
			return self.generateCandidatesCurrent_Root_main_server_listening()
		else:
			return True
	
	def transition_Root_main_server_listening_1(self, parameters):
		socket = parameters[0]
		self.exit_Root_main_server_listening()
		self.combo_step.setArenaChanged(self.Root_main_server)
		self.enter_Root_main_server_accepting()
	
	def generateCandidatesCurrent_Root_main_server_accepting(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "accepted_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				connected_socket = parameters[1]
				if self.socket == socket:
					self.small_step.addCandidate(self.transition_Root_main_server_accepting_1, e.parameters)
					return True
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "instance_created":
				self.small_step.addCandidate(self.transition_Root_main_server_accepting_2, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_main_server_accepting(self):
		if not self.combo_step.isArenaChanged(self.Root_main_server_accepting):
			return self.generateCandidatesCurrent_Root_main_server_accepting()
		else:
			return True
	
	def transition_Root_main_server_accepting_1(self, parameters):
		socket = parameters[0]
		connected_socket = parameters[1]
		self.exit_Root_main_server_accepting()
		self.big_step.outputEventOM(Event("create_instance", None, [self, 'sockets', 'Socket', connected_socket]))
		self.combo_step.setArenaChanged(self.Root_main_server)
		self.enter_Root_main_server_accepting()
	
	def transition_Root_main_server_accepting_2(self, parameters):
		instancename = parameters[0]
		self.exit_Root_main_server_accepting()
		self.big_step.outputEventOM(Event("start_instance", None, [self, instancename]))
		self.big_step.outputEventOM(Event("narrow_cast", None, [self, instancename, Event("set_association_name", None, [instancename])]))
		self.combo_step.setArenaChanged(self.Root_main_server)
		self.enter_Root_main_server_accepting()
	
	def generateCandidatesChildren_Root_main_close_socket(self):
		if self.current_state[self.Root_main_close_socket][0] == self.Root_main_close_socket_close:
			return self.generateCandidates_Root_main_close_socket_close()
		return False
	
	def generateCandidates_Root_main_close_socket(self):
		if not self.combo_step.isArenaChanged(self.Root_main_close_socket):
			return self.generateCandidatesChildren_Root_main_close_socket()
		else:
			return True
	
	def generateCandidatesCurrent_Root_main_close_socket_close(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "close":
				self.small_step.addCandidate(self.transition_Root_main_close_socket_close_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_main_close_socket_close(self):
		if not self.combo_step.isArenaChanged(self.Root_main_close_socket_close):
			return self.generateCandidatesCurrent_Root_main_close_socket_close()
		else:
			return True
	
	def transition_Root_main_close_socket_close_1(self, parameters):
		association_name = parameters[0]
		self.exit_Root_main_close_socket_close()
		self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
		self.combo_step.setArenaChanged(self.Root_main_close_socket)
		self.enter_Root_main_close_socket_close()
	
	# Generate transition candidates for current small step
	def generateCandidates(self):
		self.generateCandidates_Root()

class Socket(RuntimeClassBase):
	def __init__(self, controller, my_socket):
		RuntimeClassBase.__init__(self, controller)
		
		self.semantics.big_step_maximality = StatechartSemantics.TakeMany
		self.semantics.internal_event_lifeline = StatechartSemantics.Queue
		self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
		self.semantics.priority = StatechartSemantics.SourceParent
		self.semantics.concurrency = StatechartSemantics.Single
		
		# Call user defined constructor
		Socket.user_defined_constructor(self, my_socket)
	
	def user_defined_constructor(self, my_socket):
		self.socket = my_socket
		self.received_data = ""
		self.send_data = ""
		self.closed = False
		self.association_name = None
	
	def user_defined_destructor(self):
		pass
	
	def initializeStatechart(self):
		self.current_state[self.Root] = []
		self.current_state[self.Root_connected] = []
		self.current_state[self.Root_connected_listening] = []
		self.current_state[self.Root_connected_sending] = []
		self.current_state[self.Root_connected_queueing] = []
		self.current_state[self.Root_connected_parsing] = []
		
		# Enter default state
		self.enter_Root_init()
	
	# Unique IDs for all statechart nodes
	Root = 0
	Root_connected = 1
	Root_connected_listening = 2
	Root_connected_sending = 3
	Root_connected_queueing = 4
	Root_connected_parsing = 5
	Root_init = 6
	Root_connected_listening_listen = 7
	Root_connected_listening_closed = 8
	Root_connected_sending_waiting_for_data = 9
	Root_connected_sending_transferring = 10
	Root_connected_queueing_queueing = 11
	Root_connected_parsing_wait_for_header = 12
	Root_connected_parsing_closing = 13
	Root_connected_parsing_wait_for_payload = 14
	Root_close = 15
	
	# Statechart enter/exit action method(s)
	
	def enter_Root_connected(self):
		self.current_state[self.Root].append(self.Root_connected)
	
	def exit_Root_connected(self):
		self.exit_Root_connected_listening()
		self.exit_Root_connected_sending()
		self.exit_Root_connected_queueing()
		self.exit_Root_connected_parsing()
		self.current_state[self.Root] = []
	
	def enter_Root_connected_listening(self):
		self.current_state[self.Root_connected].append(self.Root_connected_listening)
	
	def exit_Root_connected_listening(self):
		if self.Root_connected_listening_listen in self.current_state[self.Root_connected_listening]:
			self.exit_Root_connected_listening_listen()
		if self.Root_connected_listening_closed in self.current_state[self.Root_connected_listening]:
			self.exit_Root_connected_listening_closed()
		self.current_state[self.Root_connected] = []
	
	def enter_Root_connected_sending(self):
		self.current_state[self.Root_connected].append(self.Root_connected_sending)
	
	def exit_Root_connected_sending(self):
		if self.Root_connected_sending_waiting_for_data in self.current_state[self.Root_connected_sending]:
			self.exit_Root_connected_sending_waiting_for_data()
		if self.Root_connected_sending_transferring in self.current_state[self.Root_connected_sending]:
			self.exit_Root_connected_sending_transferring()
		self.current_state[self.Root_connected] = []
	
	def enter_Root_connected_queueing(self):
		self.current_state[self.Root_connected].append(self.Root_connected_queueing)
	
	def exit_Root_connected_queueing(self):
		if self.Root_connected_queueing_queueing in self.current_state[self.Root_connected_queueing]:
			self.exit_Root_connected_queueing_queueing()
		self.current_state[self.Root_connected] = []
	
	def enter_Root_connected_parsing(self):
		self.current_state[self.Root_connected].append(self.Root_connected_parsing)
	
	def exit_Root_connected_parsing(self):
		if self.Root_connected_parsing_wait_for_header in self.current_state[self.Root_connected_parsing]:
			self.exit_Root_connected_parsing_wait_for_header()
		if self.Root_connected_parsing_closing in self.current_state[self.Root_connected_parsing]:
			self.exit_Root_connected_parsing_closing()
		if self.Root_connected_parsing_wait_for_payload in self.current_state[self.Root_connected_parsing]:
			self.exit_Root_connected_parsing_wait_for_payload()
		self.current_state[self.Root_connected] = []
	
	def enter_Root_init(self):
		self.current_state[self.Root].append(self.Root_init)
	
	def exit_Root_init(self):
		self.current_state[self.Root] = []
	
	def enter_Root_connected_listening_listen(self):
		self.big_step.outputEvent(Event("recv_socket", "socket_out", [self.socket]))
		self.current_state[self.Root_connected_listening].append(self.Root_connected_listening_listen)
	
	def exit_Root_connected_listening_listen(self):
		self.current_state[self.Root_connected_listening] = []
	
	def enter_Root_connected_listening_closed(self):
		self.closed = True
		self.current_state[self.Root_connected_listening].append(self.Root_connected_listening_closed)
	
	def exit_Root_connected_listening_closed(self):
		self.current_state[self.Root_connected_listening] = []
	
	def enter_Root_connected_sending_waiting_for_data(self):
		self.current_state[self.Root_connected_sending].append(self.Root_connected_sending_waiting_for_data)
	
	def exit_Root_connected_sending_waiting_for_data(self):
		self.current_state[self.Root_connected_sending] = []
	
	def enter_Root_connected_sending_transferring(self):
		self.current_state[self.Root_connected_sending].append(self.Root_connected_sending_transferring)
	
	def exit_Root_connected_sending_transferring(self):
		self.current_state[self.Root_connected_sending] = []
	
	def enter_Root_connected_queueing_queueing(self):
		self.current_state[self.Root_connected_queueing].append(self.Root_connected_queueing_queueing)
	
	def exit_Root_connected_queueing_queueing(self):
		self.current_state[self.Root_connected_queueing] = []
	
	def enter_Root_connected_parsing_wait_for_header(self):
		self.current_state[self.Root_connected_parsing].append(self.Root_connected_parsing_wait_for_header)
	
	def exit_Root_connected_parsing_wait_for_header(self):
		self.current_state[self.Root_connected_parsing] = []
	
	def enter_Root_connected_parsing_closing(self):
		self.timers[0] = 0.0
		self.current_state[self.Root_connected_parsing].append(self.Root_connected_parsing_closing)
	
	def exit_Root_connected_parsing_closing(self):
		self.timers.pop(0, None)
		self.current_state[self.Root_connected_parsing] = []
	
	def enter_Root_connected_parsing_wait_for_payload(self):
		self.current_state[self.Root_connected_parsing].append(self.Root_connected_parsing_wait_for_payload)
	
	def exit_Root_connected_parsing_wait_for_payload(self):
		self.current_state[self.Root_connected_parsing] = []
	
	def enter_Root_close(self):
		self.big_step.outputEvent(Event("close_socket", "socket_out", [self.socket]))
		self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("close_socket", None, [self.association_name])]))
		self.current_state[self.Root].append(self.Root_close)
	
	def exit_Root_close(self):
		self.current_state[self.Root] = []
	
	# Statechart enter/exit default method(s)
	
	def enterDefault_Root_connected(self):
		self.enter_Root_connected()
		self.enterDefault_Root_connected_listening()
		self.enterDefault_Root_connected_sending()
		self.enterDefault_Root_connected_queueing()
		self.enterDefault_Root_connected_parsing()
	
	def enterDefault_Root_connected_listening(self):
		self.enter_Root_connected_listening()
		self.enter_Root_connected_listening_listen()
	
	def enterDefault_Root_connected_sending(self):
		self.enter_Root_connected_sending()
		self.enter_Root_connected_sending_waiting_for_data()
	
	def enterDefault_Root_connected_queueing(self):
		self.enter_Root_connected_queueing()
		self.enter_Root_connected_queueing_queueing()
	
	def enterDefault_Root_connected_parsing(self):
		self.enter_Root_connected_parsing()
		self.enter_Root_connected_parsing_wait_for_header()
	
	# Statechart transitions
	
	def generateCandidatesChildren_Root(self):
		if self.current_state[self.Root][0] == self.Root_init:
			return self.generateCandidates_Root_init()
		elif self.current_state[self.Root][0] == self.Root_connected:
			return self.generateCandidates_Root_connected()
		elif self.current_state[self.Root][0] == self.Root_close:
			return self.generateCandidates_Root_close()
		return False
	
	def generateCandidates_Root(self):
		if not self.combo_step.isArenaChanged(self.Root):
			return self.generateCandidatesChildren_Root()
		else:
			return True
	
	def generateCandidatesCurrent_Root_init(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "set_association_name":
				self.small_step.addCandidate(self.transition_Root_init_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_init(self):
		if not self.combo_step.isArenaChanged(self.Root_init):
			return self.generateCandidatesCurrent_Root_init()
		else:
			return True
	
	def transition_Root_init_1(self, parameters):
		association_name = parameters[0]
		self.exit_Root_init()
		self.association_name = association_name
		self.combo_step.setArenaChanged(self.Root)
		self.enterDefault_Root_connected()
	
	def generateCandidatesChildren_Root_connected(self):
		branch_done = False
		branch_done = (self.generateCandidates_Root_connected_listening() or branch_done)
		branch_done = (self.generateCandidates_Root_connected_sending() or branch_done)
		branch_done = (self.generateCandidates_Root_connected_queueing() or branch_done)
		branch_done = (self.generateCandidates_Root_connected_parsing() or branch_done)
		return branch_done
	
	def generateCandidatesCurrent_Root_connected(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "close":
				self.small_step.addCandidate(self.transition_Root_connected_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_connected(self):
		if not self.combo_step.isArenaChanged(self.Root_connected):
			branch_done = False
			if self.semantics.priority == StatechartSemantics.SourceParent:
				branch_done = self.generateCandidatesCurrent_Root_connected()
				if not branch_done:
					branch_done = self.generateCandidatesChildren_Root_connected()
			elif self.semantics.priority == StatechartSemantics.SourceChild:
				branch_done = self.generateCandidatesChildren_Root_connected()
				if not branch_done:
					branch_done = self.generateCandidatesCurrent_Root_connected()
			return branch_done
		else:
			return True
	
	def transition_Root_connected_1(self, parameters):
		self.exit_Root_connected()
		self.combo_step.setArenaChanged(self.Root)
		self.enter_Root_close()
	
	def generateCandidatesChildren_Root_connected_listening(self):
		if self.current_state[self.Root_connected_listening][0] == self.Root_connected_listening_listen:
			return self.generateCandidates_Root_connected_listening_listen()
		elif self.current_state[self.Root_connected_listening][0] == self.Root_connected_listening_closed:
			return self.generateCandidates_Root_connected_listening_closed()
		return False
	
	def generateCandidates_Root_connected_listening(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_listening):
			return self.generateCandidatesChildren_Root_connected_listening()
		else:
			return True
	
	def generateCandidatesCurrent_Root_connected_listening_listen(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "received_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				data = parameters[1]
				if (self.socket == socket) and (len(data) > 0):
					self.small_step.addCandidate(self.transition_Root_connected_listening_listen_1, e.parameters)
					return True
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "received_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				data = parameters[1]
				if (self.socket == socket) and (len(data) == 0):
					self.small_step.addCandidate(self.transition_Root_connected_listening_listen_2, e.parameters)
					return True
		return False
	
	def generateCandidates_Root_connected_listening_listen(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_listening_listen):
			return self.generateCandidatesCurrent_Root_connected_listening_listen()
		else:
			return True
	
	def transition_Root_connected_listening_listen_1(self, parameters):
		socket = parameters[0]
		data = parameters[1]
		self.exit_Root_connected_listening_listen()
		self.received_data += data
		self.raiseInternalEvent(Event("received_data", None, []))
		self.combo_step.setArenaChanged(self.Root_connected_listening)
		self.enter_Root_connected_listening_listen()
	
	def transition_Root_connected_listening_listen_2(self, parameters):
		socket = parameters[0]
		data = parameters[1]
		self.exit_Root_connected_listening_listen()
		self.raiseInternalEvent(Event("received_data", None, []))
		self.combo_step.setArenaChanged(self.Root_connected_listening)
		self.enter_Root_connected_listening_closed()
	
	def generateCandidates_Root_connected_listening_closed(self):
		return False
	
	def generateCandidatesChildren_Root_connected_sending(self):
		if self.current_state[self.Root_connected_sending][0] == self.Root_connected_sending_waiting_for_data:
			return self.generateCandidates_Root_connected_sending_waiting_for_data()
		elif self.current_state[self.Root_connected_sending][0] == self.Root_connected_sending_transferring:
			return self.generateCandidates_Root_connected_sending_transferring()
		return False
	
	def generateCandidates_Root_connected_sending(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_sending):
			return self.generateCandidatesChildren_Root_connected_sending()
		else:
			return True
	
	def generateCandidatesCurrent_Root_connected_sending_waiting_for_data(self):
		enabled_events = self.getEnabledEvents()
		if len(self.send_data) > 0:
			self.small_step.addCandidate(self.transition_Root_connected_sending_waiting_for_data_1, [])
			return True
		return False
	
	def generateCandidates_Root_connected_sending_waiting_for_data(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_sending_waiting_for_data):
			return self.generateCandidatesCurrent_Root_connected_sending_waiting_for_data()
		else:
			return True
	
	def transition_Root_connected_sending_waiting_for_data_1(self, parameters):
		self.exit_Root_connected_sending_waiting_for_data()
		self.big_step.outputEvent(Event("send_socket", "socket_out", [self.socket, self.send_data]))
		self.combo_step.setArenaChanged(self.Root_connected_sending)
		self.enter_Root_connected_sending_transferring()
	
	def generateCandidatesCurrent_Root_connected_sending_transferring(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if (e.name == "sent_socket") and (e.port == "socket_in"):
				parameters = e.parameters
				socket = parameters[0]
				sent_bytes = parameters[1]
				if self.socket == socket:
					self.small_step.addCandidate(self.transition_Root_connected_sending_transferring_1, e.parameters)
					return True
		return False
	
	def generateCandidates_Root_connected_sending_transferring(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_sending_transferring):
			return self.generateCandidatesCurrent_Root_connected_sending_transferring()
		else:
			return True
	
	def transition_Root_connected_sending_transferring_1(self, parameters):
		socket = parameters[0]
		sent_bytes = parameters[1]
		self.exit_Root_connected_sending_transferring()
		self.send_data = self.send_data[sent_bytes:]
		self.combo_step.setArenaChanged(self.Root_connected_sending)
		self.enter_Root_connected_sending_waiting_for_data()
	
	def generateCandidatesChildren_Root_connected_queueing(self):
		if self.current_state[self.Root_connected_queueing][0] == self.Root_connected_queueing_queueing:
			return self.generateCandidates_Root_connected_queueing_queueing()
		return False
	
	def generateCandidates_Root_connected_queueing(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_queueing):
			return self.generateCandidatesChildren_Root_connected_queueing()
		else:
			return True
	
	def generateCandidatesCurrent_Root_connected_queueing_queueing(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "HTTP_input":
				self.small_step.addCandidate(self.transition_Root_connected_queueing_queueing_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_connected_queueing_queueing(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_queueing_queueing):
			return self.generateCandidatesCurrent_Root_connected_queueing_queueing()
		else:
			return True
	
	def transition_Root_connected_queueing_queueing_1(self, parameters):
		data = parameters[0]
		self.exit_Root_connected_queueing_queueing()
		post_data = "&".join(["%s=%s" % (k, v) for k, v in data.iteritems()])
		self.send_data += "HTTP/1.0 200 OK\r\n"
		self.send_data += "Content-Length: %s\r\n" % len(post_data)
		self.send_data += "Content-Type: %s; charset=UTF-8\r\n" % "text/plain"
		self.send_data += "\r\n"
		self.send_data += str(post_data)
		self.combo_step.setArenaChanged(self.Root_connected_queueing)
		self.enter_Root_connected_queueing_queueing()
	
	def generateCandidatesChildren_Root_connected_parsing(self):
		if self.current_state[self.Root_connected_parsing][0] == self.Root_connected_parsing_wait_for_header:
			return self.generateCandidates_Root_connected_parsing_wait_for_header()
		elif self.current_state[self.Root_connected_parsing][0] == self.Root_connected_parsing_closing:
			return self.generateCandidates_Root_connected_parsing_closing()
		elif self.current_state[self.Root_connected_parsing][0] == self.Root_connected_parsing_wait_for_payload:
			return self.generateCandidates_Root_connected_parsing_wait_for_payload()
		return False
	
	def generateCandidates_Root_connected_parsing(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_parsing):
			return self.generateCandidatesChildren_Root_connected_parsing()
		else:
			return True
	
	def generateCandidatesCurrent_Root_connected_parsing_wait_for_header(self):
		enabled_events = self.getEnabledEvents()
		if '\r\n\r\n' in self.received_data and self.received_data.startswith('POST'):
			self.small_step.addCandidate(self.transition_Root_connected_parsing_wait_for_header_1, [])
			return True
		enabled_events = self.getEnabledEvents()
		if self.closed and len(self.received_data) == 0:
			self.small_step.addCandidate(self.transition_Root_connected_parsing_wait_for_header_2, [])
			return True
		return False
	
	def generateCandidates_Root_connected_parsing_wait_for_header(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_parsing_wait_for_header):
			return self.generateCandidatesCurrent_Root_connected_parsing_wait_for_header()
		else:
			return True
	
	def transition_Root_connected_parsing_wait_for_header_1(self, parameters):
		self.exit_Root_connected_parsing_wait_for_header()
		header, self.received_data = self.received_data.split("\r\n\r\n", 1)
		header = header.lower()
		if "content-length" in header:
		    _, after = header.split("content-length:", 1)
		    after = after.split("\r\n", 1)[0]
		    after = after.strip()
		    self.length = int(after)
		else:
		    self.length = float('inf')
		self.combo_step.setArenaChanged(self.Root_connected_parsing)
		self.enter_Root_connected_parsing_wait_for_payload()
	
	def transition_Root_connected_parsing_wait_for_header_2(self, parameters):
		self.exit_Root_connected_parsing_wait_for_header()
		self.combo_step.setArenaChanged(self.Root_connected_parsing)
		self.enter_Root_connected_parsing_closing()
	
	def generateCandidatesCurrent_Root_connected_parsing_closing(self):
		enabled_events = self.getEnabledEvents()
		for e in enabled_events:
			if e.name == "_0after":
				self.small_step.addCandidate(self.transition_Root_connected_parsing_closing_1, e.parameters)
				return True
		return False
	
	def generateCandidates_Root_connected_parsing_closing(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_parsing_closing):
			return self.generateCandidatesCurrent_Root_connected_parsing_closing()
		else:
			return True
	
	def transition_Root_connected_parsing_closing_1(self, parameters):
		self.exit_Root_connected_parsing_closing()
		self.raiseInternalEvent(Event("close", None, []))
		self.combo_step.setArenaChanged(self.Root_connected_parsing)
		self.enter_Root_connected_parsing_closing()
	
	def generateCandidatesCurrent_Root_connected_parsing_wait_for_payload(self):
		enabled_events = self.getEnabledEvents()
		if len(self.received_data) >= self.length or self.closed:
			self.small_step.addCandidate(self.transition_Root_connected_parsing_wait_for_payload_1, [])
			return True
		return False
	
	def generateCandidates_Root_connected_parsing_wait_for_payload(self):
		if not self.combo_step.isArenaChanged(self.Root_connected_parsing_wait_for_payload):
			return self.generateCandidatesCurrent_Root_connected_parsing_wait_for_payload()
		else:
			return True
	
	def transition_Root_connected_parsing_wait_for_payload_1(self, parameters):
		self.exit_Root_connected_parsing_wait_for_payload()
		if self.length == float('inf'):
		    data = self.received_data
		    self.received_data = ""
		else:
		    data = self.received_data[:self.length]
		    self.received_data = self.received_data[self.length:]
		
		# We support POST data only, so everything is in the data
		try:
		    params = dict([p.split('=') for p in data.split('&')])
		    data = {k: urllib.unquote_plus(v) for k, v in params.iteritems()}
		except:
		    import traceback
		    traceback.print_exc()
		    print("Could not parse: " + str(data))
		    data = {}
		self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("HTTP_output", None, [self.association_name, data])]))
		self.combo_step.setArenaChanged(self.Root_connected_parsing)
		self.enter_Root_connected_parsing_wait_for_header()
	
	def generateCandidates_Root_close(self):
		return False
	
	# Generate transition candidates for current small step
	def generateCandidates(self):
		self.generateCandidates_Root()

class ObjectManager(ObjectManagerBase):
	def __init__(self, controller):
		ObjectManagerBase.__init__(self, controller)
	
	def instantiate(self, class_name, construct_params):
		if class_name == "Server":
			instance = Server(self.controller, construct_params[0])
			instance.associations = {}
			instance.associations["sockets"] = Association("Socket", 0, -1)
		elif class_name == "Socket":
			instance = Socket(self.controller, construct_params[0])
			instance.associations = {}
			instance.associations["parent"] = Association("Server", 1, 1)
		return instance

class Controller(ThreadsControllerBase):
	def __init__(self, bootfile, keep_running = None):
		if keep_running == None: keep_running = True
		ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
		self.addInputPort("socket_in")
		self.addOutputPort("socket_out")
		self.object_manager.createInstance("Server", [bootfile])