| | |
| | | LoggerConfig config; |
| | | FILE *logFile; |
| | | |
| | | void dolog(const char *fmt, va_list ap, int level, int err = 0) { |
| | | char buf[MAXLINE]; |
| | | |
| | | struct timeval tv; |
| | | struct tm *info; |
| | | gettimeofday(&tv, NULL); |
| | | info = localtime(&tv.tv_sec); |
| | | strftime(buf, MAXBUF - 1, "%Y-%d-%m %H:%M:%S", info); |
| | | snprintf(buf + strlen(buf), MAXBUF - strlen(buf) - 1, ",%ld [%s] ", tv.tv_usec, strlevel(level)); |
| | | vsnprintf(buf + strlen(buf), MAXBUF - strlen(buf) - 1, fmt, ap); |
| | | |
| | | if (err != 0) { |
| | | snprintf(buf + strlen(buf), MAXBUF - strlen(buf) - 1, ": %s", strerror(err)); |
| | | } |
| | | strcat(buf, "\n"); |
| | | fflush(stdout); /* in case stdout and stderr are the same */ |
| | | |
| | | if(logFile != NULL) { |
| | | fputs(buf, logFile); |
| | | } |
| | | if(config.console) { |
| | | fputs(buf, stdout); |
| | | } |
| | | |
| | | fflush(NULL); /* flushes all stdio output streams */ |
| | | } |
| | | void dolog(const char *fmt, va_list ap, int level, int err = 0); |
| | | |
| | | |
| | | void init(); |
| | |
| | | } |
| | | } |
| | | |
| | | int shm_recvfrom2(shm_socket_t *socket, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { |
| | | return 501; |
| | | } |
| | | |
| | | // 短连接方式接受 |
| | | int shm_recvfrom(shm_socket_t *sokt, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { |
| | |
| | | |
| | | shm_msg_t src; |
| | | |
| | | printf ("====== before ======\n"); |
| | | rv = sokt->queue->pop(src, timeout, flag); |
| | | printf ("====== after ======\n %d", rv); |
| | | |
| | | if (rv == 0) { |
| | | if(buf != NULL) { |
| | |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | fprintf(fp, "requst:%s\n", sendbuf); |
| | | // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000); |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000); |
| | | printf("send %d nodes\n", n); |
| | | for(j=0; j < recv_arr_size; j++) { |
| | | fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", |
| | |
| | | } |
| | | |
| | | |
| | | void *run_recv2(void *skptr) { |
| | | void *run_recv(void *skptr) { |
| | | pthread_detach(pthread_self()); |
| | | void *recvbuf; |
| | | int size; |
| | |
| | | while (true) { |
| | | printf("run_recv before\n"); |
| | | |
| | | rv = sk->recvfrom_timeout( &recvbuf, &size, &key, &timeout); |
| | | // rv = sk->recvfrom_timeout( &recvbuf, &size, &key, &timeout); |
| | | rv = sk->recvfrom( &recvbuf, &size, &key); |
| | | if(rv == 0) { |
| | | printf("收到订阅消息:%s\n", recvbuf); |
| | | free(recvbuf); |
| | |
| | | |
| | | } |
| | | |
| | | void *run_recv(void *skptr) { |
| | | void *run_recv2(void *skptr) { |
| | | pthread_detach(pthread_self()); |
| | | while(true) { |
| | | logger->debug("================run_recv\n"); |
| | | sleep(1); |
| | | // sleep(1); |
| | | } |
| | | } |
| | | |
| | |
| | | while (true) { |
| | | //printf("Usage: pub <topic> [content] or sub <topic>\n"); |
| | | printf("Can I help you? sub, pub, desub or quit %d\n", i++); |
| | | // sleep(100); |
| | | scanf("%s", action); |
| | | |
| | | printf("client 1\n"); |
| | | // scanf("%s", action); |
| | | std::cin >> action; |
| | | printf("client 2\n"); |
| | | if(strcmp(action, "sub") == 0) { |
| | | printf("Please input topic!\n"); |
| | | scanf("%s", topic); |