Browse Source

network and serialization init

kaikas 7 years ago
parent
commit
51ad561681
6 changed files with 89 additions and 17 deletions
  1. 1 0
      src/myco-agent.c
  2. 2 7
      src/myco-daemon.c
  3. 0 5
      src/myco-indexer.c
  4. 47 3
      src/myco-modules-default.c
  5. 23 0
      src/myco-serialize.c
  6. 16 2
      test/myconetwork.c

+ 1 - 0
src/myco-agent.c

@@ -183,6 +183,7 @@ struct resource myco_agent_request_resource(int agent_message_queue_id, char *ag
     sprintf(msg.resource_name, "%s", resource_name);
     sprintf(msg.agent_name, "%s", agent_name);
     sprintf(msg.message, "REQUEST RESOURCE");
+    msg.sender_pid = getpid();
 
     msg = myco_send_and_receive(msg, myco_get_global_message_queue(), agent_message_queue_id);
 

+ 2 - 7
src/myco-daemon.c

@@ -325,12 +325,8 @@ int myco_daemon_request_resource(message msg) {
 		// Handle case where resource is on another node
 		// TODO: Request Resource information from indexer
 
-		// fetch resource
-		void *local_resource_pointer;
-		int local_resource_size;
-		char resource_name[RESOURCE_NAME_LENGTH];
-
-		myco_network_fetch_resource(local_resource_pointer, resource_name, local_resource_size);
+		// Fetch resource
+		myco_network_fetch_resource(msg.resource_pointer, msg.sender_pid, msg.resource_size);
 
 		// if no resource registered with indexer:
 		if (0 == 1) {
@@ -340,7 +336,6 @@ int myco_daemon_request_resource(message msg) {
 		}
 	}
 
-
 	return 0;
 }
 

+ 0 - 5
src/myco-indexer.c

@@ -278,11 +278,6 @@ int myco_indexer_start(int port) {
         if (new_socket > 0)
             inet_ntoa (address.sin_addr);
         do {
-            /*
-               printf ("Nachricht zum Versenden: ");
-               fgets (buffer, BUF, stdin);
-               send (new_socket, buffer, strlen (buffer), 0);
-               */
             size = recv (new_socket, buffer, BUF-1, 0);
             if( size > 0)
                 buffer[size] = '\0';

+ 47 - 3
src/myco-modules-default.c

@@ -14,12 +14,56 @@
    */
 
 
-// Serialize with protobuf-c
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
 // Request(local pointer, remote pointer, size)
 // make ping pong example
+myco_network_fetch_resource(void *local_pointer, pid_t local_pid, int size) {
+	printf("\nTransferring data over network!\n");
+
+	// Create local memory for transfer
+	void *resource_pointer = malloc(size);
+
+	// Create socket for transfer
+	int create_socket, new_socket;
+        socklen_t addrlen;
+        char *buffer = malloc (size);
+        //ssize_t size;
+        struct sockaddr_in address;
+        const int y = 1;
+    	char *command;
+	char *agent;
+	char *resource;
+
+	printf ("\e[2J");
+	create_socket=socket (AF_INET, SOCK_STREAM, 0);
+	setsockopt( create_socket, SOL_SOCKET,
+	    SO_REUSEADDR, &y, sizeof(int));
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = INADDR_ANY;
+	address.sin_port = htons (15001);
+	if (bind ( create_socket,
+		(struct sockaddr *) &address,
+		sizeof (address)) != 0) {
+	printf( "Port already taken!\n");
+	}
+	listen (create_socket, 5);
+	addrlen = sizeof (struct sockaddr_in);
+	
+	// Read memory over network
 
-myco_network_fetch_resource(void *local_pointer, char name[RESOURCE_NAME_LENGTH], int size) {
-	printf("Transferring data over network!\n");
+	// Write memory to Agent
+	if (myco_write_memory(local_pid, local_pointer, size, (void *)resource_pointer, size) == -1) {
+		printf("ERROR: %s\n", strerror(errno));
+		return -1;
+	}
 
 	return 0;
 }

+ 23 - 0
src/myco-serialize.c

@@ -0,0 +1,23 @@
+/* Copyright (C) 2016 Max Riechelmann <max.riechelmann@student.kit.edu>
+   (Karlsruhe Institute of Technology)
+   This library is free software; you can redistribute it and/or modify it
+   under the terms of the GNU Lesser General Public License as published by the
+   Free Software Foundation; either version 2.1 of the License, or (at your
+   option) any later version.
+   This library 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. See the GNU Lesser General Public License for more
+   details.
+   You should have received a copy of the GNU Lesser General Public License along
+   with this library; if not, write to the Free Software Foundation, Inc., 51
+   Franklin St, Fifth Floor, Boston, MA 02110, USA
+   */
+
+// TODO: serialize more data types or use a serialization framwork
+
+char myco_serialize(char message, void *pointer, int size) {
+	// TODO: ordering!
+	strcpy(message, pointer);
+
+	return message;
+}

+ 16 - 2
test/myconetwork.c

@@ -5,6 +5,7 @@
  */
 
 #include "../src/myco-agent.c"
+#include "../src/myco-serialize.c"
 
 int main() {
     char *agent_name = "DOMAIN1_MYAGENT";
@@ -19,8 +20,21 @@ int main() {
     // View available resources
     myco_agent_request_resource_list(agent_message_queue_id);
 
-    printf("Sleeping for 30 seconds...\n");
-    sleep(30);
+    printf("Sleeping for 10 seconds...\n");
+    sleep(10);
+
+    //TODO: Bandaid solution. This needs to be in the daemon later on.
+    printf("Sending data.\n");
+    struct sockaddr_in address;
+    int mysocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
+    address.sin_family = AF_INET;
+    address.sin_port = htons (15001);
+    inet_aton ("192.168.0.10", &address.sin_addr);
+    connect (mysocket, (struct sockaddr *) &address, sizeof (address));
+    // Handle indexer
+    char tcp_message[resource_size];
+    myco_serialize(tcp_message, resource_pointer, resource_size);
+    send(socket, tcp_message, strlen(tcp_message), 0);
 
     // At the end of its runtime the agent unregisters with the daemon.
     myco_agent_unregister(agent_name, agent_message_queue_id);