rserver.c 5.5 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 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 129
  char     dataName[20] = "server.data";

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

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

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

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

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
191
  processShellMsg();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
192

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

S
slguan 已提交
198 199
  return 0;
}