OrocosComponentLibrary  2.9.0
rttlua.cpp
1 /*
2  * Lua-RTT bindings. LuaComponent.
3  *
4  * (C) Copyright 2010 Markus Klotzbuecher
5  * markus.klotzbuecher@mech.kuleuven.be
6  * Department of Mechanical Engineering,
7  * Katholieke Universiteit Leuven, Belgium.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation;
12  * version 2 of the License.
13  *
14  * As a special exception, you may use this file as part of a free
15  * software library without restriction. Specifically, if other files
16  * instantiate templates or use macros or inline functions from this
17  * file, or you compile this file and link it with other files to
18  * produce an executable, this file does not by itself cause the
19  * resulting executable to be covered by the GNU General Public
20  * License. This exception does not however invalidate any other
21  * reasons why the executable file might be covered by the GNU General
22  * Public License.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27  * Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public
30  * License along with this library; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place,
32  * Suite 330, Boston, MA 02111-1307 USA
33  */
34 
35 #include <rtt/rtt-config.h>
36 #ifdef OS_RT_MALLOC
37 // need access to all TLSF functions embedded in RTT
38 #define ORO_MEMORY_POOL
39 #include <rtt/os/tlsf/tlsf.h>
40 #endif
41 #include <rtt/os/main.h>
42 #include <rtt/RTT.hpp>
43 #include <rtt/Logger.hpp>
44 #ifdef ORO_BUILD_LOGGING
45 # ifndef OS_RT_MALLOC
46 # warning "Logging needs rtalloc!"
47 # endif
48 #include <log4cpp/HierarchyMaintainer.hh>
49 #include "logging/Category.hpp"
50 #endif
51 
52 extern "C" {
53 #include "lua-repl.h"
54 void dotty (lua_State *L);
55 void l_message (const char *pname, const char *msg);
56 int dofile (lua_State *L, const char *name);
57 int dostring (lua_State *L, const char *s, const char *name);
58 int main_args(lua_State *L, int argc, char **argv);
59 
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63 #include <wordexp.h>
64 }
65 
66 #include "LuaComponent.hpp"
67 #include <rtt/os/main.h>
68 //#include <rtt/os/Mutex.hpp>
69 //#include <rtt/os/MutexLock.hpp>
70 //#include <rtt/TaskContext.hpp>
71 //#include <ocl/OCL.hpp>
72 #if defined(LUA_RTT_CORBA)
73 #include <rtt/transports/corba/TaskContextServer.hpp>
74 #include <deployment/CorbaDeploymentComponent.hpp>
75 #else
76 #include <deployment/DeploymentComponent.hpp>
77 #endif
78 
79 #ifdef LUA_RTT_TLSF
80 #define LuaComponent LuaTLSFComponent
81 #else
82 #define LuaComponent LuaComponent
83 #endif
84 
85 #define INIT_FILE "~/.rttlua"
86 
87 using namespace std;
88 using namespace RTT;
89 using namespace OCL;
90 #if defined(LUA_RTT_CORBA)
91 using namespace RTT::corba;
92 #endif
93 
94 int ORO_main(int argc, char** argv)
95 {
96  struct stat stb;
97  wordexp_t init_exp;
98 
99 #ifdef ORO_BUILD_RTALLOC
100  size_t memSize = ORO_DEFAULT_RTALLOC_SIZE;
101  void* rtMem = 0;
102  size_t freeMem = 0;
103  if (0 < memSize)
104  {
105  // don't calloc() as is first thing TLSF does.
106  rtMem = malloc(memSize);
107  assert(0 != rtMem);
108  freeMem = init_memory_pool(memSize, rtMem);
109  if ((size_t)-1 == freeMem)
110  {
111  cerr << "Invalid memory pool size of " << memSize
112  << " bytes (TLSF has a several kilobyte overhead)." << endl;
113  free(rtMem);
114  return -1;
115  }
116  cout << "Real-time memory: " << freeMem << " bytes free of "
117  << memSize << " allocated." << endl;
118  }
119 #endif // ORO_BUILD_RTALLOC
120 
121 #ifdef ORO_BUILD_LOGGING
122  log4cpp::HierarchyMaintainer::set_category_factory(
124 #endif
125 
126  LuaComponent lua("lua");
127  DeploymentComponent * dc = 0;
128 
129 #if defined(LUA_RTT_CORBA)
130  int orb_argc = argc;
131  char** orb_argv = 0;
132  char* orb_sep = 0;
133 
134  /* find the "--" separator */
135  while(orb_argc) {
136  if(0 == strcmp("--", argv[argc - orb_argc])) {
137  orb_sep = argv[argc - orb_argc];
138  argv[argc - orb_argc] = argv[0];
139  orb_argv = &argv[argc - orb_argc];
140  argc -= orb_argc;
141  break;
142  }
143  orb_argc--;
144  }
145 
146  /* if the "--" separator is found perhaps we have orb arguments */
147  if(orb_argc) {
148  try {
149  TaskContextServer::InitOrb(orb_argc, orb_argv);
150 
151  dc = new CorbaDeploymentComponent("Deployer");
152 
153  TaskContextServer::Create( dc, true, true );
154 
155  // The orb thread accepts incomming CORBA calls.
156  TaskContextServer::ThreadOrb();
157  }
158  catch( CORBA::Exception &e ) {
159  log(Error) << argv[0] <<" ORO_main : CORBA exception raised!" << Logger::nl;
160  log() << CORBA_EXCEPTION_INFO(e) << endlog();
161  if(dc)
162  {
163  delete dc;
164  dc = 0;
165  }
166  } catch (...) {
167  log(Error) << "Uncaught exception." << endlog();
168  if(dc)
169  {
170  delete dc;
171  dc = 0;
172  }
173  }
174 
175  argv[argc] = dc?NULL:orb_sep;
176  }
177 
178  /* fallback to the default deployer if corba have failed to provide one */
179  if(!dc)
180 #endif
181  dc = new DeploymentComponent("Deployer");
182 
183  lua.connectPeers(dc);
184 
185  /* run init file */
186  wordexp(INIT_FILE, &init_exp, 0);
187  if(stat(init_exp.we_wordv[0], &stb) != -1) {
188  if((stb.st_mode & S_IFMT) != S_IFREG)
189  cout << "rttlua: warning: init file " << init_exp.we_wordv[0] << " is not a regular file" << endl;
190  else
191  lua.exec_file(init_exp.we_wordv[0]);
192  }
193  wordfree(&init_exp);
194 
195  main_args(lua.getLuaState().get(), argc, argv);
196 
197 #if defined(LUA_RTT_CORBA)
198  if(orb_argc) {
199  TaskContextServer::ShutdownOrb();
200  TaskContextServer::DestroyOrb();
201  }
202 #endif
203 
204  delete dc;
205 
206 #ifdef ORO_BUILD_LOGGING
207  log4cpp::HierarchyMaintainer::getDefaultMaintainer().shutdown();
208  log4cpp::HierarchyMaintainer::getDefaultMaintainer().deleteAllCategories();
209 #endif
210 
211 #ifdef ORO_BUILD_RTALLOC
212  if (0 != rtMem)
213  {
214  std::cout << "TLSF bytes allocated=" << memSize
215  << " overhead=" << (memSize - freeMem)
216  << " max-used=" << get_max_size(rtMem)
217  << " currently-used=" << get_used_size(rtMem)
218  << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
219  << "\n";
220 
221  destroy_memory_pool(rtMem);
222  free(rtMem);
223  }
224 #endif
225  return 0;
226 }
static log4cpp::Category * createOCLCategory(const std::string &name, log4cpp::Category *parent, log4cpp::Priority::Value priority)
Factory function for log4cpp::HierarchyMaintainer Creates an OCL logging category.
Definition: Category.cpp:137
STL namespace.
The Orocos Component Library.
Definition: Component.hpp:43
A Component for deploying (configuring) other components in an application.
Definition: Category.hpp:10