Просмотр исходного кода

improve console (a little), needs additional work

mikymod 12 лет назад
Родитель
Сommit
6450bcd55b
5 измененных файлов с 179 добавлено и 85 удалено
  1. 60 49
      src/ConsoleServer.cpp
  2. 35 16
      src/ConsoleServer.h
  3. 61 6
      src/lua/LuaEnvironment.cpp
  4. 11 4
      src/lua/LuaEnvironment.h
  5. 12 10
      tools/gui/console/console.py

+ 60 - 49
src/ConsoleServer.cpp

@@ -1,3 +1,29 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #include "ConsoleServer.h"
 #include "Log.h"
 #include "StringUtils.h"
@@ -10,31 +36,28 @@
 namespace crown
 {
 
-static IntSetting g_port("console_port", "port used by console", 10000, 9999, 65535);
+static IntSetting g_read_port("read_port", "port used for reading", 10000, 9999, 65535);
+static IntSetting g_write_port("write_port", "port used for writing", 10001, 9999, 65535);
+
 
 //-----------------------------------------------------------------------------
-const char* ConsoleServer::init_console = 	"cmd = {}; "
+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 "
+											"				cmd = cmd .. 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() :
 	m_active(false),
-	m_count(0),
 	m_thread(ConsoleServer::background_thread, (void*)this, "console-thread")
 {
 }
@@ -42,9 +65,10 @@ ConsoleServer::ConsoleServer() :
 //-----------------------------------------------------------------------------
 void ConsoleServer::init()
 {
+	LuaEnvironment* lua = device()->lua_environment();
 
-	device()->lua_environment()->load_buffer(init_console, string::strlen(init_console));
-	device()->lua_environment()->execute(0, 0);
+	lua->load_buffer(init_console, string::strlen(init_console));
+	lua->execute(0, 0);
 
 	m_active = true;
 }
@@ -58,52 +82,52 @@ void ConsoleServer::shutdown()
 //-----------------------------------------------------------------------------
 void ConsoleServer::read_eval_loop()
 {
-	m_socket.open(g_port);
+	m_socket.open(g_read_port);
 
-	Log::i("In read-eval loop");
+	LuaEnvironment* lua = device()->lua_environment();
 
-	char tmp[1024];
+	char cmd[1024];
 
 	while (m_active)
 	{
-		receive((char*)tmp, 1024);
+		// FIXME: send response of previous command
+		if (lua->status())
+		{
+			const char* tmp = lua->error();
+			send((char*)tmp, 1024);
+		}
+
+		receive((char*)cmd, 1024);
 
-		Log::i("Received: %s", tmp);
-		// FIXME: parse and then fill m_buffer
+		// FIXME: parse and then fill m_cmd_buffer
 
-		add_command(tmp);
+		string::strcpy(m_cmd_buffer, cmd);
 	}
 
 	m_socket.close();
 }
 
 //-----------------------------------------------------------------------------
-void ConsoleServer::add_command(const char* cmd)
+void ConsoleServer::execute()
 {
 	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);
+	LuaEnvironment* lua = device()->lua_environment();
 
-	++m_count;
+	lua->load_buffer(m_cmd_buffer, string::strlen(m_cmd_buffer));
+	lua->execute(0, 0);
+	// Reset cmd buffer
+	m_cmd_buffer[0] = '\0';
 
-	m_command_mutex.unlock();
-}
-
-//-----------------------------------------------------------------------------
-void ConsoleServer::execute()
-{
-	m_command_mutex.lock();
-	for (uint32_t i = 0; i < m_count; i++)
+	// If LuaEnvironment status is false
+	if (!lua->status())
 	{
-		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, "");
+
+		const char* tmp = lua->error();
+
+		// Send error to client
 	}
 
-	m_count = 0;
 	m_command_mutex.unlock();
 }
 
@@ -121,19 +145,6 @@ void ConsoleServer::receive(char* data, size_t 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)
 {
@@ -141,4 +152,4 @@ void* ConsoleServer::background_thread(void* thiz)
 }
 
 
-}
+} // namespace crown

+ 35 - 16
src/ConsoleServer.h

@@ -1,3 +1,31 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
 #include "Types.h"
 #include "TCPSocket.h"
 #include "Thread.h"
@@ -22,42 +50,33 @@ public:
 	void					init();
 	/// Stop listening
 	void					shutdown();
-
+	/// Read-evaluation loop, executed on a different thread
 	void					read_eval_loop();
