From 46963c736e76cc0e23e361d7a59bbf95da3e8419 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 23 七月 2020 16:36:41 +0800
Subject: [PATCH] hear beat

---
 src/socket/include/shm_socket.h |    2 
 src/libshm_queue.a              |    0 
 src/socket/shm_socket.c         |   13 +++
 demo/queue                      |    0 
 test_socket/dgram_socket_test   |    0 
 src/socket/dgram_mod_socket.c   |   93 ----------------------
 demo/pub_sub                    |    0 
 test_socket/Makefile            |    2 
 test_socket/dgram_mod_survey    |    0 
 demo/dgram_mod_req_rep.c        |    4 
 src/queue/mm.c                  |    2 
 demo/dgram_mod_survey.c         |   58 ++++++++++++++
 test_socket/dgram_mod_survey.c  |   12 +-
 test_socket/dgram_mod_req_rep   |    0 
 demo/req_rep                    |    0 
 15 files changed, 85 insertions(+), 101 deletions(-)

diff --git a/demo/dgram_mod_req_rep.c b/demo/dgram_mod_req_rep.c
index a857ce6..ceaa18c 100644
--- a/demo/dgram_mod_req_rep.c
+++ b/demo/dgram_mod_req_rep.c
@@ -11,8 +11,8 @@
   int rv;
   int remote_port;
   while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
-    sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf);
-    puts(sendbuf);
+    printf( "REGIST CENTER RECEIVED REQUEST FROM %d: %s", remote_port, recvbuf);
+    sprintf(sendbuf, "RECEIVED FROM %d, %s", remote_port, recvbuf);
     dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
     free(recvbuf);
   }
diff --git a/demo/dgram_mod_survey.c b/demo/dgram_mod_survey.c
new file mode 100644
index 0000000..f19b6a1
--- /dev/null
+++ b/demo/dgram_mod_survey.c
@@ -0,0 +1,58 @@
+#include "dgram_mod_socket.h"
+#include "shm_mm.h"
+#include "usg_common.h"
+
+void server(int port) {
+  void *socket = dgram_mod_open_socket(SURVEY);
+  dgram_mod_bind(socket, port);
+  int size;
+  void *recvbuf;
+  char sendbuf[512];
+  int rv;
+  int remote_port;
+  while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
+    printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
+    free(recvbuf);
+  }
+  dgram_mod_close_socket(socket);
+}
+
+void client(int port) {
+  void *socket = dgram_mod_open_socket(SURVEY);
+  int size;
+  void *recvbuf;
+  char sendbuf[512];
+  long i = 0;
+  while (true) {
+    sprintf(sendbuf, "%d", i);
+    printf("SEND HEART:%s\n", sendbuf);
+    dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port);
+    free(recvbuf);
+    sleep(1);
+    i++;
+  }
+  dgram_mod_close_socket(socket);
+}
+
+ 
+
+int main(int argc, char *argv[]) {
+  shm_init(512);
+  int port;
+  if (argc < 3) {
+    fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client");
+    return 1;
+  }
+
+  port = atoi(argv[2]);
+
+  if (strcmp("server", argv[1]) == 0) {
+    server(port);
+  }
+
+  if (strcmp("client", argv[1]) == 0)
+    client(port);
+
+  
+  return 0;
+}
\ No newline at end of file
diff --git a/demo/pub_sub b/demo/pub_sub
index 093adca..ce02873 100755
--- a/demo/pub_sub
+++ b/demo/pub_sub
Binary files differ
diff --git a/demo/queue b/demo/queue
index 3676d0e..642a722 100755
--- a/demo/queue
+++ b/demo/queue
Binary files differ
diff --git a/demo/req_rep b/demo/req_rep
index a9c3f85..bfb33e1 100755
--- a/demo/req_rep
+++ b/demo/req_rep
Binary files differ
diff --git a/src/libshm_queue.a b/src/libshm_queue.a
index e50435f..396bc22 100644
--- a/src/libshm_queue.a
+++ b/src/libshm_queue.a
Binary files differ
diff --git a/src/queue/mm.c b/src/queue/mm.c
index 179f369..b09aa73 100644
--- a/src/queue/mm.c
+++ b/src/queue/mm.c
@@ -447,8 +447,6 @@
 static inline void rm_fblock(void *rbp)
 {
   // the successor of the previous block of rbp point to next block of rbp
-// err_msg(0, "SUCCRP(PREV_FBLKP(rbp) %p\n", SUCCRP(PREV_FBLKP(rbp)) );
-// err_msg(0, "NEXT_FBLKP(rbp)%p\n",        NEXT_FBLKP(rbp) );
 
   PUT_PTR(SUCCRP(PREV_FBLKP(rbp)), NEXT_FBLKP(rbp));
   // the predecessor of then next block of rbp point to previous block of rbp
diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c
index 5b24fc7..4dac9e8 100644
--- a/src/socket/dgram_mod_socket.c
+++ b/src/socket/dgram_mod_socket.c
@@ -28,18 +28,6 @@
 int dgram_mod_close_socket(void * _socket) {
 	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
 	shm_close_socket(socket->shm_socket);
-	// if(socket->recv_queue_map != NULL) {
-	// 	for(auto iter = socket->recv_queue_map->begin(); iter != socket->recv_queue_map->end(); iter++) {
-	// 		delete iter->second;
-	// 		socket->recv_queue_map->erase(iter);
-			
-	// 	}
-	// 	delete socket->recv_queue_map;
-	// }
-
-
-	// if(socket->recv_thread != 0)
-	// 	pthread_cancel(socket->recv_thread);
 	free(_socket);
 }
 
@@ -58,90 +46,13 @@
 int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) {
 
 	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	// if(socket->mod == REQ_REP && socket->recv_thread != 0) {
-	// 	err_exit(0, "you have used sendandrecv method os you can not use recvfrom method any more. these two method can not be used at the same time.");
-	// 	return -1;
-	// }
 	return shm_recvfrom(socket->shm_socket, buf, size, port);
 }
