Esempio n. 1
0
/*
 * @brief function for libjson benchmark
 *
 * @param jsonString test data as a string
 * @return none
 */
void libjsonBenchmark(std::string jsonString)
{
    timespec time1, time2; 

    std::cout << std::setw(25) << "libjson";

    //Parsing the string
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    JSONNode n = libjson::parse(jsonString);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

    printTimeDiff(time1, time2);

    //Serialize to string
    std::string out;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    out = n.write();
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

    printTimeDiff(time1, time2);
    std::cout << std::endl;
}
Esempio n. 2
0
std::string ModuleManager::callMethod(const std::string& sender, const std::string& reciver, const std::string& action, const std::string& jsonString, IrcConnection* connection) {
	if(reciver=="IRCCONNECTION") {
		JSONNode n;
		if(!jsonString.empty()) {
			n = libjson::parse(jsonString);
		}

		if(action == "joinChan") {
			connection->joinChan(n["channel"].as_string());
		}
		else if(action == "partChan") {
			connection->partChan(n["channel"].as_string());
		}
		else if(action == "sendQuit") {
			connection->sendQuit(n["msg"].as_string());
		}
		else if(action == "sendMessage") {
			connection->sendMessage(n["target"].as_string(), n["message"].as_string());
		}
		else if(action == "sendNotice") {
			connection->sendNotice(n["target"].as_string(), n["message"].as_string());
		}
		else if(action == "sendAction") {
			connection->sendNotice(n["target"].as_string(), n["message"].as_string());
		}
		else if(action == "sendCTCP") {
			connection->sendNotice(n["target"].as_string(), n["message"].as_string());
		}
		else if(action == "changeNick") {
			connection->changeNick(n["nick"].as_string());
		}
		else if(action == "setUserMode") {
			connection->setUserMode(n["mode"].as_string());
		}
		else if(action == "setMode") {
			connection->setMode(n["nick"].as_string(), n["mode"].as_string());
		}
		else if(action == "getChannels") {
			JSONNode n(JSON_NODE);

			std::map<std::string, IrcChannel>& channelList = connection->getChannels();
			std::map<std::string, IrcChannel>::iterator iter;
			for(iter=channelList.begin(); iter!=channelList.end(); ++iter) {
				JSONNode n2(JSON_ARRAY);
				n2.set_name(iter->first);
				n2.push_back(convertIrcChannelToJSONString(iter->second));
				n.push_back(n2);
			}
			return n.write();
		}
		else if(action == "getServer") {
			return connection->getServer();
		}
		else if(action == "getPort") {
			return Base::StringUtils::toString(connection->getPort());
		}
		else if(action == "getNick") {
			return connection->getNick();
		}
		else if(action == "getID") {
			return connection->getID();
		}
	}
	else if(reciver=="IRC") {
		JSONNode n;
		if(!jsonString.empty()) {
			n = libjson::parse(jsonString);
		}

		if(action == "disconect") {
			mIRC->disconect(n["id"].as_string());
		}
		else if(action == "disconectAll") {
			mIRC->disconectAll();
		}
	}
	else if(reciver=="MODULEMANAGER") {
		JSONNode n;
		if(!jsonString.empty()) {
			n = libjson::parse(jsonString);
		}

		if(action == "reloadModule") {
			return Base::StringUtils::toString(reloadModule(n["id"].as_string()));
		}
		else if(action == "loadModuleBinary" || action == "loadModuleScript") {
			std::map<std::string, std::string> tmpParams;

			JSONNode n2 = n["params"];
			JSONNode::iterator i = n2.begin();
			for(; i!=n2.end(); ++i) {
				tmpParams[(*i).name()] = (*i).as_string();
			}

			if(action == "loadModuleBinary") {
				return Base::StringUtils::toString(loadModuleBinary(n["id"].as_string(), n["file"].as_string(), tmpParams));
			}
			else {
				return Base::StringUtils::toString(loadModuleScript(n["id"].as_string(), n["file"].as_string(), tmpParams));
			}
		}
		else if(action == "unloadModule") {
			unloadModule(n["id"].as_string());
		}
	}
	else {
		Module* module = getModule(reciver);
		if(!module) {
			return "";
		}

		if(module->type == Module::BINARY) {
			if(!module->binaryModule.onInternalMessage) {
				return "";
			}

			return module->binaryModule.onInternalMessage(sender.c_str(), action.c_str(), jsonString.c_str(), connection);
		}
	}

	return "";
}
Esempio n. 3
0
/*!
 \brief figure out the meaning of life
 \param argc		number of args
 \param argv		arguments
 
 Parses arguments and environment variables and kicks off execution of our
 little git hook.
 */
