client.cpp 2.5 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#include "sut.h"
H
Haojun Liao 已提交
17
#include "tdatablock.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static void processClientRsp(void* parent, SRpcMsg* pRsp, SEpSet* pEpSet) {
S
Shengliang Guan 已提交
20
  TestClient* client = (TestClient*)parent;
S
Shengliang Guan 已提交
21
  client->SetRpcRsp(pRsp);
dengyihao's avatar
fix bug  
dengyihao 已提交
22
  uInfo("x response:%s from dnode, code:0x%x, msgSize: %d", TMSG_INFO(pRsp->msgType), pRsp->code, pRsp->contLen);
S
Shengliang Guan 已提交
23 24 25
  tsem_post(client->GetSem());
}

dengyihao's avatar
fix bug  
dengyihao 已提交
26
void TestClient::SetRpcRsp(SRpcMsg* rsp) {
dengyihao's avatar
dengyihao 已提交
27
  if (this->pRsp) {
wafwerar's avatar
wafwerar 已提交
28
    taosMemoryFree(this->pRsp);
dengyihao's avatar
dengyihao 已提交
29
  }
wafwerar's avatar
wafwerar 已提交
30
  this->pRsp = (SRpcMsg*)taosMemoryCalloc(1, sizeof(SRpcMsg));
dengyihao's avatar
fix bug  
dengyihao 已提交
31 32 33 34 35
  this->pRsp->msgType = rsp->msgType;
  this->pRsp->code = rsp->code;
  this->pRsp->pCont = rsp->pCont;
  this->pRsp->contLen = rsp->contLen;
};
S
Shengliang Guan 已提交
36 37 38

tsem_t* TestClient::GetSem() { return &sem; }

dengyihao's avatar
fix bug  
dengyihao 已提交
39
void TestClient::DoInit() {
S
Shengliang Guan 已提交
40 41
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
  taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt);
S
Shengliang Guan 已提交
42 43
  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
dengyihao's avatar
fix bug  
dengyihao 已提交
44
  rpcInit.label = (char*)"shell";
S
Shengliang Guan 已提交
45 46 47 48 49
  rpcInit.numOfThreads = 1;
  rpcInit.cfp = processClientRsp;
  rpcInit.sessions = 1024;
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.idleTime = 30 * 1000;
dengyihao's avatar
fix bug  
dengyihao 已提交
50
  rpcInit.user = (char*)this->user;
dengyihao's avatar
dengyihao 已提交
51
  // rpcInit.ckey = (char*)"key";
S
Shengliang Guan 已提交
52
  rpcInit.parent = this;
dengyihao's avatar
dengyihao 已提交
53 54
  // rpcInit.secret = (char*)secretEncrypt;
  // rpcInit.spi = 1;
S
Shengliang Guan 已提交
55 56 57

  clientRpc = rpcOpen(&rpcInit);
  ASSERT(clientRpc);
dengyihao's avatar
fix bug  
dengyihao 已提交
58 59
  tsem_init(&this->sem, 0, 0);
}
S
Shengliang Guan 已提交
60

61
bool TestClient::Init(const char* user, const char* pass) {
dengyihao's avatar
fix bug  
dengyihao 已提交
62 63
  strcpy(this->user, user);
  strcpy(this->pass, pass);
dengyihao's avatar
dengyihao 已提交
64
  this->pRsp = NULL;
dengyihao's avatar
fix bug  
dengyihao 已提交
65
  this->DoInit();
S
Shengliang Guan 已提交
66 67 68 69 70 71 72 73
  return true;
}

void TestClient::Cleanup() {
  tsem_destroy(&sem);
  rpcClose(clientRpc);
}

dengyihao's avatar
fix bug  
dengyihao 已提交
74 75 76 77
void TestClient::Restart() {
  this->Cleanup();
  this->DoInit();
}
78

S
Shengliang Guan 已提交
79
SRpcMsg* TestClient::SendReq(SRpcMsg* pReq) {
S
Shengliang Guan 已提交
80
  SEpSet epSet = {0};
81
  addEpIntoEpSet(&epSet, tsLocalFqdn, tsServerPort);
S
Shengliang Guan 已提交
82
  rpcSendRequest(clientRpc, &epSet, pReq, NULL);
S
Shengliang Guan 已提交
83
  tsem_wait(&sem);
dengyihao's avatar
fix bug  
dengyihao 已提交
84
  uInfo("y response:%s from dnode, code:0x%x, msgSize: %d", TMSG_INFO(pRsp->msgType), pRsp->code, pRsp->contLen);
S
Shengliang Guan 已提交
85 86 87

  return pRsp;
}