-
-// void *_dgram_mod_run_recv(void * _socket) {
-// 	pthread_detach(pthread_self());
-// 	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-// 	void *buf;
-// 	int size;
-// 	int port;
-// 	shm_msg_t msg;
-// 	LockFreeQueue<shm_msg_t, DM_Allocator> *queue;
-// 	std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter;
-// // printf("==============_dgram_mod_run_recv recv before\n");
-// 	while(shm_recvfrom(socket->shm_socket, &buf, &size, &port) == 0) {
-// 		if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) {
-// 			queue = iter->second;
-// 		} else {
-// 			queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16);
-// 			socket->recv_queue_map->insert({port, queue});
-// 		}
-
-// 		msg.buf = buf;
-// 		msg.size = size;
-// 		msg.port = port;
-// // printf("==============_dgram_mod_run_recv push before\n");
-// 		queue->push(msg);
-// // printf("==============_dgram_mod_run_recv push after\n");
-	 
-// 	}
-// 	return NULL;
-
-
-
-// }
-
+ 
 
 
 int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) {
-	
 	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	if(socket->mod != REQ_REP) {
-		err_exit(0, "you can't use this method other than REQ_REP mod!");
-	}
-	// if(socket->recv_queue_map == NULL) {
-	// 	socket->recv_queue_map = new std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >;
-	// }
-
-	// std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter;
-	// LockFreeQueue<shm_msg_t, DM_Allocator> *queue;
-	// if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) {
-	// 	queue = iter->second;
-	// } else {
-	// 	queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16);
-	// 	socket->recv_queue_map->insert({port, queue});
-	// }
-
-	// if (socket->recv_thread == 0) {
-		
-	// 	pthread_create(&(socket->recv_thread ), NULL, _dgram_mod_run_recv , _socket);
-		
-	// }
-
-	// shm_msg_t msg;
-	// if(queue->pop(msg)) {
-	// 	*recv_buf = msg.buf;
-	// 	*recv_size = msg.size;
-	// 	return 0;
-	// }
-
-	int recv_port;
-	int rv;
-
-	shm_socket_t *shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
-	if (shm_sendto(shm_socket, send_buf, send_size, send_port) == 0) {
-		rv = shm_recvfrom(shm_socket, recv_buf, recv_size, &recv_port);
-		shm_close_socket(shm_socket);
-		return rv;
-	}
-	
-	
-	return -1;
+	return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size);
 
 }
