Changeset 736

Show
Ignore:
Timestamp:
06/21/09 05:07:51 (15 months ago)
Author:
ghoulsblade
Message:

irc: implemented ping, now works on quakenet. multithreading : started experimenting with boost-thread for a second lua state

Location:
trunk/lugre
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/lugre/include/lugre_scripting.h

    r565 r736  
    6868        bool    LuaCall         (const char *func, const char *sig = "", ...); 
    6969         
     70         
     71        void    InitLugreLuaEnvironment         (lua_State*     L); 
     72         
    7073        // cInputListener methods 
    7174        virtual void    Notify_KeyPress         (const unsigned char iKey,const int iLetter); 
  • trunk/lugre/include/lugre_thread.h

    r120 r736  
    3636class cThread_LoadFileImpl; 
    3737 
     38int             MyThreadSleepMilliSeconds               (int iSleepTimeMilliSeconds); 
     39void    LuaRegisterThreading                    (lua_State*     L); 
    3840 
    3941/// connects to host:port , sends senddata and collects answer until the other side closes the connection 
     
    8082 
    8183 
     84 
     85 
     86 
     87 
     88 
     89 
     90 
     91 
    8292}; 
    8393 
  • trunk/lugre/lua/lib.irc.lua

    r83 r736  
    3131        if not self:IsConnected() then return end 
    3232         
     33        print("DEBUG:IRC:send",">"..tostring(line).."<") 
     34         
    3335        local fifo = CreateFIFO() 
    3436        FIFO_PushPlainText(fifo,line .. "\r\n") 
     
    5860                local message = "" 
    5961                 
     62                 
    6063                if string.find(line," ",pos) then 
    6164                        sender = trim(string.sub(line,pos,string.find(line," ",pos))) 
     
    7881                end 
    7982                 
    80                 print("DEBUG",tostring(sender) .. "|" .. tostring(command) .. "|" .. tostring(param) .. "|" .. tostring(message)) 
    81                  
    8283                 
    8384                local nick = trim(sender,":") 
     
    9394                        self:OnMessage(nick,param,message) 
    9495                end 
     96                 
     97                 
     98                local a,b 
     99                local msg_prefix 
     100                local msg_command 
     101                local msg_params 
     102                if (string.find(line,"^:")) then  
     103                        a,b,msg_prefix,msg_command,msg_params = string.find(line,"^:([^ ]+) ([^ ]+) (.*)\r\n$") 
     104                else  
     105                        a,b,msg_command,msg_params = string.find(line,"([^ ]+) (.*)\r\n$") 
     106                end 
     107                 
     108                local RPL_MOTD = "372" 
     109                local RPL_UMODEIS = "221" 
     110 
     111                         
     112                --~ print("DEBUG",tostring(sender) .. "|" .. tostring(command) .. "|" .. tostring(param) .. "|" .. tostring(message)) 
     113                if (msg_command ~= RPL_MOTD) then  
     114                        --~ print("DEBUG:IRC",">"..tostring(msg_command).."<",">"..tostring(msg_params).."<",">"..tostring(string.gsub(line,"\r\n$","")).."<") 
     115                        print("DEBUG:IRC:recv",">"..tostring(string.gsub(line,"\r\n$","")).."<") 
     116                end 
     117                 
     118                if (msg_command == RPL_UMODEIS) then -- final login message on quakenet  
     119                        if (self.on_login_complete) then self:on_login_complete() end 
     120                end 
     121                if (msg_command == "PING") then 
     122                        local a,b,daemon = string.find(msg_params,"^:(.*)") 
     123                        if (not daemon) then daemon = tostring(msg_params) end 
     124                        -- quakenet sends  >PING :424867537<  so we just remove the : 
     125                        -- zw sends        >PING :zwischenwelt.org<  
     126                        print("IRC: got ping, sending pong.. daemon="..daemon) 
     127                        self:SendLine("PONG "..daemon) 
     128                end 
     129                -- DEBUG   PING|||zwischenwelt.org 
    95130 
    96131                -- read out next line 
  • trunk/lugre/src/lugre_scripting.cpp

    r723 r736  
    376376        L = lua_open();  
    377377        assert(L); 
     378        InitLugreLuaEnvironment(L); 
     379         
     380        cInput::RegisterListener(this); 
     381 
     382        int res = luaL_dofile(L,sLuaMainPath.c_str()); 
     383        if (res) { 
     384                fprintf(stderr,"%s\n",lua_tostring(L,-1)); 
     385                MyCrash("error in main script-initialisation\n"); 
     386                exit(-1);  
     387        } 
     388} 
     389 
     390 
     391 
     392void    cScripting::InitLugreLuaEnvironment             (lua_State*     L) { 
    378393        luaL_openlibs(L); 
    379394        //~ luaopen_base(L); 
     
    447462        { for (std::list<cScriptingPlugin*>::iterator itor=mlPlugins.begin();itor!=mlPlugins.end();++itor) 
    448463                (*itor)->RegisterLua_Classes(L); } 
    449                  
    450         cInput::RegisterListener(this); 
    451  
    452         res = luaL_dofile(L,sLuaMainPath.c_str()); 
    453         if (res) { 
    454                 fprintf(stderr,"%s\n",lua_tostring(L,-1)); 
    455                 MyCrash("error in main script-initialisation\n"); 
    456                 exit(-1);  
    457         } 
    458464} 
    459465 
  • trunk/lugre/src/lugre_scripting.general.cpp

    r715 r736  
    471471 
    472472 
     473         
    473474void    RegisterLua_General_Classes                     (lua_State*     L) { 
    474475        RegisterLuaXML(L); 
    475476        LuaRegisterFIFO(L); 
    476477        LuaRegisterNet(L); 
     478        LuaRegisterThreading(L); 
    477479        cGfx3D::LuaRegister(L); 
    478480        cGfx2D::LuaRegister(L); 
     
    482484        cSoundSource::LuaRegister(L); 
    483485        cRandom::LuaRegister(L); 
    484         cThread_NetRequest::LuaRegister(L); 
    485         cThread_LoadFile::LuaRegister(L); 
    486486        cTexAtlas::LuaRegister(L); 
    487487        cImage::LuaRegister(L); 
  • trunk/lugre/src/lugre_thread.cpp

    r120 r736  
    1717// warning ! starting a thread makes a COPY of the passed in functor-object, so you cannot access the original 
    1818// see http://boost.org/doc/html/boost/thread.html#id1291385-bb for details 
     19// http://www.boost.org/doc/libs/1_39_0/doc/html/thread.html 
    1920 
    2021// see also http://engineering.meta-comm.com/resources/cs-win32_1_30_2_metacomm/libs/thread/doc/thread.html 
     
    188189 
    189190 
     191 
    190192}; 
  • trunk/lugre/src/lugre_thread_L.cpp

    r120 r736  
    33#include "lugre_fifo.h" 
    44#include "lugre_luabind.h" 
     5#include "lugre_scripting.h" 
     6 
     7#ifdef ENABLE_THREADS 
     8#include <boost/thread/xtime.hpp> 
     9#include <boost/thread/thread.hpp> 
     10#include <boost/thread/mutex.hpp> 
     11#endif 
    512 
    613namespace Lugre { 
     
    111118}; 
    112119 
     120 
     121// result : 0:not supported,  1:success   2:interrupted 
     122int             MyThreadSleepMilliSeconds (int iSleepTimeMilliSeconds) { 
     123        #ifdef ENABLE_THREADS 
     124         
     125         
     126        /* 
     127        ancient boost version : 103401 
     128        boost::xtime xt; 
     129        boost::xtime_get(&xt, boost::TIME_UTC); 
     130        int big = 1000*1000*1000; 
     131        xt.sec += (iSleepTimeMilliSeconds / 1000); 
     132        while (xt.nsec > big) { xt.nsec -= big; xt.sec += 1; } 
     133        xt.nsec += (iSleepTimeMilliSeconds % 1000)*1000*1000; 
     134        while (xt.nsec > big) { xt.nsec -= big; xt.sec += 1; } 
     135        boost::thread::sleep(xt); 
     136        */ 
     137         
     138         
     139        //~ #else 
     140        //~ #define BOOST_VERSION 103401 -- ghoul : old : linux 
     141        //~ #define BOOST_VERSION 103700 -- ghoul : new 
     142        //~ #define BOOST_VERSION 103800 -- hagish:win 
     143        // #define BOOST_LIB_VERSION "1_34_1" 
     144                 
     145        try { 
     146                // boost::this_thread::sleep(system_time const& abs_time);       
     147                // boost::this_thread::sleep(TimeDuration const& rel_time); 
     148                boost::this_thread::sleep(boost::posix_time::milliseconds(iSleepTimeMilliSeconds)); 
     149        } catch (...) { 
     150                return 2; 
     151        } 
     152        return 1; 
     153        #else 
     154        return 0; 
     155        #endif 
     156} 
     157 
     158#ifdef ENABLE_THREADS 
     159 
     160class cLuaThread : public cSmartPointable { public: 
     161        cFIFO                   mFIFO; 
     162        boost::mutex    mMutex; 
     163        boost::thread*  mThread; 
     164        std::string             msFilePath; 
     165         
     166        cLuaThread      (std::string sFilePath); 
     167        virtual ~cLuaThread     () { if (mThread) { delete mThread; mThread = 0; } } 
     168         
     169        cFIFO&  GetFIFO         () { return mFIFO; } 
     170        void    LockMutex       () { mMutex.lock(); } 
     171        void    UnLockMutex     () { mMutex.unlock(); } 
     172        void    Interrupt       () { if (mThread) mThread->interrupt(); } 
     173}; 
     174 
     175class cLuaThread_Callable { public: // must be copyable 
     176        cLuaThread*             mSelfThreadHandle; 
     177        std::string             msFilePath; 
     178        cLuaThread_Callable (cLuaThread* mSelfThreadHandle,std::string msFilePath) : mSelfThreadHandle(mSelfThreadHandle),msFilePath(msFilePath) {} 
     179         
     180        static int      DontUseWarning_Client_Sleep     (lua_State *L) { printf("DontUseWarning_Client_Sleep\n"); return 0; } 
     181         
     182        // bool         Thread_Sleep    (iSleepTimeMilliSeconds)   // returns true if it was interrupted 
     183        static int      Thread_Sleep    (lua_State *L) {  
     184                int iSleepTimeMilliSeconds = luaL_checkint(L,1); 
     185                if (MyThreadSleepMilliSeconds(iSleepTimeMilliSeconds) == 2) { lua_pushboolean(L,true); return 1; } 
     186                return 0; 
     187        } 
     188                 
     189    void operator()() { 
     190                //~ msFilePath 
     191                //  boost::thread::sleep() 
     192                 
     193                                 
     194                lua_State* L = lua_open(); 
     195                if (!L) { printf("cLuaThread_Callable: failed to init lua state\n"); return; } 
     196                cScripting::GetSingletonPtr()->InitLugreLuaEnvironment(L); 
     197                 
     198                 
     199                lua_register(L,"Client_Sleep",  &cLuaThread_Callable::DontUseWarning_Client_Sleep); 
     200                lua_register(L,"Thread_Sleep",  &cLuaThread_Callable::Thread_Sleep); 
     201                 
     202                 
     203                cLuaBind<cLuaThread>::CreateUData(L,mSelfThreadHandle); 
     204                lua_setglobal(L,"this_thread"); 
     205                 
     206                 
     207                int res = luaL_dofile(L,msFilePath.c_str()); 
     208                if (res) { 
     209                        fprintf(stderr,"%s\n",lua_tostring(L,-1)); 
     210                        MyCrash("error during cLuaThread_Callable run\n"); 
     211                        exit(-1);  
     212                } 
     213        } 
     214}; 
     215 
     216 
     217 
     218cLuaThread::cLuaThread  (std::string sFilePath) : mThread(0),msFilePath(sFilePath) { 
     219        cLuaThread_Callable     myImpl(this,sFilePath); 
     220        mThread = new boost::thread(myImpl); // warning ! this COPIES the impl object 
     221} 
     222 
     223 
     224class cLuaThread_L : public cLuaBind<cLuaThread> { public: 
     225        // implementation of cLuaBind 
     226 
     227                /// called by Register(), registers object-methods (see cLuaBind constructor for examples) 
     228                virtual void RegisterMethods    (lua_State *L) { PROFILE 
     229                        #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cLuaThread_L::methodname)); 
     230 
     231                        REGISTER_METHOD(Destroy); 
     232                        REGISTER_METHOD(CreateFifoHandle); 
     233                        REGISTER_METHOD(LockMutex); 
     234                        REGISTER_METHOD(UnLockMutex); 
     235                        REGISTER_METHOD(Interrupt);  
     236 
     237                        #undef REGISTER_METHOD 
     238                         
     239                        lua_register(L,"CreateLuaThread",                                       &cLuaThread_L::CreateLuaThread); 
     240                        lua_register(L,"Threads_GetHardwareConcurrency",        &cLuaThread_L::Threads_GetHardwareConcurrency); 
     241                } 
     242                 
     243        // static methods exported to lua 
     244                  
     245                /// luathread           CreateLuaThread (sFilePath) 
     246                static int                      CreateLuaThread (lua_State *L) { PROFILE 
     247                        std::string     sFilePath               = luaL_checkstring(L,1); 
     248                        return CreateUData(L,new cLuaThread(sFilePath)); 
     249                } 
     250                 
     251                /// int                         Threads_GetHardwareConcurrency  ()    
     252                // The number of hardware threads available on the current system (e.g. number of CPUs or cores or hyperthreading units), or 0 if this information is not available.  
     253                static int                      Threads_GetHardwareConcurrency  (lua_State *L) { PROFILE 
     254                        lua_pushnumber(L,boost::thread::hardware_concurrency()); //~ unsigned boost::thread::hardware_concurrency(); 
     255                        return 1; 
     256                } 
     257                 
     258                 
     259                         
     260        // object methods exported to lua 
     261 
     262                // use LockMutex -- UnLockMutex  around access to this fifo ! 
     263                /// for lua     : fifo  CreateFifoHandle        () 
     264                static int                      CreateFifoHandle        (lua_State *L) { PROFILE 
     265                        cFIFO& pFIFO = checkudata_alive(L)->GetFIFO(); 
     266                        return cLuaBind<cFIFO>::CreateUData(L,&pFIFO); 
     267                } 
     268                 
     269                /// for lua     : void  LockMutex       () 
     270                static int                      LockMutex       (lua_State *L) { PROFILE checkudata_alive(L)->LockMutex(); return 0; } 
     271                /// for lua     : void  UnLockMutex     () 
     272                static int                      UnLockMutex     (lua_State *L) { PROFILE checkudata_alive(L)->UnLockMutex(); return 0; } 
     273                /// for lua     : void  Interrupt       () 
     274                static int                      Interrupt       (lua_State *L) { PROFILE checkudata_alive(L)->Interrupt(); return 0; } 
     275                 
     276                /// Destroy() 
     277                static int      Destroy                 (lua_State *L) { PROFILE 
     278                        delete checkudata_alive(L); 
     279                        return 0; 
     280                } 
     281 
     282                virtual const char* GetLuaTypeName () { return "lugre.luathread"; } 
     283}; 
     284 
     285 
     286 
     287 
     288 
     289#endif 
    113290         
    114291/// lua binding 
     
    120297} 
    121298 
    122 }; 
     299void    LuaRegisterThreading                    (lua_State*     L) { 
     300        cThread_NetRequest::LuaRegister(L); 
     301        cThread_LoadFile::LuaRegister(L); 
     302        #ifdef ENABLE_THREADS 
     303        cLuaBind<cLuaThread>::GetSingletonPtr(new cLuaThread_L())->LuaRegister(L); 
     304        #endif 
     305} 
     306 
     307 
     308};