Lua HTTP Server + Native Agent

2009/06/29 21:12 | Posted by 아겔-_-

Lua HTTP Server + Native Agent

 

2009/06/29 00:52:49

 

 

native-agent의 새로운 아키텍쳐를 생각해냈다.

 

  1. Lua인터프리터만을 내장한 .exe가 실행하고 autoexec.lua을 실행화일이 위치한 디렉토리에서 실행.
  2. 필요한 lua extension은 모두 .exe와 함께 배포하고, autoexec.lua에서 로딩.
  3. 심지어 http서버도 extension으로 배포하고 autoexec.lua에서 시작

 

이렇게만 되면...

  • 이후에 native agent의 기능이 추가되더라도

    • lua extension만 c로 작성하고
    • 이걸 autoexec.lua에서 로드하고
  • 요청을 받은 lua 코드를 실행후 결과를 되돌리면 ok

 

단순히 ecm용 native agent가 아니라 재사용가능.

 

 

 

 

 

  • the Xavante Lua Web Server

 

Lua 5.1 custom interpreter build

인터프리터를 내장한 실행화일을 하나 만들기

 

MS Visual Studio 2008 / C++

 

충족할 조건은

  1. 시작후 같은 위치의 autoexec.lua을 평가할것

    1. 시작후 해당 위치로 chdir() (혹은 이후 로딩할 디렉토리를 지정 가능할까?)
  2. 같은 위치의 lua extension을 실행시간에 로딩할수있는 환경을 제공할것

 

프로토타입 프로젝트 세팅은

  1. 단순히 Win32 Console Application
  2. lua 5.1 소스에서 lua.c, luac.c등을 제외하고 헤더, 소스를 모두 프로젝트에 추가-_-;;;
  3. 별일 없이 바로 컴파일/링크 성공-_-b

 

 

__cdecl이 아니라서 lua_close을 링크 못한다네-_-;;; 이거 어떻하지 2009/06/29 01:35:38

http://terry51.egloos.com/998726

관련 lua.h등을 #include할때...

 

  1. extern "C" {
  2. #include "lua.h"
  3. #include "lauxlib.h"
  4. #include "lualib.h"
  5. }

 

extern "C"로 살포시 감싸주기

 

"luaL_dofile()"로 같은 디렉토리의 파일을 읽는건 성공한듯 (에러가 없는듯)

그런데 파일의 내용을 실행하지 못하는것으로 보이는데?;;; 2009/06/29 01:43:57

 

"luaL_openlibs()"을 호출해서 기본 라이브러리를 로드해야 하는구나.

  1. luaL_openlibs(L);

그럼에도 불구하고 어째서 Win32 + MSVC에서는 PANIC일까 2009/06/29 10:11:19

리눅스에서는 잘 굴러가는구만...

PANIC: unprotected error in call to Lua API (unable to get ModuleFileName)

 

http://blog.naver.com/PostView.nhn?blogId=sinerujin&logNo=60034089717

lua-shell_mbcs.png

VC++의 프로젝트 설정에서 유니코드에서 MBCS로 전환하니 또 되는군항-_- 2009/06/29 10:31:35

해결: lua 5.1에서 lua 5.1.4로 업그레이드 하니 문제가 없네-_-;;; 2009/06/29 10:42:51

 

 

lua-socket extension loading

http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/home.html

 

여기서 소스를 다운받고, dll로 빌드했다. 기존 lua-shell에서 lua부분을 static library로 빼서 공유하고, lua-shell은 소스 하나짜리인 프로젝트로 수정. 그래도 뭔가 실행시간에 로딩하는데 문제가 있는걸까.

 

lua-socket -> project(lua-socket) -> socket.dll

                -> project(lua-mime) -> mime.dll

 

 

에러가 있을때 에러내용을 알수있는 방법이 필요하다.

  1. static int report (lua_State *L, int status) {
  2.   if (status && !lua_isnil(L, -1)) {
  3.     const char *msg = lua_tostring(L, -1);
  4.     if (msg == NULL) msg = "(error object is not a string)";
  5.     l_message(progname, msg);
  6.     lua_pop(L, 1);
  7.   }
  8.   return status;
  9. }

뭔가 스택에서 에러와 관련한걸 꺼낸 모양이군. 이렇게 찍으니 다음처럼...

LUA-SHELL: error loading module 'socket.core' from file '.\socket\core.dll':

        지정된 프로시저를 찾을 수 없습니다.

--> 간단한 .lua 모듈을 만들어 테스트해보니 정상적으로 작동하는걸로봐서 socket.core가 잘못 컴파일된 모듈이구나... 2009/06/29 12:02:40

 

결국 loadlib.c:loader_C()함수에서 모든걸 처리하는걸 알았고, "msgbox" 모듈일 경우에 dll을 로드한 다음에 luaopen_msgbox_core()라는 함수를 호출해 함수등을 등록함을 알았다.

 

다음과 같은 dll을 작성했고

  1.  
  2. #include "stdafx.h"


  3. #include "lauxlib.h"

  4.   /* Pop-up a Windows message box with your choice of message and caption */
  5.   int lua_msgbox(lua_State* L)
  6.   {
  7.     const char* message = luaL_checkstring(L, 1);
  8.     const char* caption = luaL_optstring(L, 2, "");
  9.     int result = MessageBoxA(NULL, message, caption, MB_OK);
  10.     lua_pushnumber(L, result);
  11.     return 1;
  12.   }

  13.   int __declspec(dllexport) luaopen_msgbox_core (lua_State* L)
  14.   {
  15.    lua_register(L, "msgbox",  lua_msgbox);
  16.    return 0;
  17.   }

 

다음처럼 불러내니 성공

  1.  
  2. require("msgbox.core")
  3. msgbox("OH HAI!", "!!!")

 

이걸 socket, mime에도 적용해야지-_- 2009/06/29 20:57:36

 

결국 다른 함수는 둘째치고, luaopen_msgbox_core()함수에만 다음처럼 선언하면 되는거였음.

  1.   int __declspec(dllexport) luaopen_msgbox_core (lua_State* L)

(함수원형에라도...)

 

 

결국 lua-socket에서 mime, socket을 각각 분리해내고 빌드&실행성공 하악...

  1. -- socket
  2. socket = require("socket")
  3. print(socket._VERSION)

  4. -- mime
  5. mime = require("mime")
  6. print(mime._VERSION)

 

 

extension loading 규칙
  • .lua 소스는 같은 디렉토리의 lua/ 이후에

    • lua/socket.lua
  • .dll 확장은 조금씩 다르지만 그냥 같은 디렉토리에 예를 들어 socket.core 확장은

    • socket/core.dll

 

 

 

 

 

 

이 글은 스프링노트에서 작성되었습니다.

'삽질+돈되는짓' 카테고리의 다른 글

Lua HTTP Server + Native Agent  (6) 2009/06/29
IoC, DI, and Google Guice  (0) 2009/06/21
Maven 2와 Apache Ivy.  (0) 2009/05/23
System.currentTimeMillis()으로 실행시간 측정하지않기  (0) 2009/04/27
github을 시작했어요.  (0) 2009/02/13
dispatch에 관한 단상.  (3) 2009/01/18
이전 1 2 3 4 5 ... 69 다음