Ejemplo n.º 1
0
static int
proto(glite_renewal_core_context ctx, int sock)
{
    char  *buf = NULL;
    size_t  buf_len;
    int  ret;
    edg_wlpr_Response  response;
    edg_wlpr_Request  request;
    command_table  *command;
    struct timeval timeout;

    memset(&request, 0, sizeof(request));
    memset(&response, 0, sizeof(response));

    timeout.tv_sec = (long) default_timeout;
    timeout.tv_usec = (long) ((default_timeout - timeout.tv_sec) * 1e6);

    ret = edg_wlpr_Read(sock, &timeout, &buf, &buf_len);
    if (ret) {
        edg_wlpr_Log(ctx, LOG_ERR, "Error reading from client: %s",
                     edg_wlpr_GetErrorText(ret));
        return ret;
    }

    ret = decode_request(ctx, buf, buf_len, &request);
    free(buf);
    if (ret)
        goto end;

    /* XXX check request (protocol version, ...) */

    command = find_command(ctx, request.command);
    if (command == NULL) {
        ret = EDG_WLPR_ERROR_UNKNOWN_COMMAND;
        edg_wlpr_Log(ctx, LOG_ERR, "Received unknown command (%d)", request.command);
        goto end;
    }

    command->handler(ctx, &request, &response);

    ret = encode_response(ctx, &response, &buf);
    if (ret)
        goto end;

    ret = edg_wlpr_Write(sock, &timeout, buf, strlen(buf) + 1);
    free(buf);
    if (ret) {
        edg_wlpr_Log(ctx, LOG_ERR, "Error sending response of command %d to client: %s",
                     request.command,
                     edg_wlpr_GetErrorText(ret));
        goto end;
    }

end:
    edg_wlpr_CleanRequest(&request);
    edg_wlpr_CleanResponse(&response);

    return ret;
}
Ejemplo n.º 2
0
void JsonParser::parse_msg(string msg, int sock){
	Json::Value root;
	Json::Features f = Json::Features::strictMode();
	Json::Reader reader(f);
	bool parsedOk = false;
	string s_response = "";
	string json_msg;
	int status = 0;

	parsedOk=reader.parse(msg,root,false);
	if(!parsedOk){
		printf("parse error: %s",STR2CHAR(reader.getFormatedErrorMessages()));
		return;
	}
	m_iMsgType = root["msgType"].asInt();

	switch(m_iMsgType)
	{
	case SIGN_IN:{
		printf("******************sign_in******************\n");
		string userId = root["srcId"].asString();
		string passwd = root["password"].asString();
		bool is_correct = 0;
		m_pMysqlExec->verify_pw(is_correct, userId, passwd);
		if(is_correct){
			printf("password correct!\n");
		}
		online_map.insert(map<string, int>::value_type(userId, sock));
		break;
	}
	case REGISTER:
	{
		printf("******************register******************\n");
		string userId = root["srcId"].asString();
		string passwd = root["password"].asString();
		if(m_pMysqlExec->reg_user(userId, passwd)){
			online_map.insert(map<string, int>::value_type(userId, sock));
		}
		break;
	}
	case SEND_MSG:
	{
		printf("******************send_msg******************\n");
		string srcId = root["srcId"].asString();
		string dstId = root["dstId"].asString();
		string msg = root["msg"].asString();
		bool is_buddy = 0;
		m_pMysqlExec->is_buddy(is_buddy, srcId, dstId);
		if(!is_buddy){
			printf("%s and %s are not friends\n", srcId.c_str(), dstId.c_str());
			break;
		}
		map<string, int>::iterator res = online_map.find(dstId);
		if(res != online_map.end()){
			printf("find %s's sock\n", dstId.c_str());
			json_msg = encode_msg(srcId, msg);
			send_msg(res->second, STR2CHAR(json_msg), json_msg.length());
		}
		break;
	}

	case FIND_PERSON:{
		printf("******************find_person******************\n");
		string dstId = root["dstId"].asString();
		bool is_exist = 0;
		if(!m_pMysqlExec->find_user(is_exist, dstId)){
			printf("can not find user %s\n", dstId.c_str());
		}
		if(is_exist){
			json_msg = encode_response(FIND_PERSON_RES, OK, dstId);
		}else{
			json_msg = encode_response(FIND_PERSON_RES, FAIL, dstId);
		}
		send_msg(sock, STR2CHAR(json_msg), json_msg.length());
		break;
	}

	case ADD_BUDDY:{
		printf("******************add_buddy******************\n");
		string srcId = root["srcId"].asString();
		string dstId = root["dstId"].asString();
		bool is_buddy = 0;
		if(!m_pMysqlExec->is_buddy(is_buddy, srcId, dstId)){
			json_msg = encode_response(ADD_BUDDY_RES, FAIL, dstId);
			send_msg(sock, STR2CHAR(json_msg), json_msg.length());
			break;
		}
		if(!is_buddy){
			if(!m_pMysqlExec->add_buddy(srcId, dstId)){
				status = FAIL;
			}
		}
		status = OK;
		json_msg = encode_response(ADD_BUDDY_RES, status, dstId);
		send_msg(sock, STR2CHAR(json_msg), json_msg.length());
		break;
	}
	case DEL_BUDDY:{
		string srcId = root["srcId"].asString();
		string dstId = root["dstId"].asString();
		if(!m_pMysqlExec->del_buddy(srcId, dstId)){
			json_msg = encode_response(ADD_BUDDY_RES, FAIL, dstId);
			send_msg(sock, STR2CHAR(json_msg), json_msg.length());
			break;
		}
		string json_msg;
		json_msg = encode_response(DEL_BUDDY_RES, OK, dstId);
		send_msg(sock, STR2CHAR(json_msg), json_msg.length());
		break;
	}

	default:{
		printf("unknown type\n");
		break;
	}
	}

}