Forráskód Böngészése

run ConsoleServer on a different thread, lot of changes

mikymod 12 éve
szülő
commit
88d7eee929
2 módosított fájl, 151 hozzáadás és 22 törlés
  1. 110 14
      src/ConsoleServer.cpp
  2. 41 8
      src/ConsoleServer.h

+ 110 - 14
src/ConsoleServer.cpp

@@ -2,47 +2,143 @@
 #include "Log.h"
 #include "StringUtils.h"
 #include "Device.h"
+#include "LuaEnvironment.h"
+#include "StringUtils.h"
+#include "JSONParser.h"
+#include "IntSetting.h"
 
 namespace crown
 {
 
+static IntSetting g_port("console_port", "port used by console", 10000, 9999, 65535);
+
+//-----------------------------------------------------------------------------
+const char* ConsoleServer::init_console = 	"cmd = {}; "
+											"local i = 0; "
+											"for name,class in pairs(_G) do "
+											"	if type(class) == 'table' then "
+			 								"		for func_name,func in pairs(class) do "
+			 								"			if type(func) == 'function' then "
+			 								"				i = i + 1 "
+											"				cmd[i] = name .. '.' .. func_name "
+											"			end "
+											"		end "
+											"	end "
+											"end";
+
+//-----------------------------------------------------------------------------
+const char* ConsoleServer::retrieve_cmds = 	"for i,line in pairs(cmd) do "
+											"	print(i .. '- ' .. line) "
+											"end";
+
 //-----------------------------------------------------------------------------
-ConsoleServer::ConsoleServer()
+ConsoleServer::ConsoleServer() :
+	m_active(false),
+	m_count(0),
+	m_thread(ConsoleServer::background_thread, (void*)this, "console-thread")
 {
 }
 
 //-----------------------------------------------------------------------------
-void ConsoleServer::start(uint32_t port)
+void ConsoleServer::init()
 {
-	m_socket.open(port);
+
+	device()->lua_environment()->load_buffer(init_console, string::strlen(init_console));
+	device()->lua_environment()->execute(0, 0);
+
+	m_active = true;
 }
 
 //-----------------------------------------------------------------------------
-void ConsoleServer::stop()
+void ConsoleServer::shutdown()
 {
-	m_socket.close();
+	m_active = false;
 }
 
 //-----------------------------------------------------------------------------
-void ConsoleServer::receive_command()
+void ConsoleServer::read_eval_loop()
 {
-	// legge socket
-	int32_t bytes_read = m_socket.receive((void*)data, 1024);
-	if (bytes_read > 0)
+	m_socket.open(g_port);
+
+	Log::i("In read-eval loop");
+
+	char tmp[1024];
+
+	while (m_active)
 	{
-		parse_command();
+		receive((char*)tmp, 1024);
+
+		Log::i("Received: %s", tmp);
+		// FIXME: parse and then fill m_buffer
+
+		add_command(tmp);
 	}
+
+	m_socket.close();
 }
 
 //-----------------------------------------------------------------------------
-void ConsoleServer::parse_command()
+void ConsoleServer::add_command(const char* cmd)
 {
-	char* command = (char*)data;
+	m_command_mutex.lock();
+
+	Log::i("Pussy: %s, len: %d", cmd, string::strlen(cmd));
+	string::strcpy(m_buffer[m_count].command, cmd);
+	Log::i(m_buffer[m_count].command);
+
+	++m_count;
 
-	if (string::strcmp(command, "device stop") == 0)
+	m_command_mutex.unlock();
+}
+
+//-----------------------------------------------------------------------------
+void ConsoleServer::execute()
+{
+	m_command_mutex.lock();
+	for (uint32_t i = 0; i < m_count; i++)
 	{
-		device()->stop();
+		Log::i("command: %s, size: %d", m_buffer[i].command, string::strlen(m_buffer[i].command));
+		device()->lua_environment()->load_buffer(m_buffer[i].command, string::strlen(m_buffer[i].command));
+		device()->lua_environment()->execute(0, 0);
+		string::strcpy(m_buffer[i].command, "");
 	}
+
+	m_count = 0;
+	m_command_mutex.unlock();
 }
 
+//-----------------------------------------------------------------------------
+void ConsoleServer::send(const void* data, size_t size)
+{
+	bool sent = m_socket.send(data, size);
+}
+
+//-----------------------------------------------------------------------------
+void ConsoleServer::receive(char* data, size_t size)
+{
+	int32_t bytes_read = m_socket.receive(data, size);
+
+	// FIX: parse json (JSONParser needs rework)
+}
+
+//-----------------------------------------------------------------------------
+void ConsoleServer::parse_command(const uint8_t* data)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+void ConsoleServer::command_list()
+{
+	device()->lua_environment()->load_buffer(retrieve_cmds, string::strlen(retrieve_cmds));
+	device()->lua_environment()->execute(0, 0);
+}
+
+//-----------------------------------------------------------------------------
+void* ConsoleServer::background_thread(void* thiz)
+{
+	((ConsoleServer*)thiz)->read_eval_loop();	
+}
+
+
 }

+ 41 - 8
src/ConsoleServer.h

@@ -2,29 +2,62 @@
 #include "TCPSocket.h"
 #include "Thread.h"
 #include "Mutex.h"
-#include "Cond.h"
 
 namespace crown
 {
 
+struct ConsoleCommand
+{
+	char	command[1024];
+};
+
+/// ConsoleServer runs scripts that provide some utilities for the console
 class ConsoleServer
 {
 public:
 
-					ConsoleServer();
+	/// Constructor
+							ConsoleServer();
+	/// Start listening on @port
+	void					init();
+	/// Stop listening
+	void					shutdown();
 
-	void			start(uint32_t port);
-	void			stop();
-	void			receive_command();
+	void					read_eval_loop();
 
+	void					add_command(const char* cmd);
+
+	void					execute();
+	/// Send data to client
+	void					send(const void* data, size_t size = 1024);
+	/// Receive data to client
+	void					receive(char* data, size_t size = 1024);
+	/// Return the list of command
+	void 					command_list();
 
 private:
 
-	void 			parse_command();
+	void 					parse_command(const uint8_t* cmd);
+
+	static void*			background_thread(void* thiz);
+
+private:
+
+	os::TCPSocket			m_socket;
+
+	bool					m_active;
+
+	uint32_t				m_count;
+
+	ConsoleCommand			m_buffer[16];
 
-	os::TCPSocket	m_socket;
+	os::Thread				m_thread;
+	os::Mutex				m_command_mutex;
 
-	uint8_t			data[1024];
+	/// Lua script which initializes ConsoleServer
+	static const char*		init_console;
+	/// Lua script which provides a list of all commands
+	static const char*		retrieve_cmds;
 };
 
 } // namespace crown