Example #1
0
int main(int argc ,char** argv){
    int iRet = parseArg(argc, argv);

    if (0 == iRet) return 0;
    if (-1 == iRet) return 1;

    CwxSockStream  stream;
    CwxINetAddr  addr(g_unPort, g_strHost.c_str());
    CwxSockConnector conn;
    if (0 != conn.connect(stream, addr)){
        printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno);
        return 1;
    }
    CwxPackageWriterEx writer;
    CwxPackageReaderEx reader;
    CwxMsgHead head;
    CwxMsgBlock* block=NULL;
    char szErr2K[2048];
    char const* pErrMsg=NULL;
	CwxKeyValueItemEx item;
	CWX_UINT32 uiBufLen = UNISTOR_MAX_DATA_SIZE ;
	char* szBuf = (char*)malloc(uiBufLen);
	CwxEncodeXml xmlEncode;
	CwxXmlPackageConv xmlConv(&xmlEncode);
    CWX_UINT32 uiVersion=0;
    CWX_UINT32 uiFieldNum=0;
    if (g_file.length()){
		string strContent;
		if (!CwxFile::readTxtFile(g_file, strContent)){
			printf("Failure to read xml file:%s\n", g_file.c_str());
			free(szBuf);
			return -1;
		}
		if (!xmlConv.xmlToPackage(strContent.c_str(), szBuf, uiBufLen)){
			printf("Failure to convert xml, err:%s\n", xmlConv.getErrMsg());
			free(szBuf);
			return -1;
		}
        if (!reader.unpack(szBuf, uiBufLen)){
            printf("Failure to unpack msg, err:%s\n", reader.getErrMsg());
            free(szBuf);
            return -1;
        }
		CWX_UINT32 index=0;
		item.m_szData = reader.getKey(index)->m_szData;
		item.m_uiDataLen = reader.getKey(index)->m_uiDataLen;
		item.m_bKeyValue = reader.getKey(index)->m_bKeyValue;
	}else{
		item.m_szData = g_data.c_str();
		item.m_uiDataLen = g_data.length();
		item.m_bKeyValue = false;
	}

    CwxKeyValueItemEx  key;
    key.m_szData = g_key.c_str();
    key.m_uiDataLen = g_key.length();
    key.m_bKeyValue = false;
    CwxKeyValueItemEx  field;
    field.m_szData = g_field.c_str();
    field.m_uiDataLen = g_field.length();
    field.m_bKeyValue = false;
    CwxKeyValueItemEx  extra;
    extra.m_szData = g_extra.c_str();
    extra.m_uiDataLen = g_extra.length();
    extra.m_bKeyValue = false;

	do {
		if (UNISTOR_ERR_SUCCESS != UnistorPoco::packRecvAdd(
            &writer,
            block,
			100,
            key,
            g_field.length()?&field:NULL,
            g_extra.length()?&extra:NULL,
			item,
			g_uiExpire,
            g_sign,
            g_uiVersion,
            g_bCache,
            g_user.c_str(),
            g_passwd.c_str(),
			szErr2K))
		{
            printf("failure to pack add key package, err=%s\n", szErr2K);
            iRet = 1;
            break;
        }
        if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(),
            block->rd_ptr(),
            block->length()))
        {
            printf("failure to send message, errno=%d\n", errno);
            iRet = 1;
            break;
        }
        CwxMsgBlockAlloc::free(block);
        block = NULL;
        //recv msg
        if (0 >= CwxSocket::read(stream.getHandle(), head, block))
        {
            printf("failure to read the reply, errno=%d\n", errno);
            iRet = 1;
            break;
        }
        if (UnistorPoco::MSG_TYPE_RECV_ADD_REPLY != head.getMsgType())
        {
            printf("recv a unknow msg type, msg_type=%u\n", head.getMsgType());
            iRet = 1;
            break;
        }
        if (UNISTOR_ERR_SUCCESS != UnistorPoco::parseRecvReply(&reader,
            block,
            iRet,
            uiVersion,
            uiFieldNum,
            pErrMsg,
            szErr2K))
        {
            printf("failure to unpack reply msg, err=%s\n", szErr2K);
            iRet = 1;
            break;
        }
        if (UNISTOR_ERR_SUCCESS != iRet)
        {
            printf("failure to add key, task_id=%u, err_code=%d, err=%s\n", head.getTaskId(), iRet, pErrMsg);
            iRet = 1;
            break;
        }
        iRet = 0;
        printf("success to add key[%s], data=%s, version=%u, fieldnum=%u, taskid=%u\n",
            g_key.c_str(),
			g_data.c_str(),
            uiVersion,
            uiFieldNum,
            head.getTaskId());
    } while(0);
    if (block) CwxMsgBlockAlloc::free(block);
    stream.close();
	free(szBuf);
    return iRet;
}
Example #2
0
int main(int argc ,char** argv)
{
    int iRet = parseArg(argc, argv);

    if (0 == iRet) return 0;
    if (-1 == iRet) return 1;

    CwxSockStream  stream;
    CwxINetAddr  addr(g_unPort, g_strHost.c_str());
    CwxSockConnector conn;
    if (0 != conn.connect(stream, addr))
    {
        printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno);
        return 1;
    }
    CwxPackageWriterEx writer;
    CwxPackageWriterEx writer1;
    CwxPackageReaderEx reader;
    CwxMsgHead head;
    CwxMsgBlock* block=NULL;
    char szErr2K[2048];
	CWX_UINT32  output_buf_len = UNISTOR_MAX_KVS_SIZE;
	char* output_buf = (char*)malloc(output_buf_len);
	bool bGets=g_key.size()>1?true:false;
	do {
		string strField;
        list<string>::iterator iter = g_field.begin();
        while(iter != g_field.end()){
			if (!strField.length()){
				strField = *iter;
			}else{
				strField += "\n";
				strField += *iter;
			}
			iter++;
		}
        CwxKeyValueItemEx  field;
        field.m_szData = strField.c_str();
        field.m_uiDataLen = strField.length();
        field.m_bKeyValue = false;
        CwxKeyValueItemEx  extra;
        extra.m_szData = g_extra.c_str();
        extra.m_uiDataLen = g_extra.length();
        extra.m_bKeyValue = false;
        if (!bGets){
            CwxKeyValueItemEx  key;
            key.m_szData = g_key.begin()->c_str();
            key.m_uiDataLen = g_key.begin()->length();
            key.m_bKeyValue = false;
            if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKey(&writer,
                block,
                100,
                key,
                strField.length()?&field:NULL,
                g_extra.length()?&extra:NULL,
                g_bVer,
                g_user.c_str(),
                g_passwd.c_str(),
                g_bMaster,
                g_ucKeyInfo,
                szErr2K))
            {
                printf("failure to pack get key package, err=%s\n", szErr2K);
                iRet = 1;
                break;
            }
        }else{
            list<pair<char const*, CWX_UINT16> > keys;
            list<string>::iterator iter = g_key.begin();
            while(iter != g_key.end()){
                keys.push_back(pair<char const*, CWX_UINT16>(iter->c_str(), iter->length()));
                iter++;
            }
            if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKeys(&writer,
                &writer1,
                block,
                100,
                keys,
                strField.length()?&field:NULL,
                g_extra.length()?&extra:NULL,
                g_user.c_str(),
                g_passwd.c_str(),
                g_bMaster,
                g_ucKeyInfo,
                szErr2K))
            {
                printf("failure to pack gets key package, err=%s\n", szErr2K);
                iRet = 1;
                break;
            }
        }
		//send
		if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(),
            block->rd_ptr(),
            block->length()))
        {
            printf("failure to send message, errno=%d\n", errno);
            iRet = 1;
            break;
        }
        CwxMsgBlockAlloc::free(block);
        block = NULL;
        //recv msg
        if (0 >= CwxSocket::read(stream.getHandle(), head, block))
        {
            printf("failure to read the reply, errno=%d\n", errno);
            iRet = 1;
            break;
        }

		if ((bGets?UnistorPoco::MSG_TYPE_RECV_GETS_REPLY:UnistorPoco::MSG_TYPE_RECV_GET_REPLY) != head.getMsgType())
        {
            printf("recv a unknow msg type, task_id=%u, msg_type=%u\n", head.getTaskId(), head.getMsgType());
            iRet = 1;
            break;
        }
		printf("query result, task_id=%u, len=%u, msg_len=%u\n", head.getTaskId(), head.getDataLen(), block->length());
		CwxPackageEx::dump(block->rd_ptr(),
			block->length(),
			output_buf,
			output_buf_len,
			"  ");
		printf("dump len:%u\n", output_buf_len);
		output_buf[output_buf_len] = 0x00;
        for (CWX_UINT32 i=0; i<output_buf_len; i++){
            if (0 == output_buf[i]) output_buf[i]=' ';
        }
		printf("%s\n", output_buf);
    } while(0);
    if (block) CwxMsgBlockAlloc::free(block);
	if (output_buf) free(output_buf);
    stream.close();
    return iRet;
}