-
-	void					add_command(const char* cmd);
-
+	/// Execute commands, executed on main thread
 	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(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::Thread				m_thread;
 	os::Mutex				m_command_mutex;
 
+	// Is console active?
+	bool					m_active;
+	// Commands buffer
+	char					m_cmd_buffer[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

+ 61 - 6
src/lua/LuaEnvironment.cpp

@@ -35,7 +35,8 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 LuaEnvironment::LuaEnvironment(lua_State* L) :
-	m_stack(L)
+	m_stack(L),
+	m_status(false)
 {
 	// Open Lua default libraries
 	luaL_openlibs(m_stack.state());
@@ -60,22 +61,63 @@ LuaStack LuaEnvironment::stack()
 	return m_stack;
 }
 
+//-----------------------------------------------------------------------------
+const char* LuaEnvironment::error()
+{
+	if (string::strlen(m_error_buffer) > 0)
+	{
+		char* tmp = m_error_buffer;
+
+		m_error_buffer[0] = '\0';
+
+		m_status = false;
+
+		return tmp;
+	}
+	else
+	{
+		return NULL;
+	}
+}
+
+//-----------------------------------------------------------------------------
+const bool LuaEnvironment::status()
+{
+	return m_status;
+}
+
+
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_buffer(const char* buffer, size_t len)
 {
-	luaL_loadbuffer(m_stack.state(), buffer, len, "");
+	int32_t loaded = luaL_loadbuffer(m_stack.state(), buffer, len, "");
+
+	if (loaded != 0)
+	{
+		lua_error();
+	}
 }
 
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_file(const char* file)
 {
-	luaL_loadfile(m_stack.state(), file);
+	int32_t loaded = luaL_loadfile(m_stack.state(), file);
+
+	if (loaded != 0)
+	{
+		lua_error();
+	}
 }
 
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_string(const char* str)
 {
-	luaL_loadstring(m_stack.state(), str);
+	int32_t loaded = luaL_loadstring(m_stack.state(), str);
+
+	if (loaded != 0)
+	{
+		lua_error();
+	}
 }
 
 //-----------------------------------------------------------------------------
@@ -87,7 +129,20 @@ void LuaEnvironment::get_global_symbol(const char* symbol)
 //-----------------------------------------------------------------------------
 void LuaEnvironment::execute(int32_t args, int32_t results)
 {
-	lua_pcall(m_stack.state(), args, results, 0);
+	int32_t executed = lua_pcall(m_stack.state(), args, results, 0);
+
+	if (executed != 0)
+	{
+		lua_error();
+	}
+}
+
+//-----------------------------------------------------------------------------
+void LuaEnvironment::lua_error()
+{
+	string::strcpy(m_error_buffer, lua_tostring(m_stack.state(), -1));
+
+	m_status = true;
 }
 
 //-----------------------------------------------------------------------------
@@ -126,4 +181,4 @@ CE_EXPORT int32_t luaopen_libcrown(lua_State* L)
 	return 1;
 }
 
-} // namespace crown
+} // namespace crown

+ 11 - 4
src/lua/LuaEnvironment.h

@@ -52,7 +52,9 @@ public:
 
 	LuaStack		stack();
 
-	const char*		error_buffer();
+	const char*		error();
+
+	const bool 		status();
 
 	void			init();
 
@@ -73,15 +75,20 @@ public:
 	/// Load a function to proper module
 	void			load_module_function(const char* module, const char* name, const lua_CFunction func);
 
+private:
+
+	void			lua_error();
+	// Disable copying
+					LuaEnvironment(const LuaEnvironment&);
+	LuaEnvironment& operator=(const LuaEnvironment&);
+
 private:
 
 	LuaStack		m_stack;
 
 	char			m_error_buffer[1024];
 
-	// Disable copying
-					LuaEnvironment(const LuaEnvironment&);
-	LuaEnvironment& operator=(const LuaEnvironment&);
+	bool			m_status;
 };
 
 void load_vec2(LuaEnvironment& env);

+ 12 - 10
tools/gui/console/console.py

@@ -8,10 +8,7 @@ from gi.repository import Gtk
 CMD_CLEAR   = "clear"   # Clear console output
 CMD_EXIT    = "exit"    # Close console
 CMD_HELP    = "help"    # Console help
-
-# Server Console commands
-CMD_STOP    = "device stop" # Stop Engine and close console
-CMD_FRAME   = "frame"
+CMD_VOID    = ""        
 
 # Help message
 MSG_HELP    =   "1- clear - clear screen output\n2- exit  - terminate console\n3- device stop - terminate engine and console\n4- help - print this message\n"
@@ -57,25 +54,30 @@ class Console:
             self.m_entry.set_text("")
 
         elif cmd == CMD_EXIT:
-            self.on_destroy()
-
-        elif cmd == CMD_STOP:
-            self.run_command(cmd)
-            self.popup_dialog("Crown has stopped!", "Console connection will be closed")
-            self.on_destroy()          
+            self.on_destroy()         
 
         elif cmd == CMD_HELP:
             self.print_help()
 
+        elif cmd == CMD_VOID:
+            self.print_command('');
+
         else:    
             self.run_command(cmd)        
 
 #------------------------------------------------------------------------------
     def run_command(self, cmd):
+        msg = ''
         # Send command to Crown
         self.m_sock.send(cmd.encode())
         self.print_command(cmd)
 
+        # Receive response
+        # msg = self.m_sock.recv(1024);
+        # print(msg.decode('utf-8'))
+        # if msg != "OK":
+        #     self.print_command(msg.decode('utf-8'))
+
 #------------------------------------------------------------------------------
     def print_command(self, cmd):
         # Print command to console