From 8c42a659c0cc9178d1f1305acb41dfbf4a8697ef Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 22 十月 2020 16:20:26 +0800
Subject: [PATCH] update

---
 test_net_socket/test_net_mod_socket.c |  197 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 178 insertions(+), 19 deletions(-)

diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index 2d9c94e..3cb1c56 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -1,42 +1,201 @@
-#include "net_mod_server_socket.h"
-#include "net_mod_socket.h"
+#include "net_mod_server_socket_wrapper.h"
+#include "net_mod_socket_wrapper.h"
 #include "shm_mm.h"
 #include "dgram_mod_socket.h"
+#include "usg_common.h"
 
-void server() {
-	// void *socket = dgram_mod_open_socket();
-	// ShmModSocket tt;
-	 ShmModSocket * m_socket = new ShmModSocket;
-	// NetModServerSocket(5000, 	NULL);
+typedef struct Targ {
+	int port;
+	int id;
 
-	NetModServerSocket *serverSocket  = new NetModServerSocket(5000, m_socket);
-	serverSocket->start();
+}Targ;
+
+void server(int port) {
+	void *serverSocket  = net_mod_server_socket_open(port);
+	if(net_mod_server_socket_start(serverSocket) != 0) {
+		err_exit(errno, "net_mod_server_socket_start");
+	}
 }
 
-void client(){
-	NetModSocket client("localhost", 5000);
-	char buf[MAXLINE];
+void client(int port ){
+	void * client = net_mod_socket_open();
+	char content[MAXLINE];
+	char action[512];
+  char topic[512];
+	
+	int recv_arr_size, i, n;
 
-  while (fgets(buf, MAXLINE, stdin) != NULL) {
-  	client.send(buf, strlen(buf));
+
+	net_mod_recv_msg_t *recv_arr;
+	//192.168.20.104
+  int node_arr_size = 2;
+	net_node_t node_arr[] = {
+		// {"192.168.5.22", port, 11},
+		{"192.168.20.10", port, 11},
+		{"192.168.20.104", port, 11}
+	};
+
+	int pub_node_arr_size = 3;
+	net_node_t pub_node_arr[] = {
+		{"192.168.5.22", port, 8},
+		{"192.168.20.10", port, 8},
+		{"192.168.20.104", port, 8}
+	};
+	
+  while (true) {
+    //printf("Usage: pub <topic> [content] or sub <topic>\n");
+    printf("Can I help you? pub, send or quit\n");
+    scanf("%s",action);
     
+    if(strcmp(action, "pub") == 0) {
+    	printf("Please input topic and content\n");
+      scanf("%s %s", topic, content);
+
+    	n = net_mod_socket_pub(client, pub_node_arr, pub_node_arr_size, topic, strlen(topic)+1, content, strlen(content)+1);
+    	printf("pub %d nodes\n", n);
+    }
+    else if(strcmp(action, "send") == 0) {
+    	getc(stdin);
+    	printf("Please input  content\n");
+    	
+		  if (fgets(content, MAXLINE, stdin) != NULL) {
+		  	// 鏀跺埌娑堟伅鐨勮妭鐐瑰嵆浣挎病鏈夊搴旂殑淇℃伅锛� 涔熻鍥炲涓�涓〃绀烘棤鐨勬秷鎭�,鍚﹀垯浼氫竴鐩寸瓑寰�
+		    n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
+		    printf("send %d nodes\n", n);
+		    for(i=0; i<recv_arr_size; i++) {
+		    	printf("host:%s, port: %d, key:%d, content: %s\n", 
+		    		recv_arr[i].host,
+		    		recv_arr[i].port,
+		    		recv_arr[i].key,
+		    		recv_arr[i].content
+		    	);
+		    }
+		    
+				// 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀
+		    net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
+		  }
+    }
+    else if(strcmp(action, "quit") == 0) {
+      break;
+    } else {
+      printf("error input argument\n");
+      continue;
+    }
+   
   }
+  net_mod_socket_close(client);
+
+  
+}
+ 
+
+#define  SCALE  100000
+
+void *runclient(void *arg) {
+  Targ *targ = (Targ *)arg;
+  int port = targ->port;
+  char sendbuf[512];
+ 
+  int i,j, n, recv_arr_size;
+  net_mod_recv_msg_t *recv_arr;
+
+  int node_arr_size = 1;
+	//192.168.20.104
+	net_node_t node_arr[] = {
+		//{0, port, 11}
+    {"192.168.20.10", port, 11}
+    // {"192.168.20.104", port, 11}
+	};
+
+  void * client = net_mod_socket_open();
+	
+	char filename[512];
+	sprintf(filename, "test%d.tmp", targ->id);
+	FILE *fp = NULL;
+	fp = fopen(filename, "w+");
+	// fp = stdout;
+
+	int recvsize;
+	void *recvbuf;
+  for (i = 0; i < SCALE; i++) {
+    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);
+    //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", 
+    		recv_arr[j].host,
+    		recv_arr[j].port,
+    		recv_arr[j].key,
+    		recv_arr[j].content
+    	);
+    }
+		// 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀
+		net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
+  }
+  fclose(fp);
+  net_mod_socket_close(client);
+  return (void *)i;
+}
+
+void mclient(int port) {
+
+  int status, i = 0, processors = 1;
+  void *res[processors];
+  // Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
+  Targ targs[processors];
+  pthread_t tids[processors];
+  char sendbuf[512];
+  struct timeval start, end;
+  long total = 0;
+  
+printf("寮�濮嬫祴璇�...\n");  
+  gettimeofday(&start, NULL);
+  for (i = 0; i < processors; i++) {
+    targs[i].port = port;
+    targs[i].id = i;
+    pthread_create(&tids[i], NULL, runclient, (void *)&targs[i]);
+  }
+
+  for (i = 0; i < processors; i++) {
+    if (pthread_join(tids[i], &res[i]) != 0) {
+      perror("multyThreadClient pthread_join");
+    } else {
+    	total += (long)res[i];
+      //fprintf(stderr, "client(%d) 鍐欏叆 %ld 鏉℃暟鎹甛n", i, (long)res[i]);
+    }
+  }
+
+  gettimeofday(&end, NULL);
+
+  double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec);
+  long diffsec = (long) (difftime/1000000);
+  long diffusec = difftime - diffsec*1000000;
+  fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total );
+  // fflush(stdout);
 }
 
 int main(int argc, char *argv[]) {
 	shm_init(512);
 
-	if (argc < 2) {
-	 fprintf(stderr, "Usage: %s %s|%s\n", argv[0], "server", "client");
-	 return 1;
+	int port;
+  if (argc < 3) {
+    fprintf(stderr, "Usage: %s %s|%s <PORT> \n", argv[0],  "server", "client");
+    return 1;
   }
 
+  port = atoi(argv[2]);
+	 
+
 	if (strcmp("server", argv[1]) == 0 ) {
-     server();
+     server(port);
   }
 
   if (strcmp("client", argv[1]) == 0)
-     client();
+     client(port);
+
+  if (strcmp("mclient", argv[1]) == 0)
+    mclient(port);
 }
 
 

--
Gitblit v1.8.0