rserver.c 5.7 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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"
#include "tlog.h"
#include "trpc.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
20
#include "tqueue.h"
S
slguan 已提交
21 22
#include <stdint.h>

陶建辉(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 39 40 41 42 43 44 45
    if (numOfMsgs <= 0) {
      usleep(1000);
      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 73
      rpcMsg.pCont = rpcMallocCont(msgSize);
      rpcMsg.contLen = msgSize;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
74
      rpcMsg.handle = pRpcMsg->handle;
75
      rpcMsg.code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
76
      rpcSendResponse(&rpcMsg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
77 78

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

  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83
  taosFreeQall(qall);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84
/*
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
85 86 87 88 89 90 91
  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) 已提交
92
*/
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
93

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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;
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
116
void processRequestMsg(SRpcMsg *pMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
117 118 119 120 121 122 123
  SRpcMsg *pTemp;

  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) 已提交
124 125
}

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

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

  for (int i=1; i<argc; ++i) {
142
    if (strcmp(argv[i], "-p")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
143 144 145
      rpcInit.localPort = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-i")==0 && i < argc-1) {
      strcpy(rpcInit.localIp, argv[++i]); 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
146
    } else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
147 148 149 150 151 152 153 154 155 156 157
      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]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
158
      ddebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
159
      uDebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
160 161 162 163
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-i ip]: server IP address, default is:%s\n", rpcInit.localIp);
      printf("  [-p port]: server port number, default is:%d\n", rpcInit.localPort);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
164
      printf("  [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
165 166 167 168 169 170 171 172 173 174
      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) 已提交
175
  tsAsyncLog = 0;
176
  rpcInit.connType = TAOS_CONN_SERVER;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
177 178

  taosInitLog("server.log", 100000, 10);
S
slguan 已提交
179

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

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
196
  processShellMsg();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
197

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

S
slguan 已提交
203 204
  return 0;
}