Example #1
0
void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data);
  IOTJS_ASSERT(tcp_wrap != NULL);

  // tcp handle
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // socket object
  JObject jsocket = jtcp.GetProperty("owner");
  IOTJS_ASSERT(jsocket.IsObject());

  // onread callback
  JObject jonread = jtcp.GetProperty("onread");
  IOTJS_ASSERT(jonread.IsFunction());

  JArgList jargs(4);
  jargs.Add(jsocket);
  jargs.Add(JVal::Number((int)nread));
  jargs.Add(JVal::Bool(false));

  if (nread <= 0) {
    if (buf->base != NULL) {
      ReleaseBuffer(buf->base);
    }
    if (nread < 0) {
      if (nread == UV__EOF) {
        jargs.Set(2, JVal::Bool(true));
      }
      MakeCallback(jonread, JObject::Null(), jargs);
    }
    return;
  }

  JObject jbuffer(CreateBuffer(static_cast<size_t>(nread)));
  BufferWrap* buffer_wrap = BufferWrap::FromJBuffer(jbuffer);

  buffer_wrap->Copy(buf->base, nread);

  jargs.Add(jbuffer);
  MakeCallback(jonread, JObject::Null(), jargs);

  ReleaseBuffer(buf->base);
}
Example #2
0
// A client socket wants to connect to this server.
// Parameters:
//   * uv_stream_t* handle - server handle
//   * int status - status code
static void OnConnection(uv_stream_t* handle, int status) {
  // Server tcp wrapper.
  TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data);
  IOTJS_ASSERT(tcp_wrap->tcp_handle() == reinterpret_cast<uv_tcp_t*>(handle));

  // Tcp object
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // `onconnection` callback.
  JObject jonconnection = jtcp.GetProperty("onconnection");
  IOTJS_ASSERT(jonconnection.IsFunction());

  // The callback takes two parameter
  // [0] status
  // [1] client tcp object
  JArgList args(2);
  args.Add(JVal::Number(status));

  if (status == 0) {
    // Create client socket handle wrapper.
    JObject jcreate_tcp = jtcp.GetProperty("createTCP");
    IOTJS_ASSERT(jcreate_tcp.IsFunction());

    JObject jclient_tcp = jcreate_tcp.CallOk(JObject::Null(),
                                             JArgList::Empty());
    IOTJS_ASSERT(jclient_tcp.IsObject());

    TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(jclient_tcp.GetNative());

    uv_stream_t* client_handle =
        reinterpret_cast<uv_stream_t*>(tcp_wrap->tcp_handle());

    int err = uv_accept(handle, client_handle);
    if (err) {
      return;
    }

    args.Add(jclient_tcp);
  }

  MakeCallback(jonconnection, jtcp, args);
}
Example #3
0
void JStringServer::ExtractStrings()
{
    FILE* fp = fopen( "strings.txt", "wt" );
    if (!fp)
    {
        return;
    }
    m_Dictionary.clear();

    //  extract text strings
    JObjectIterator it( JCore::s_pInstance );
    JString val, hash;
    while (it)
    {
        JObject* pObj = *it;
        if (pObj->HasName( "system" ))
        {
            it.breadth_next();
            continue;
        }
        ++it;
        bool bRes = pObj->GetProperty( "text", val );
        if (!bRes || val.size() == 0 || val[0] == '#')
        {
            continue;
        }
        if (!HasCyrillics( val.c_str() ))
        {
            continue;
        }
        HashString( val.c_str(), hash );
        JStringDictionary::iterator location = m_Dictionary.find( hash );

        JString taggedHash = "#";
        taggedHash += hash;
        pObj->SetProperty( "text", taggedHash.c_str() );

        if (location != m_Dictionary.end())
        {
            if (val == (*location).second)
            {
                continue;
            }
            assert( false );
        }
        fprintf( fp, "%s %s\n", hash.c_str(), val.c_str() );
        m_Dictionary[hash] = val;
    }
    fclose( fp );

    //  save scripts
    g_pPersistServer->SaveScripts();
}
Example #4
0
// Socket close result handler.
static void AfterClose(uv_handle_t* handle) {
  HandleWrap* tcp_wrap = HandleWrap::FromHandle(handle);
  IOTJS_ASSERT(tcp_wrap != NULL);

  // tcp object.
  JObject jtcp = tcp_wrap->jobject();
  IOTJS_ASSERT(jtcp.IsObject());

  // callback function.
  JObject jcallback = jtcp.GetProperty("onclose");
  if (jcallback.IsFunction()) {
    MakeCallback(jcallback, JObject::Null(), JArgList::Empty());
  }
}
Example #5
0
int JLuaServer::FnGet( lua_State* pLua )
{
    if (lua_gettop( pLua ) < 2 || lua_gettop( pLua ) > 3)
    {
        rlog.err( "LUA: Incorrect command usage: <get>. "
            "Function takes 2 or 3 arguments (<objectRef>|<objectName>, <propName>, [tag]), but %d is provided", 
            lua_gettop( pLua ) );
        lua_settop( pLua, 0 );
        lua_pushnil( pLua );
        return 1;
    }
    JObject* pObj = NULL;
    const char* pPath = lua_tostring( pLua, 1 );
    if (pPath)
    {
        JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata );
        assert( pThread->m_pLua == pLua );
        JObject* pRootObj = pThread->m_pRootObj;
        pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj );
    }
    else
    {
        pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) );
    }
    const char* pPropName = lua_tostring( pLua, 2 );
    int tag = -1;
    if (lua_isnumber( pLua, 3 ))
    {
        tag = (int)lua_tonumber( pLua, 3 );
    }

    if (!pObj)
    {
        rlog.warn( "LUA: Trying to call <get> for the nil object. Nil is returned." );
        lua_pushnil( pLua );
        return 1;
    }

    static JString val;
    bool bRes = pObj->GetProperty( pPropName, val, tag );
    if (!bRes)
    {
        rlog.err( "LUA: Could not get property '%s' for object '%s' of type '%s'.", 
            pPropName, pObj->GetName(), pObj->ClassName() );
    }

    lua_pushstring( pLua, val.c_str() );
    return 1;
} // JLuaServer::FnGet
Example #6
0
JObject MakeStatObject(uv_stat_t* statbuf) {


#define X(name)                              \
  JObject name((int)statbuf->st_##name);        \

  X(dev)
  X(mode)
  X(nlink)
  X(uid)
  X(gid)
  X(rdev)

#undef X

#define X(name)                              \
  JObject name((double)statbuf->st_##name);        \

  X(blksize)
  X(ino)
  X(size)
  X(blocks)

#undef X


  Module* module = GetBuiltinModule(MODULE_FS);
  JObject* fs = module->module;
  JObject createStat = fs->GetProperty("createStat");

  JArgList args(10);
  args.Add(dev);
  args.Add(mode);
  args.Add(nlink);
  args.Add(uid);
  args.Add(gid);
  args.Add(rdev);
  args.Add(blksize);
  args.Add(ino);
  args.Add(size);
  args.Add(blocks);

  JResult jstat_res(createStat.Call(JObject::Null(), args));
  IOTJS_ASSERT(jstat_res.IsOk());

  return jstat_res.value();
}
Example #7
0
JObject MakeStatObject(uv_stat_t* statbuf) {
  Module* module = GetBuiltinModule(MODULE_FS);
  IOTJS_ASSERT(module != NULL);

  JObject* fs = module->module;
  IOTJS_ASSERT(fs != NULL);

  JObject createStat = fs->GetProperty("_createStat");
  IOTJS_ASSERT(createStat.IsFunction());

  JObject jstat;

#define X(statobj, name) \
  JObject name((int32_t)statbuf->st_##name); \
  statobj.SetProperty(#name, name); \

  X(jstat, dev)
  X(jstat, mode)
  X(jstat, nlink)
  X(jstat, uid)
  X(jstat, gid)
  X(jstat, rdev)

#undef X

#define X(statobj, name) \
  JObject name((double)statbuf->st_##name); \
  statobj.SetProperty(#name, name); \

  X(jstat, blksize)
  X(jstat, ino)
  X(jstat, size)
  X(jstat, blocks)

#undef X

  JArgList jargs(1);
  jargs.Add(jstat);

  JResult jstat_res(createStat.Call(JObject::Null(), jargs));
  IOTJS_ASSERT(jstat_res.IsOk());

  return jstat_res.value();
}