\ No newline at end of file
diff --git a/src/socket/include/shm_socket.h b/src/socket/include/shm_socket.h
index 30b85da..822450d 100644
--- a/src/socket/include/shm_socket.h
+++ b/src/socket/include/shm_socket.h
@@ -73,6 +73,8 @@
 
 int shm_send(shm_socket_t * socket, const void *buf, const int size) ;
 
+int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size);
+
 int shm_recv(shm_socket_t * socket, void **buf, int *size) ;
 
 int shm_sendto(shm_socket_t *socket, const void *buf, const int size, const int port);
diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c
index 5b8b9c0..6708469 100644
--- a/src/socket/shm_socket.c
+++ b/src/socket/shm_socket.c
@@ -302,6 +302,19 @@
 	}
 }
 
+int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) {
+	int recv_port;
+	int rv;
+
+	shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM);
+	if (shm_sendto(tmp_socket, send_buf, send_size, send_port) == 0) {
+		rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_port);
+		shm_close_socket(tmp_socket);
+		return rv;
+	}
+	return -1;
+}
+
 
 /**
  * 缁戝畾key鍒伴槦鍒楋紝浣嗘槸骞朵笉浼氬垱寤洪槦鍒椼�傚鏋滄病鏈夊搴旀寚瀹歬ey鐨勯槦鍒楁彁绀洪敊璇苟閫�鍑�
diff --git a/test_socket/Makefile b/test_socket/Makefile
index 214cd1a..dd1d80f 100644
--- a/test_socket/Makefile
+++ b/test_socket/Makefile
@@ -14,7 +14,7 @@
 include $(ROOT)/Make.defines.$(PLATFORM)
 
 
-PROGS =	dgram_socket_test dgram_mod_req_rep
+PROGS =	dgram_socket_test dgram_mod_req_rep dgram_mod_survey
 
 
 build: $(PROGS)
diff --git a/test_socket/dgram_mod_req_rep b/test_socket/dgram_mod_req_rep
index 2643d2f..3d62d71 100755
--- a/test_socket/dgram_mod_req_rep
+++ b/test_socket/dgram_mod_req_rep
Binary files differ
diff --git a/test_socket/dgram_mod_survey b/test_socket/dgram_mod_survey
new file mode 100755
index 0000000..dbb98dc
--- /dev/null
+++ b/test_socket/dgram_mod_survey
Binary files differ
diff --git a/test_socket/dgram_mod_survey.c b/test_socket/dgram_mod_survey.c
index 988877b..f19b6a1 100644
--- a/test_socket/dgram_mod_survey.c
+++ b/test_socket/dgram_mod_survey.c
@@ -11,7 +11,7 @@
   int rv;
   int remote_port;
   while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
-    printf( "鏀跺埌鏉ヨ嚜%d娉ㄥ唽淇℃伅: %s", remote_port, recvbuf);
+    printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
     free(recvbuf);
   }
   dgram_mod_close_socket(socket);
@@ -22,12 +22,14 @@
   int size;
   void *recvbuf;
   char sendbuf[512];
+  long i = 0;
   while (true) {
-    printf("request: ");
-    scanf("%s", sendbuf);
-    dgram_mod_send(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &size);
-    printf("reply: %s\n", (char *)recvbuf);
+    sprintf(sendbuf, "%d", i);
+    printf("SEND HEART:%s\n", sendbuf);
+    dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port);
     free(recvbuf);
+    sleep(1);
+    i++;
   }
   dgram_mod_close_socket(socket);
 }
diff --git a/test_socket/dgram_socket_test b/test_socket/dgram_socket_test
index 56cbe94..63e14ea 100755
--- a/test_socket/dgram_socket_test
+++ b/test_socket/dgram_socket_test
Binary files differ

--
Gitblit v1.8.0