| | |
| | | #include <nng/nng.h> |
| | | #include <nng/protocol/reqrep0/rep.h> |
| | | #include <nng/protocol/reqrep0/req.h> |
| | | #include <nng/protocol/survey0/survey.h> |
| | | #include <nng/protocol/survey0/respond.h> |
| | | |
| | | using namespace std; |
| | | |
| | |
| | | if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { |
| | | fatal("nng_recv", rv); |
| | | } |
| | | |
| | | printf("RECEIVED RPC REQUEST:\n %s", buf); |
| | | jsonreader.parse(buf, request); |
| | | nng_free(buf, sz); |
| | | std::string method = request["method"].asString(); |
| | |
| | | pthread_create(&tid, NULL, worker, NULL); |
| | | } |
| | | |
| | | |
| | | void heartBeat(const char *url, const char *name) |
| | | { |
| | | nng_socket sock; |
| | | int rv; |
| | | |
| | | if ((rv = nng_respondent0_open(&sock)) != 0) { |
| | | fatal("nng_respondent0_open", rv); |
| | | } |
| | | if ((rv = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)) != 0) { |
| | | fatal("nng_dial", rv); |
| | | } |
| | | for (;;) { |
| | | char *buf = NULL; |
| | | size_t sz; |
| | | if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) == 0) { |
| | | printf("CLIENT (%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, buf); |
| | | nng_free(buf, sz); |
| | | char response[1024]; |
| | | sprintf(response, "%s-%d", name, getpid()); |
| | | printf("CLIENT (%s): SENDING SURVEY RESPONSE:%s\n", name, response); |
| | | if ((rv = nng_send(sock, response, strlen(response) + 1, 0)) != 0) { |
| | | fatal("nng_send", rv); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void *heart(void *vargp) |
| | | { |
| | | pthread_detach(pthread_self()); |
| | | heartBeat(config.get("heart_server").c_str(), "netdisk"); |
| | | return NULL; |
| | | } |
| | | |
| | | // 心跳发送进程 |
| | | void initHeart() { |
| | | pthread_t tid; |
| | | pthread_create(&tid, NULL, heart, NULL); |
| | | } |
| | | |
| | | int main() |
| | | { |
| | | //环境变量初始化 |
| | |
| | | hcEnvConfig.libpath = "../lib/hc"; |
| | | HCNetdisk::netdisk_init(&hcEnvConfig); |
| | | |
| | | |
| | | |
| | | registRequestHandleFun(); |
| | | |
| | | initThreadPool(); |
| | | initHeart(); |
| | | |
| | | startServer(config.get("server_url").c_str()); |
| | | |