rserver.c 5.6 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

//#define _DEFAULT_SOURCE
#include "os.h"
S
slguan 已提交
18 19
#include "tglobal.h"
#include "rpcLog.h"
S
slguan 已提交
20
#include "trpc.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
21
#include "tqueue.h"
S
slguan 已提交
22

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
23 24 25
int msgSize = 128;
int commit = 0;
int dataFd = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
26
void *qhandle = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
27

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
28
void processShellMsg() {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29
  static int num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
30
  taos_qall  qall;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
31 32 33 34
  SRpcMsg   *pRpcMsg, rpcMsg;
  int        type;
 
  qall = taosAllocateQall();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
35 36

  while (1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
37
    int numOfMsgs = taosReadAllQitems(qhandle, qall);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
38
    if (numOfMsgs <= 0) {
39
      usleep(100);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40 41 42 43 44 45
      continue;
    }     

    tTrace("%d shell msgs are received", numOfMsgs);

    for (int i=0; i<numOfMsgs; ++i) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
46
      taosGetQitem(qall, &type, (void **)&pRpcMsg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
47 48
 
      if (dataFd >=0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49
        if ( write(dataFd, pRpcMsg->pCont, pRpcMsg->contLen) <0 ) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
50 51 52 53
          tPrint("failed to write data file, reason:%s", strerror(errno));
        }
      }
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
54

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
55 56 57 58 59
    if (commit >=2) {
      num += numOfMsgs;
      if ( fsync(dataFd) < 0 ) {
        tPrint("failed to flush data to file, reason:%s", strerror(errno));
      }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
61 62
      if (num % 10000 == 0) {
        tPrint("%d request have been written into disk", num);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
63
      }
64
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65 66 67 68
  
    taosResetQitems(qall);
    for (int i=0; i<numOfMsgs; ++i) {

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
69 70 71
      taosGetQitem(qall, &type, (void **)&pRpcMsg);
      rpcFreeCont(pRpcMsg->pCont);

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
72
      memset(&rpcMsg, 0, sizeof(rpcMsg));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
73 74
      rpcMsg.pCont = rpcMallocCont(msgSize);
      rpcMsg.contLen = msgSize;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
75
      rpcMsg.handle = pRpcMsg->handle;
76
      rpcMsg.code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
77
      rpcSendResponse(&rpcMsg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
78 79

      taosFreeQitem(pRpcMsg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
80 81 82 83
    }

  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84
  taosFreeQall(qall);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
85
/*
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
86 87 88 89 90 91 92
  SRpcIpSet ipSet;
  ipSet.numOfIps = 1;
  ipSet.index = 0;
  ipSet.port = 7000;
  ipSet.ip[0] = inet_addr("192.168.0.2");

  rpcSendRedirectRsp(ahandle, &ipSet);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
93
*/
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
94

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
95 96
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
int retrieveAuthInfo(char *meterId, char *spi, char *encrypt, char *secret, char *ckey) {
  // app shall retrieve the auth info based on meterID from DB or a data file
  // demo code here only for simple demo
  int ret = 0;

  if (strcmp(meterId, "michael") == 0) {
    *spi = 1;
    *encrypt = 0;
    strcpy(secret, "mypassword");
    strcpy(ckey, "key");
  } else if (strcmp(meterId, "jeff") == 0) {
    *spi = 0;
    *encrypt = 0;
  } else {
    ret = -1;  // user not there
  }

  return ret;
}

J
jtao1735 已提交
117
void processRequestMsg(SRpcMsg *pMsg, SRpcIpSet *pIpSet) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
118
  SRpcMsg *pTemp;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
119
 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
120 121 122 123 124
  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));

  tTrace("request is received, type:%d, contLen:%d, item:%p", pMsg->msgType, pMsg->contLen, pTemp);
  taosWriteQitem(qhandle, TAOS_QTYPE_RPC, pTemp); 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
125 126
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
127
int main(int argc, char *argv[]) {
S
slguan 已提交
128
  SRpcInit rpcInit;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
129 130
  char     dataName[20] = "server.data";

131 132
  taosBlockSIGPIPE();

S
slguan 已提交
133 134
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localPort    = 7000;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
135
  rpcInit.label        = "SER";
S
slguan 已提交
136
  rpcInit.numOfThreads = 1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
137
  rpcInit.cfp          = processRequestMsg;
S
slguan 已提交
138
  rpcInit.sessions     = 1000;
139
  rpcInit.idleTime     = tsShellActivityTimer*1500; 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
140
  rpcInit.afp          = retrieveAuthInfo;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
141 142

  for (int i=1; i<argc; ++i) {
143
    if (strcmp(argv[i], "-p")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
144
      rpcInit.localPort = atoi(argv[++i]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
145
    } else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
146 147 148 149 150 151 152 153 154 155 156
      rpcInit.numOfThreads = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-m")==0 && i < argc-1) {
      msgSize = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-s")==0 && i < argc-1) {
      rpcInit.sessions = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-o")==0 && i < argc-1) {
      tsCompressMsgSize = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-w")==0 && i < argc-1) {
      commit = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-d")==0 && i < argc-1) {
      rpcDebugFlag = atoi(argv[++i]);
157
      dDebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
158
      uDebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
159 160 161
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-p port]: server port number, default is:%d\n", rpcInit.localPort);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
162
      printf("  [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
163 164 165 166 167 168 169 170 171 172
      printf("  [-s sessions]: number of sessions, default is:%d\n", rpcInit.sessions);
      printf("  [-m msgSize]: message body size, default is:%d\n", msgSize);
      printf("  [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize);
      printf("  [-w write]: write received data to file(0, 1, 2), default is:%d\n", commit);
      printf("  [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag);
      printf("  [-h help]: print out this help\n\n");
      exit(0);
    }
  } 

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
173
  tsAsyncLog = 0;
174
  rpcInit.connType = TAOS_CONN_SERVER;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
175
  taosInitLog("server.log", 100000, 10);
S
slguan 已提交
176

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
177 178
  void *pRpc = rpcOpen(&rpcInit);
  if (pRpc == NULL) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
179
    tError("failed to start RPC server");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
180
    return -1;
S
slguan 已提交
181 182
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183
  tPrint("RPC server is running, ctrl-c to exit");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
184

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
185
  if (commit) {
186
    dataFd = open(dataName, O_APPEND | O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);  
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
187 188 189 190
    if (dataFd<0) 
      tPrint("failed to open data file, reason:%s", strerror(errno));
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
191
  qhandle = taosOpenQueue(sizeof(SRpcMsg));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
192

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
193
  processShellMsg();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
194

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
195 196 197 198
  if (dataFd >= 0) {
    close(dataFd);
    remove(dataName);
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
199

S
slguan 已提交
200 201
  return 0;
}