int main(int argc, const char * argv[])
{
	char *git_repo_path;
	char *next_ref;
	vector<WTConnection *> conns;
	
	
	if(argc < 2 || argv[1] == NULL)
	{
#if defined(HAVE_GETPROGNAME)
		usage(getprogname());
#elif defined(HAVE_PROGINVOC)
		usage(program_invocation_short_name);
#else
		usage("real-git-rcmp");
#endif
		return -1;
	}
	
	
	// Why set the errno?
	// because, if we don't have a GIT_DIR, we use cwd.
	// if cwd (on up) doesn't have a git repo, then we won't have an errno
	//   to print for perror and it might be something silly, like
	//   "Not a typewriter".  let's not do that, mmk?
	errno = ENOENT;
	
	if(getenv("GIT_DIR") == NULL)
	{
		char *cwd = getcwd(NULL, 0);
		git_repo_path = find_git_repo_from_path(cwd);
		free(cwd);
	}
	else
		git_repo_path = strdup(getenv("GIT_DIR"));
	
	
	if(git_repo_path == NULL)
	{
		perror("can't find git path");
		return 1;
	}
	
	
	for(size_t urls = 1; urls < argc; urls++)
	{
		WTConnection *conn = new WTConnection(NULL);
		conn->connect(argv[urls]);
		conns.push_back(conn);
	}
	
	
	// handle refs passed via stdin
	// format is "old-sha1 SP new-sha1 SP refname LF"
	// so, fgets is a good tool for this
	next_ref = static_cast<char *>(malloc(512));
	while((next_ref = fgets(next_ref, 512, stdin)) != NULL)
	{
		const char *old_id, *new_id; char *ref; JSONNode *node;
		
		char *new_id_start;
		
		// Find the first space
		char *space = strchr(next_ref, (int)' ');
		if(space == NULL) continue;
		
		// Replace the space with NUL so the string is terminated
		space[0] = '\0';
		old_id = next_ref;
		
		// The new SHA1 starts after the space
		new_id_start = space + 1;
		space = strchr(new_id_start, (int)' ');
		if(space == NULL) continue;
		
		// Replace the space with NUL so the string is terminated
		space[0] = '\0';
		new_id = new_id_start;
		
		// The ref is whatever is left over after the new ID's space.
		ref = space + 1;
		
		node = git_hook_main(git_repo_path, old_id, new_id, ref);
		
		if(node == NULL) continue;
		
		for(size_t next_conn = 0; next_conn < conns.size(); next_conn++)
		{
			WTConnection *conn = conns.at(next_conn);
			char *result;
			string payload = "payload=" + string(URLEncode(node->write().c_str()));
			uint64_t len = payload.length();
#ifdef DEBUG
			fprintf(stderr, "POSTing %s (%llu bytes) to %s\n", payload.c_str(), len, argv[next_conn + 1]);
#endif
			result = static_cast<char *>(conn->upload(payload.c_str(), &len));
#ifdef DEBUG
			if(result != NULL)
				fprintf(stderr, "result: %s\n(%llu bytes)", result, len);
#endif
			free(result);
		}
		delete node;
	}
	free(next_ref);
	
	
	while(conns.size() > 0)
	{
		WTConnection *conn = conns.back();
		conn->disconnect();
		delete conn;
		conns.pop_back();
	}
	
	
	free(git_repo_path);
	
	return 0;
}
Esempio n. 4
0
ResponseCode UpdateRepoTagOfTarball(std::string pathTarball, std::string repo, std::string tag, std::string &idImage) {
  TAR *tar = NULL;
  int ret = 0;
  int th_found = 0;
  int exitcode = 0;
//  char tmp_filepath[] = P_tmpdir "/libtar-tmpfile-XXXXXX";
  //gen path to upload image
  char* tmp_imagepath = tempnam (FileUtils::GetPathDir(pathTarball).c_str(), "imageDirTmp_");
  std::string pathTmpDir = tmp_imagepath;
  free(tmp_imagepath);
  std::cout << "path tmp dir: " << pathTmpDir << std::endl;

  int tmp_fd = -1;

  ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_RDONLY, 0, 0);
  if (ret != 0) {
	fprintf(stdout, "Fail to open file: %s\n", pathTarball.c_str());
//	return FILE_ACTION_ERROR;
	exitcode = 2;
  }
	if (exitcode == 0) {
		if (tar_extract_all(tar, (char*)pathTmpDir.c_str()) != 0) {
            fprintf(stderr, "Fail to extract file: %s\n", pathTarball.c_str());
			exitcode = 2;
		}
	}

	if (exitcode == 0) {
		ret = tar_close(tar);
		if (ret != 0) {
			perror("Failed to close tar file.");
			exitcode = 2;
		}
		tar = NULL;
	}

  //Modify repository file
  if (exitcode == 0) {
	char buf[BUFSIZ];
	ssize_t n_buf = 0;
	FILE *tmpfile = NULL;

	std::ifstream in((pathTmpDir + "/" + REPOSITORIES_FILE).c_str(), std::ios::binary);
	std::string info((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());

	JSONNode n;
	try
	{
		n = libjson::parse(info);
	} catch(const std::invalid_argument& e) {
		std::cerr << "Invalid argument: " << e.what() << '\n';
//		return FILE_ACTION_ERROR;
		exitcode = 1;
	}
	if(exitcode == 0){
		JSONNode::const_iterator i = n.begin();
		//if (i != n.end() && i -> name() == TAG_SERVICE_STR && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE)
		if (i != n.end() && i -> type() == JSON_NODE){
			JSONNode tag_id_node = i->as_node();
			i = tag_id_node.begin();
			if (i != n.end() && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE){
				idImage = i->as_string();
			}else{
				std::cout << "Tarball format error.\n";
		//		return FILE_ACTION_ERROR;
				exitcode = 1;
			}
		}
	}else{
		std::cout << "Tarball format error.\n";
//			return FILE_ACTION_ERROR;
		exitcode = 1;
		}
	}

	if(exitcode == 0){
		JSONNode newRepoNode(JSON_NODE);
		newRepoNode.set_name(repo);
		newRepoNode.push_back(JSONNode(tag, idImage));

		JSONNode newNode;
		newNode.push_back(newRepoNode);
		std::string content = newNode.write();

		FILE * pFile;

		if (exitcode == 0) {
			pFile = fopen ((pathTmpDir + "/" + REPOSITORIES_FILE).c_str() , "w");
			if (pFile == NULL) {
			   printf("Error opening file %s\n", (pathTmpDir + "/" + REPOSITORIES_FILE).c_str());
		//	   return FILE_ACTION_ERROR;
			   exitcode = 1;
			}
		}

		if (exitcode == 0) {
			fwrite (content.c_str() , sizeof(char), content.size(), pFile);
			fclose (pFile);
			printf("content tmp file: %s\n", content.c_str());
		}
	}

	if (exitcode == 0) {
		remove (pathTarball.c_str());
		ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_WRONLY | O_CREAT, 0600, 0);
		if (ret != 0) {
		  fprintf(stderr, "Fail to open file: %s\n", pathTarball.c_str());
		//	  return FILE_ACTION_ERROR;
		  exitcode = 2;
		}

		if (exitcode == 0) {
			if (tar_append_tree(tar, (char*)pathTmpDir.c_str(), "") != 0) {
			  fprintf(stderr, "Fail to compress file: %s\n", pathTarball.c_str());
		//		  return FILE_ACTION_ERROR;
			  exitcode = 2;
			}
		}

		if (exitcode == 0) {
			ret = tar_close(tar);
			if (ret != 0) {
				perror("Failed to close tar file.");
				exitcode = 2;
			} else {
				tar = NULL;
			}
		}
	}
	std::cout << "delete_folder_tree: " << pathTmpDir.c_str() << std::endl;
	if (FileUtils::delete_folder_tree(pathTmpDir.c_str())) {
		fprintf(stderr, "Fail to delete temp dir: %s\n", pathTmpDir.c_str());
	}

	if (exitcode == 0) {
		return FILE_ACTION_SUCCESS;
	} else if (exitcode == 1) {
		return FILE_ACTION_ERROR;
	} else {
		return DATA_ERROR;
	}
}
Esempio n. 5
0
MEVENT CSkypeProto::AppendDBEvent(MCONTACT hContact, MEVENT hEvent, const char *szContent, const char *szUid, time_t edit_time)
{
	mir_cslock lck(m_AppendMessageLock);
	DBEVENTINFO dbei = { sizeof(dbei) };
	dbei.cbBlob = db_event_getBlobSize(hEvent);
	dbei.pBlob = mir_ptr<BYTE>((PBYTE)mir_alloc(dbei.cbBlob));
	db_event_get(hEvent, &dbei);

	JSONNode jMsg = JSONNode::parse((char*)dbei.pBlob);
	if (jMsg)
	{
		JSONNode &jEdits = jMsg["edits"];
		if (jEdits)
		{
			for (auto it = jEdits.begin(); it != jEdits.end(); ++it)
			{
				const JSONNode &jEdit = *it;

				if (jEdit["time"].as_int() == edit_time)
					return hEvent;
			}
			JSONNode jEdit;
			jEdit 
				<< JSONNode("time", (long)edit_time)
				<< JSONNode("text", szContent);

			jEdits << jEdit;
		}
	}
	else
	{
		jMsg = JSONNode();
		JSONNode jOriginalMsg; jOriginalMsg.set_name("original_message");
		JSONNode jEdits(JSON_ARRAY); jEdits.set_name("edits");
		JSONNode jEdit;

		jOriginalMsg
			<< JSONNode("time", (long)dbei.timestamp)
			<< JSONNode("text", (char*)dbei.pBlob);

		jMsg << jOriginalMsg;

		jEdit 
			<< JSONNode("time", (long)edit_time)
			<< JSONNode("text", szContent);

		jEdits << jEdit;	
		jMsg   << jEdits;


	}
	db_event_delete(hContact, hEvent);	
	return AddDbEvent(SKYPE_DB_EVENT_TYPE_EDITED_MESSAGE, hContact, dbei.timestamp, DBEF_UTF, jMsg.write().c_str(), szUid);
}