httpSystem.c 2.8 KB
Newer Older
H
hzcheng 已提交
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
slguan 已提交
16 17
#define _DEFAULT_SOURCE
#include "os.h"
S
slguan 已提交
18 19 20 21
#include "taos.h"
#include "tglobal.h"
#include "tsocket.h"
#include "ttimer.h"
S
slguan 已提交
22
#include "tadmin.h"
23 24 25 26
#include "httpInt.h"
#include "httpContext.h"
#include "httpSession.h"
#include "httpServer.h"
H
hzcheng 已提交
27 28
#include "httpResp.h"
#include "httpHandle.h"
S
TD-1204  
Shengliang Guan 已提交
29
#include "httpQueue.h"
S
Shengliang Guan 已提交
30 31 32
#include "httpGcHandle.h"
#include "httpRestHandle.h"
#include "httpTgHandle.h"
S
slguan 已提交
33

S
slguan 已提交
34 35 36 37
#ifndef _ADMIN
void adminInitHandle(HttpServer* pServer) {}
void opInitHandle(HttpServer* pServer) {}
#endif
S
slguan 已提交
38

39
HttpServer tsHttpServer;
H
hzcheng 已提交
40

S
TD-1311  
Shengliang Guan 已提交
41
int32_t httpInitSystem() {
42 43 44 45 46
  strcpy(tsHttpServer.label, "rest");
  tsHttpServer.serverIp = 0;
  tsHttpServer.serverPort = tsHttpPort;
  tsHttpServer.numOfThreads = tsHttpMaxThreads;
  tsHttpServer.processData = httpProcessData;
H
hzcheng 已提交
47

48
  pthread_mutex_init(&tsHttpServer.serverMutex, NULL);
H
hzcheng 已提交
49

50 51 52 53 54
  restInitHandle(&tsHttpServer);
  adminInitHandle(&tsHttpServer);
  gcInitHandle(&tsHttpServer);
  tgInitHandle(&tsHttpServer);
  opInitHandle(&tsHttpServer);
H
hzcheng 已提交
55 56 57 58

  return 0;
}

S
TD-1311  
Shengliang Guan 已提交
59
int32_t httpStartSystem() {
60
  httpInfo("start http server ...");
H
hzcheng 已提交
61

62 63
  if (tsHttpServer.status != HTTP_SERVER_INIT) {
    httpError("http server is already started");
H
hzcheng 已提交
64 65 66
    return -1;
  }

S
TD-1204  
Shengliang Guan 已提交
67 68 69 70 71
  if (!httpInitResultQueue()) {
    httpError("http init result queue failed");
    return -1;
  }

72
  if (!httpInitContexts()) {
73
    httpError("http init contexts failed");
H
hzcheng 已提交
74 75 76
    return -1;
  }

77
  if (!httpInitSessions()) {
H
hzcheng 已提交
78 79 80 81
    httpError("http init session failed");
    return -1;
  }

82
  if (!httpInitConnect()) {
H
hzcheng 已提交
83 84 85 86 87 88 89 90
    httpError("http init server failed");
    return -1;
  }

  return 0;
}

void httpStopSystem() {
91
  tsHttpServer.status = HTTP_SERVER_CLOSING;
S
TD-1207  
Shengliang Guan 已提交
92 93 94 95
  tsHttpServer.stop = 1;
#ifdef WINDOWS
  closesocket(tsHttpServer.fd);
#else
96
  shutdown(tsHttpServer.fd, SHUT_RD);
S
TD-1207  
Shengliang Guan 已提交
97
#endif
S
slguan 已提交
98
  tgCleanupHandle();
H
hzcheng 已提交
99 100 101
}

void httpCleanUpSystem() {
102
  httpInfo("http server cleanup");
H
hzcheng 已提交
103
  httpStopSystem();
S
slguan 已提交
104

105
  httpCleanUpConnect();
106 107
  httpCleanupContexts();
  httpCleanUpSessions();
S
TD-1204  
Shengliang Guan 已提交
108 109
  httpCleanupResultQueue();

110
  pthread_mutex_destroy(&tsHttpServer.serverMutex);
S
TD-1848  
Shengliang Guan 已提交
111
  tfree(tsHttpServer.pThreads);
112 113
  tsHttpServer.pThreads = NULL;
  
114
  tsHttpServer.status = HTTP_SERVER_CLOSED;
H
hzcheng 已提交
115 116
}

S
slguan 已提交
117
int32_t httpGetReqCount() {
118
  return atomic_exchange_32(&tsHttpServer.requestNum, 0);
H
hzcheng 已提交
119
}