rserver.c 5.2 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(int numOfMsgs, SRpcMsg *pMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29 30
  static int num = 0;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
31 32 33 34 35 36 37 38
  tTrace("%d shell msgs are received", numOfMsgs);

  for (int i=0; i<numOfMsgs; ++i) {
  
    if (dataFd >=0) {
      if ( write(dataFd, pMsg->msg, pMsg->msgLen) <0 ) {
        tPrint("failed to write data file, reason:%s", strerror(errno));
      }
39
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40 41 42 43 44
   
    void *rsp = rpcMallocCont(msgSize);
    rpcSendResponse(pMsg->handle, 1, rsp, msgSize);
    rpcFreeCont(pMsg->msg);
    pMsg++;
45
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
46
   
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
47
  if (commit >=2) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48
    num += numOfMsgs;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49 50 51 52 53 54 55 56 57
    if ( fsync(dataFd) < 0 ) {
      tPrint("failed to flush data to file, reason:%s", strerror(errno));
    }

    if (num % 10000 == 0) {
      tPrint("%d request have been written into disk", num);
    }
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
58

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
59
/*
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60 61 62 63 64 65 66
  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) 已提交
67
*/
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
68

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
69 70
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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) 已提交
91 92 93 94 95 96 97 98 99
void processRequestMsg(char type, void *pCont, int contLen, void *thandle, int32_t code) {
  tTrace("request is received, type:%d, contLen:%d", type, contLen);
  SRpcMsg rpcMsg;
  rpcMsg.msg = pCont;
  rpcMsg.msgLen = contLen;
  rpcMsg.code = code;
  rpcMsg.handle = thandle;
  rpcMsg.type = type;
  taosPutIntoMsgQueue(qhandle, &rpcMsg); 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100 101
}

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

S
slguan 已提交
106 107 108
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localIp      = "0.0.0.0";
  rpcInit.localPort    = 7000;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
109
  rpcInit.label        = "SER";
S
slguan 已提交
110
  rpcInit.numOfThreads = 1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
111
  rpcInit.cfp          = processRequestMsg;
S
slguan 已提交
112 113
  rpcInit.sessions     = 1000;
  rpcInit.idleTime     = 2000;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
114
  rpcInit.afp          = retrieveAuthInfo;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
115 116

  for (int i=1; i<argc; ++i) {
117
    if (strcmp(argv[i], "-p")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
118 119 120
      rpcInit.localPort = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-i")==0 && i < argc-1) {
      strcpy(rpcInit.localIp, argv[++i]); 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
121
    } else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
122 123 124 125 126 127 128 129 130 131 132
      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) 已提交
133
      uDebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
134 135 136 137
    } 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) 已提交
138
      printf("  [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
139 140 141 142 143 144 145 146 147 148
      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) 已提交
149
  tsAsyncLog = 0;
150
  rpcInit.connType = TAOS_CONN_SERVER;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
151 152

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
154 155
  void *pRpc = rpcOpen(&rpcInit);
  if (pRpc == NULL) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
156
    tError("failed to start RPC server");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
157
    return -1;
S
slguan 已提交
158 159
  }

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
162
  if (commit) {
163
    dataFd = open(dataName, O_APPEND | O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);  
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
164 165 166 167
    if (dataFd<0) 
      tPrint("failed to open data file, reason:%s", strerror(errno));
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
168 169
  qhandle = taosInitMsgQueue(1000, processShellMsg, "SER");

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
170 171 172 173
  // loop forever
  while(1) {
    sleep(1);
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
174

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
175 176 177 178
  if (dataFd >= 0) {
    close(dataFd);
    remove(dataName);
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
179

S
slguan 已提交
180 181 182 183
  return 0;
}