Skip to content

Commit

Permalink
initial commit for lua script object
Browse files Browse the repository at this point in the history
  • Loading branch information
jpswinski committed Jun 1, 2021
1 parent b13da71 commit 2385b99
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/core/CMakeLists.txt
Expand Up @@ -39,6 +39,7 @@ if (LUA_FOUND AND READLINE_LIB)
${CMAKE_CURRENT_LIST_DIR}/LuaLibrarySys.cpp
${CMAKE_CURRENT_LIST_DIR}/LuaLibraryTime.cpp
${CMAKE_CURRENT_LIST_DIR}/LuaObject.cpp
${CMAKE_CURRENT_LIST_DIR}/LuaScript.cpp
${CMAKE_CURRENT_LIST_DIR}/MathLib.cpp
${CMAKE_CURRENT_LIST_DIR}/MetricDispatch.cpp
${CMAKE_CURRENT_LIST_DIR}/MetricRecord.cpp
Expand Down Expand Up @@ -95,6 +96,7 @@ if (LUA_FOUND AND READLINE_LIB)
${CMAKE_CURRENT_LIST_DIR}/LuaLibrarySys.h
${CMAKE_CURRENT_LIST_DIR}/LuaLibraryTime.h
${CMAKE_CURRENT_LIST_DIR}/LuaObject.h
${CMAKE_CURRENT_LIST_DIR}/LuaScript.h
${CMAKE_CURRENT_LIST_DIR}/MathLib.h
${CMAKE_CURRENT_LIST_DIR}/MetricDispatch.h
${CMAKE_CURRENT_LIST_DIR}/MetricRecord.h
Expand Down
2 changes: 1 addition & 1 deletion packages/core/HttpClient.cpp
Expand Up @@ -72,7 +72,7 @@ int HttpClient::luaCreate (lua_State* L)
ip_addr = NULL;
}

/* Return File Device Object */
/* Return Http Client Object */
return createLuaObject(L, new HttpClient(L, ip_addr, port));
}
catch(const RunTimeException& e)
Expand Down
113 changes: 113 additions & 0 deletions packages/core/LuaScript.cpp
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2021, University of Washington
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the University of Washington nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON AND CONTRIBUTORS
* “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF WASHINGTON OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/******************************************************************************
* INCLUDES
******************************************************************************/


#include "LuaScript.h"
#include "LuaEngine.h"
#include "core.h"

/******************************************************************************
* STATIC DATA
******************************************************************************/

const char* LuaScript::OBJECT_TYPE = "LuaScript";
const char* LuaScript::LuaMetaName = "LuaScript";
const struct luaL_Reg LuaScript::LuaMetaTable[] = {
{"isactive", luaIsActive},
{NULL, NULL}
};

/******************************************************************************
* PUBLIC METHODS
******************************************************************************/

/*----------------------------------------------------------------------------
* luaCreate - script(<name>, <script>, <arg>)
*----------------------------------------------------------------------------*/
int LuaScript::luaCreate (lua_State* L)
{
try
{
/* Get Parameters */
const char* name = getLuaString(L, 1);
const char* script = getLuaString(L, 2);
const char* arg = getLuaString(L, 3, true, NULL);

/* Return Lua Script Object */
return createLuaObject(L, new LuaScript(L, name, script, arg));
}
catch(const RunTimeException& e)
{
mlog(CRITICAL, "Error creating LuaScript: %s", e.what());
return returnLuaStatus(L, false);
}
}

/*----------------------------------------------------------------------------
* Constructor
*----------------------------------------------------------------------------*/
LuaScript::LuaScript(lua_State* L, const char* name, const char* script, const char* arg):
LuaObject(L, OBJECT_TYPE, LuaMetaName, LuaMetaTable)
{
engine = new LuaEngine(name, script, arg, ORIGIN, NULL, false);
}

/*----------------------------------------------------------------------------
* Destructor
*----------------------------------------------------------------------------*/
LuaScript::~LuaScript(void)
{
delete engine;
}

/*----------------------------------------------------------------------------
* luaIsActive - :isactive()
*----------------------------------------------------------------------------*/
int LuaScript::luaIsActive (lua_State* L)
{
try
{
/* Get Self */
LuaScript* lua_obj = (LuaScript*)getLuaSelf(L, 1);

/* Return Is Active */
return returnLuaStatus(L, lua_obj->engine->isActive());
}
catch(const RunTimeException& e)
{
mlog(CRITICAL, "Error checking srcipt status: %s", e.what());
return returnLuaStatus(L, false);
}
}
90 changes: 90 additions & 0 deletions packages/core/LuaScript.h
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2021, University of Washington
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the University of Washington nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON AND CONTRIBUTORS
* “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF WASHINGTON OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __lua_script__
#define __lua_script__

/******************************************************************************
* INCLUDES
******************************************************************************/

#include <atomic>

#include "MsgQ.h"
#include "OsApi.h"
#include "List.h"
#include "StringLib.h"
#include "LuaEngine.h"
#include "LuaObject.h"
#include "EndpointObject.h"

/******************************************************************************
* LUA SCRIPT CLASS
******************************************************************************/

class LuaScript: public LuaObject
{
public:

/*--------------------------------------------------------------------
* Constants
*--------------------------------------------------------------------*/

static const char* OBJECT_TYPE;
static const char* LuaMetaName;
static const struct luaL_Reg LuaMetaTable[];

/*--------------------------------------------------------------------
* Methods
*--------------------------------------------------------------------*/

static int luaCreate (lua_State* L);

LuaScript (lua_State* L, const char* name, const char* script, const char* arg);
~LuaScript (void);

private:


/*--------------------------------------------------------------------
* Data
*--------------------------------------------------------------------*/

LuaEngine* engine;

/*--------------------------------------------------------------------
* Methods
*--------------------------------------------------------------------*/

static int luaIsActive (lua_State* L);
};

#endif /* __lua_script__ */
1 change: 1 addition & 0 deletions packages/core/core.cpp
Expand Up @@ -72,6 +72,7 @@ static int core_open (lua_State *L)
{
static const struct luaL_Reg core_functions[] = {
{"getbyname", LuaObject::luaGetByName},
{"script", LuaScript::luaCreate},
{"monitor", Monitor::luaCreate},
{"cluster", ClusterSocket::luaCreate},
{"file", File::luaCreate},
Expand Down
1 change: 1 addition & 0 deletions packages/core/core.h
Expand Up @@ -64,6 +64,7 @@
#include "LuaLibrarySys.h"
#include "LuaLibraryTime.h"
#include "LuaObject.h"
#include "LuaScript.h"
#include "MathLib.h"
#include "MetricDispatch.h"
#include "MetricRecord.h"
Expand Down

0 comments on commit 2385b99

Please sign in to comment.