bool CUpdateManager::checkUpdate(JSON::Object::Ptr& param, std::string& detail) { if(!m_update_path.isDirectory()) { warnf("%s, %d: Update manager check update error[update dir not exists], please init first.", __FILE__, __LINE__); } if(param.isNull() || !param->has(UPDATE_TYPE_STR)) { detail = "450"; return false; } std::string devType = param->getValue<std::string>(UPDATE_TYPE_STR); Path path(m_update_path); path.append(devType); path.append(Path::separator()); if(!path.isDirectory()) { detail = "451"; return false; } path.append("Info"); if(!path.isFile()) { detail = "452"; warnf("%s, %d: UpdateManager checkUpdate failed, %s update info file not exists.", __FILE__, __LINE__, devType.c_str()); return false; } Util::JSONConfiguration updateConf; try { updateConf.load(path.toString()); } catch(Exception& e) { detail = "453"; warnf("%s, %d: Load JSON Configuration[%s] file failed.", __FILE__, __LINE__, path.toString().c_str()); return false; } std::string version = updateConf.getString(UPDATE_VERSION_STR); std::string buildtime = updateConf.getString(UPDATE_BUILDTIME_STR); JSON::Object::Ptr pUpdate = new JSON::Object; pUpdate->set(UPDATE_VERSION_STR, version); pUpdate->set(UPDATE_BUILDTIME_STR, buildtime); std::string newfeature = updateConf.getRawString(UPDATE_NEWFEATURE_STR); JSON::Parser parser; Dynamic::Var var = parser.parse(newfeature); JSON::Array::Ptr pNew = var.extract<JSON::Array::Ptr>(); pUpdate->set(UPDATE_NEWFEATURE_STR, pNew); param->set("update", pUpdate); return true; }
PlayerSkinColors PlayerSkinColors::load(const std::string &profileRoot, const std::string &file) { Json::Parser parser; Json::Value root = parser.parse(profileRoot + file); PlayerSkinColors colors; colors.set(PlayerSkinColors::HairTop, Color::fromString(root.get("hairTop").asString())); colors.set(PlayerSkinColors::HairBottom, Color::fromString(root.get("hairBottom").asString())); colors.set(PlayerSkinColors::BodyInner, Color::fromString(root.get("bodyInner").asString())); colors.set(PlayerSkinColors::BodyOuter, Color::fromString(root.get("bodyOuter").asString())); colors.set(PlayerSkinColors::HandInner, Color::fromString(root.get("handInner").asString())); colors.set(PlayerSkinColors::HandOuter, Color::fromString(root.get("handOuter").asString())); colors.set(PlayerSkinColors::Trousers, Color::fromString(root.get("trousers").asString())); colors.set(PlayerSkinColors::Shoes, Color::fromString(root.get("shoes").asString())); colors.set(PlayerSkinColors::Face, Color::fromString(root.get("face").asString())); return colors; }
void ElevatorList::load(const std::string& path, bool mirror) { Json::Parser parser; Json::Value root = parser.parse(path); Int32 width = root.get("width").asInt(); Int32 height = root.get("height").asInt(); Size elevators = root.get("elevators").getLength(); for (Size i = 0; i < elevators; i++) { Elevator elevator; Json::Value points = root.get("elevators").get(i).get("controlPoints"); for (Size j = 0; j < points.getLength(); j++) { Int32 x = points.get(j).get("x").asInt(); Int32 y = points.get(j).get("y").asInt(); elevator.addControlPoint(Elevator::ControlPoint(mirror ? width - 1 - x : x, height - y)); } add(elevator); } }
void Menu::loadPersonData(const std::string& filePath) { if (!File::exists(filePath)) { return; } Json::Parser parser; Json::Value json = parser.parse(filePath); persons.fromJson(json.get("persons")); for (const Person& person : persons.list()) { listbox[ALL_PLAYER_LIST]->addItem(person.getName()); } Json::Value playing = json.get("playing"); for (Size i = 0; i < playing.getLength(); i++) { std::string name = playing.get(i).asString(); listbox[CUR_PLAYERS_LIST]->addItem(name); listbox[ALL_PLAYER_LIST]->removeItem(name); } }
void Level::load(const std::string& path, bool mirror) { levelData.clear(); waterLevel = 0; Json::Parser parser; Json::Value root = parser.parse(path); width = root.get("width").asInt(); height = root.get("height").asInt(); Int32 blockCount = width * height; Json::Value blocks = root.get("blocks"); levelData.resize(blockCount); for (Size i = 0; i < blocks.getLength(); i++) { levelData[i] = blocks.get(i).asInt(); } if (mirror) { mirrorLevelData(); } waterBlock = findWaterType(); }
void regTest5() { tracef("reg test 5 begin:reg twice"); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket sss(pContext); SocketAddress sa("127.0.0.1", 12222); sss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.token"; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32]; snprintf(tms_str, 31, "%llu", tms); std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); DynamicStruct param; param["key"] = md5key; param["timestamp"] = tms_str; param["dev_name"] = "lock3"; param["dev_type"] = "sc-01"; param["uuid"] = "SC00000003"; ds["param"] = param; tracef("data send: %s.", ds.toString().c_str()); sss.sendBytes(ds.toString().c_str(), ds.toString().length()); char buf[1024] = {0, }; sss.receiveBytes(buf, 1024); JSON::Parser parser; Dynamic::Var var = parser.parse(buf); JSON::Object::Ptr pObj = var.extract<JSON::Object::Ptr>(); DynamicStruct ds_recv = *pObj; std::string token = ds_recv["param"]["token"]; DynamicStruct ds_reg; ds_reg["type"] = "request"; ds_reg["action"] = "server.register"; DynamicStruct param_reg; param_reg["token"] = token; param_reg["uuid"] = "SC00000003"; ds_reg["param"] = param_reg; StreamSocket ss; SocketAddress sa2("127.0.0.1", 13333); ss.connect(sa2, Timespan(3, 0)); ss.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); tracef("reg data send: %s.", ds_reg.toString().c_str()); memset(buf, 0, 1024); ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); ss.close(); Thread::sleep(2000); StreamSocket ss2; ss2.connect(sa2, Timespan(3, 0)); tracef("reg data send again: %s.", ds_reg.toString().c_str()); ss2.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); memset(buf, 0, 1024); ss2.receiveBytes(buf, 1024); tracef("recv data: %s.", buf); ss2.close(); tracef("reg test 5 finished."); }
int main(int argc, char** argv) { if(argc < 4) return 0; std::string host = argv[1]; UInt64 ssl_port = atoi(argv[2]); UInt64 reg_port = atoi(argv[3]); SocketAddress ssl_addr(host, ssl_port); SocketAddress reg_addr(host, reg_port); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket* ssl_sock = new SecureStreamSocket(pContext); try { ssl_sock->connect(ssl_addr, Timespan(3, 0)); } catch(Exception& e) { warnf("%s, %d: message: %s.", __FILE__, __LINE__, e.message().c_str()); } tracef("%s, %d: now sleep.", __FILE__, __LINE__); Thread::sleep(3 * 1000); char buf[512] = {0, }; DynamicStruct ssl_buf; ssl_buf["type"] = "request"; ssl_buf["action"] = "server.token"; DynamicStruct ssl_param; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32] = {0, }; snprintf(tms_str, 31, "%llu", tms); ssl_param["timestamp"] = tms_str; std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); ssl_param["key"] = md5key; ssl_param["uuid"] = "hjhjhjhj"; ssl_param["dev_type"] = "abc"; ssl_param["dev_name"] = "cba"; ssl_buf["param"] = ssl_param; snprintf(buf, 511, "%s", ssl_buf.toString().c_str()); tracef("%s, %d: send buf: %s\n", __FILE__, __LINE__, buf); ssl_sock->sendBytes(buf, 512); memset(buf, 0, 512); if(ssl_sock->poll(Timespan(30, 0), Socket::SELECT_READ) > 0) { ssl_sock->receiveBytes(buf, 512); tracef("%s, %d: receive buf: %s\n", __FILE__, __LINE__, buf); } ssl_sock->close(); delete ssl_sock; pContext = NULL; Dynamic::Var var; JSON::Parser parser; try { var = parser.parse(buf); } catch(Exception& e) { warnf("%s, %d: %s\n", __FILE__, __LINE__, e.message().c_str()); return 0; } JSON::Object::Ptr obj = var.extract<JSON::Object::Ptr>(); DynamicStruct ssl_recv = *obj; std::string token = ssl_recv["token"]; DynamicStruct reg_buf; reg_buf["action"] = "register"; reg_buf["token"] = token; reg_buf["uuid"] = "hjhjhjhj"; reg_buf["devType"] = "abc"; memset(buf, 0, 512); snprintf(buf, 511, "%s", reg_buf.toString().c_str()); StreamSocket* reg_sock = new StreamSocket(reg_addr); reg_sock->sendBytes(buf, 512); memset(buf, 0, 512); if(reg_sock->poll(Timespan(3, 0), Socket::SELECT_READ) > 0) { reg_sock->receiveBytes(buf, 512); } Thread::sleep(2000); while(1) { if(reg_sock->poll(Timespan(3, 0), Socket::SELECT_READ) > 0) { memset(buf, 0, 512); reg_sock->receiveBytes(buf, 512); parser.reset(); Dynamic::Var recv_request = parser.parse(buf); JSON::Object::Ptr req_obj = recv_request.extract<JSON::Object::Ptr>(); DynamicStruct req_ds = *req_obj; tracef("recv request:%s\n", req_ds.toString().c_str()); req_ds["type"] = "response"; req_ds["result"] = "good"; snprintf(buf, 512, "%s", req_ds.toString().c_str()); reg_sock->sendBytes(buf, 512); infof("send response[%s].\n", buf); char c; c = getchar(); if(c == 'q') break; } } delete reg_sock; return 0; }
void CHTTPRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { response.setContentType("application/json"); response.setChunkedTransferEncoding(true); if(m_buf == NULL) m_buf = new char[512]; memset(m_buf, 0, 512); request.stream().getline(m_buf, 512, '\n'); infof("%s, %d: Receive HTTP request[%s]", __FILE__, __LINE__, m_buf); JSON::Parser parser; Dynamic::Var var; try { var = parser.parse(m_buf); } catch(Exception& e) { JSON::Object::Ptr re = new JSON::Object; re->set(KEY_TYPE_STR, TYPE_RESPONSE_STR); re->set(KEY_RESULT_STR, RESULT_FAIL_STR); re->set(KEY_DETAIL_STR, "100"); DynamicStruct ds_res = *re; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); re = NULL; return; } JSON::Object::Ptr obj = var.extract<JSON::Object::Ptr>(); JSON::Object::Ptr res = new JSON::Object(*obj); if(!checkRequestFormat(obj, res)) { DynamicStruct ds_res = *res; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); res = NULL; return; } DynamicStruct ds = *obj; std::string uuid = ds[KEY_PARAM_STR][REG_UUID_STR].toString(); std::string action = ds[KEY_ACTION_STR].toString(); std::string component = ""; std::string method = ""; bool ret = parseAction(action, component, method); if(!ret) { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "105"); DynamicStruct ds_res = *res; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); res = NULL; return; } if(component == COMPONENT_SERVER_STR) { if(method == SERVER_METHOD_CHECK) { res->set(KEY_RESULT_STR, RESULT_GOOD_STR); CDeviceManager* dev_mgr = CDeviceManager::instance(); DeviceInfo* dev_info = dev_mgr->getDevice(uuid); DynamicStruct param; param[REG_UUID_STR] = uuid; if(dev_info != NULL) { param[REG_STATE_STR] = "online"; param[REG_DEV_TYPE_STR] = dev_info->devType; } else { param[REG_STATE_STR] = "offline"; } res->remove(KEY_PARAM_STR); res->set(KEY_PARAM_STR, param); } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "106"); } DynamicStruct ds_res = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); res = NULL; return; } else if(component == COMPONENT_UPDATE_STR) { std::string detail = ""; if(method == UPDATE_METHOD_CHECK) { CUpdateManager* update_manager = CUpdateManager::instance(); JSON::Object::Ptr pParam = obj->getObject(KEY_PARAM_STR); if(update_manager->checkUpdate(pParam, detail)) { res->set(KEY_RESULT_STR, RESULT_GOOD_STR); res->remove(KEY_PARAM_STR); res->set(KEY_PARAM_STR, pParam); } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, detail); } } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "106"); } DynamicStruct ds_res = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); res = NULL; return; } RequestInfo* req = new RequestInfo((UInt64)this, uuid, 5*1000*1000, obj); CRegServer* reg_server = CRegServer::instance(); //it will hang until response send back, or timeout reg_server->sendRequest(req); res = req->response; if(res.isNull()) { warnf("%s, %d: Request timeout.", __FILE__, __LINE__); DynamicStruct result = *obj; result[KEY_RESULT_STR] = RESULT_FAIL_STR; result[KEY_DETAIL_STR] = "timeout"; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str()); response.sendBuffer(result.toString().c_str(), result.toString().length()); } else { DynamicStruct result = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str()); response.sendBuffer(result.toString().c_str(), result.toString().length()); } res = NULL; delete req; }