/// <summary> /// Print Current Automata State to Console /// </summary> /// <param name="state"></param> /// <returns></returns> private void debug_state(BackendMode state) { string ret_val = "State: "; switch (state) { case BackendMode.eInit: ret_val += "Init"; break; case BackendMode.eSetProblem: ret_val += "SetProblem"; break; case BackendMode.eSolve: ret_val += "SolveProblem"; break; case BackendMode.eGetSolution: ret_val += "GetSolution"; break; case BackendMode.eTerminate: ret_val += "Terminate"; break; default: throw new NotImplementedException(); } System.Console.WriteLine(ret_val); }
/// <summary> /// Resolve Next State for Next State Logic Automata /// </summary> /// <returns> /// Enumeration Type for Next State /// </returns> private BackendMode next_state() { byte[] data = this.receive(); string msg = System.Text.Encoding.ASCII.GetString(data); BackendMode ret_val = BackendMode.eInit; if (msg == INIT_TAG) { ret_val = BackendMode.eInit; } else if (msg == SET_PROBLEM_TAG) { ret_val = BackendMode.eSetProblem; } else if (msg == SOLVE_PROBLEM_TAG) { ret_val = BackendMode.eSolve; } else if (msg == GET_SOLUTION_TAG) { ret_val = BackendMode.eGetSolution; } else if (msg == TERMINATE_SERVER_TAG) { ret_val = BackendMode.eTerminate; } else { throw new NotImplementedException(msg); } return(ret_val); }
/// <summary> /// Constructor /// </summary> /// <param name="port"></param> public TCP_Server(int port) { m_port = port; m_server = new TcpListener(IPAddress.Any, m_port); m_state = BackendMode.eInit; m_traffic_in = new List <byte[]>(); m_pc = 0; }
/// <summary> /// Handle Incoming Requests /// </summary> public void handle_requests() { m_server.Start(); Console.WriteLine("Listening to port: {0}", m_port); Console.Write("Waiting for SMT_MacroPlacer to connect ... "); m_client = m_server.AcceptTcpClient(); Console.WriteLine("Connected!"); m_stream = m_client.GetStream(); bool done = false; while (true) { switch (m_state) { case BackendMode.eInit: // Chill dude this.debug_state(BackendMode.eInit); m_state = this.next_state(); break; case BackendMode.eSetProblem: this.debug_state(BackendMode.eSetProblem); byte[] msg = this.receive(); this.set_problem(msg); m_state = this.next_state(); break; case BackendMode.eSolve: this.debug_state(BackendMode.eSolve); this.solve(); m_state = this.next_state(); break; case BackendMode.eGetSolution: this.debug_state(BackendMode.eGetSolution); this.get_solution(); m_state = this.next_state(); break; case BackendMode.eTerminate: this.debug_state(BackendMode.eTerminate); this.terminate(); done = true; break; default: throw new NotImplementedException(); } if (done) { break; } } }