00001
00002
00003
00004
00005 #include <rtt/Service.hpp>
00006 #include <rtt/plugin/ServicePlugin.hpp>
00007 #include <iostream>
00008
00009 #include "../rtt.hpp"
00010
00011 using namespace RTT;
00012
00013 class LuaService : public Service
00014 {
00015 protected:
00016 lua_State *L;
00017 os::Mutex m;
00018
00019 public:
00020 LuaService(RTT::TaskContext* tc)
00021 : RTT::Service("Lua", tc)
00022 {
00023
00024 os::MutexLock lock(m);
00025
00026 L = lua_open();
00027 lua_gc(L, LUA_GCSTOP, 0);
00028 luaL_openlibs(L);
00029 lua_gc(L, LUA_GCRESTART, 0);
00030
00031 if (L == NULL) {
00032 Logger::log(Logger::Error) << "LuaService ctr '" << this->getOwner()->getName() << "': "
00033 << "cannot create state: not enough memory" << endlog();
00034 throw;
00035 }
00036
00037
00038 lua_pushcfunction(L, luaopen_rtt);
00039 lua_call(L, 0, 0);
00040
00041 set_context_tc(tc, L);
00042
00043 this->addOperation("exec_file", &LuaService::exec_file, this);
00044 this->addOperation("exec_str", &LuaService::exec_str, this);
00045 }
00046
00047 bool exec_file(const std::string &file)
00048 {
00049 os::MutexLock lock(m);
00050 if (luaL_dofile(L, file.c_str())) {
00051 Logger::log(Logger::Error) << "LuaService '" << this->getOwner()->getName()
00052 << "': " << lua_tostring(L, -1) << endlog();
00053 return false;
00054 }
00055 return true;
00056 }
00057
00058 bool exec_str(const std::string &str)
00059 {
00060 os::MutexLock lock(m);
00061 if (luaL_dostring(L, str.c_str())) {
00062 Logger::log(Logger::Error) << "LuaService '" << this->getOwner()->getName()
00063 << "': " << lua_tostring(L, -1) << endlog();
00064 return false;
00065 }
00066 return true;
00067 }
00068 };
00069
00070 ORO_SERVICE_NAMED_PLUGIN(LuaService , "Lua")