Code for Sockets and ServerSockets. Doesn't support outgoing connections yet. Doesn...
authorbdemsky <bdemsky>
Thu, 5 Oct 2006 07:15:19 +0000 (07:15 +0000)
committerbdemsky <bdemsky>
Thu, 5 Oct 2006 07:15:19 +0000 (07:15 +0000)
Not tested.

Robust/src/ClassLibrary/ServerSocket.java
Robust/src/ClassLibrary/Socket.java
Robust/src/IR/Flat/BuildCode.java
Robust/src/Runtime/runtime.c

index 0a1a1ea2ce3273bd1fa3b0ef24685820f17e08bd..58ebc8f0c4deff3e4a9c2f09789ebc4d895c1093 100644 (file)
@@ -10,7 +10,15 @@ public class ServerSocket {
        this.fd=createSocket(port);
     }
     
-    public Socket accept();
+    public Socket accept() {
+       Socket s=new Socket();
+       int newfd=nativeaccept(s, fd);
+       s.setFD(newfd);
+       return s;
+    }
+
+    private static native int nativeaccept(Socket s,int fd);
+    
     public void close();
 
 }
index a2a06272b58cbaa7b73d1c918c911b28513f5f25..1f0f32c0570e7eaf8bb35cccfbde6ff080fdb536 100644 (file)
@@ -4,11 +4,25 @@ public class Socket {
     /* File Descriptor */
     int fd;
     
-    private Socket(int fd) {
-       this.fd=fd;
+    Socket() {
     }
     
-    public int read(byte[] b);
-    public void write(byte[] b);
-    void close();
+    int setFD(int filed) {
+       fd=filed;
+    }
+
+    public int read(byte[] b) {
+       return nativeRead(b, fd);
+    }
+    public void write(byte[] b) {
+       nativeWrite(b, fd);
+    }
+
+    private native static int nativeRead(byte[] b, int fd);
+    private native static void nativeWrite(byte[] b, int fd);
+    private native static void nativeClose(int fd);
+
+    public void close() {
+       nativeClose(fd);
+    }
 }
index 60a0d38f9f10f9c89641ec2095be7b73e0ff051c..f73c58b4b23252b24b7b65416bb1981ed951dce4 100644 (file)
@@ -110,6 +110,10 @@ public class BuildCode {
            //Print out definition for array type
            outclassdefs.println("struct "+arraytype+" {");
            outclassdefs.println("  int type;");
+           if (state.TASK) {
+               outclassdefs.println("  int flag;");
+               outclassdefs.println("  void * flagptr;");
+           }
            printClassStruct(typeutil.getClass(TypeUtil.ObjectClass), outclassdefs);
            outclassdefs.println("  int ___length___;");
            outclassdefs.println("};\n");
index 64c2456e062a58a739342971d3ec6f284d95d9bc..cab31e81fe44f325ded124a3541c9322cd345081 100644 (file)
@@ -19,9 +19,9 @@ jmp_buf error_handler;
 #include "SimpleHash.h"
 #include "GenericHashtable.h"
 #include <sys/select.h>
-#include <sys/types.h>
 #include <sys/socket.h>
-
+#include <fcntl.h>
+#include <arpa/inet.h>
 
 #ifdef CONSCHECK
 #include "instrument.h"
@@ -298,6 +298,90 @@ void processtasks() {
     }
   }
 }
+
+
+
+int ___ServerSocket______createSocket____I(struct ___ServerSocket___ * sock, int port) {
+  int fd=socket(AF_INET, SOCK_STREAM, 0);
+  int n=1;
+  struct sockaddr_in sin;
+
+  bzero (&sin, sizeof (sin));
+  sin.sin_family = AF_INET;
+  sin.sin_port = htons (port);
+  sin.sin_addr.s_addr = htonl (INADDR_ANY);
+  
+  if (fd<0)
+    longjmp(error_handler,5);
+  
+  if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
+    close(fd);
+    longjmp(error_handler, 6);
+  }
+  fcntl(fd, F_SETFD, 1);
+  fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
+
+  /* bind to port */
+  if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) { 
+    close (fd);
+    longjmp(error_handler, 7);
+  }
+
+  /* listen */
+  if (listen(fd, 10)<0) { 
+    close (fd);
+    longjmp(error_handler, 8);
+  }
+
+  /* Store the fd/socket object mapping */
+  RuntimeHashadd(fdtoobject, fd, (int) sock);
+  addreadfd(fd);
+
+  return fd;
+}
+
+int ___ServerSocket______nativeaccept____L___Socket____I(struct ___Socket___ * sock, int fd) {
+  struct sockaddr_in sin;
+  unsigned int sinlen=sizeof(sin);
+  int newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
+
+  if (newfd<0) { 
+    longjmp(error_handler, 9);
+  }
+  fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
+
+  RuntimeHashadd(fdtoobject, fd, (int) sock);
+  addreadfd(fd);
+  return newfd;
+}
+
+void ___Socket______nativeWrite_____AR_C_I(struct ArrayObject * ao, int fd) {
+  int length=ao->___length___;
+  char * charstr=((char *)& ao->___length___)+sizeof(int);
+  int bytewritten=write(fd, charstr, length);
+  if (bytewritten!=length) {
+    printf("ERROR IN NATIVEWRITE\n");
+  }
+}
+
+int ___Socket______nativeRead_____AR_C_I(struct ArrayObject * ao, int fd) {
+  int length=ao->___length___;
+  char * charstr=((char *)& ao->___length___)+sizeof(int);
+  int byteread=read(fd, charstr, length);
+  
+  if (byteread<0) {
+    printf("ERROR IN NATIVEREAD\n");
+  }
+  return byteread;
+}
+
+void ___Socket______nativeClose____I(int fd) {
+  int data;
+  RuntimeHashget(fdtoobject, fd, &data);
+  RuntimeHashremove(fdtoobject, fd, data);
+  removereadfd(fd);
+  close(fd);
+}
 #endif
 
 int ___Object______hashcode____(struct ___Object___ * ___this___) {