From b4209691aea6c60c7976613b9932e0a9a88e7301 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 5 Oct 2006 07:15:19 +0000 Subject: [PATCH] Code for Sockets and ServerSockets. Doesn't support outgoing connections yet. Doesn't cleanly abstract Input/OutputStreams. Not tested. --- Robust/src/ClassLibrary/ServerSocket.java | 10 ++- Robust/src/ClassLibrary/Socket.java | 24 +++++-- Robust/src/IR/Flat/BuildCode.java | 4 ++ Robust/src/Runtime/runtime.c | 88 ++++++++++++++++++++++- 4 files changed, 118 insertions(+), 8 deletions(-) diff --git a/Robust/src/ClassLibrary/ServerSocket.java b/Robust/src/ClassLibrary/ServerSocket.java index 0a1a1ea2..58ebc8f0 100644 --- a/Robust/src/ClassLibrary/ServerSocket.java +++ b/Robust/src/ClassLibrary/ServerSocket.java @@ -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(); } diff --git a/Robust/src/ClassLibrary/Socket.java b/Robust/src/ClassLibrary/Socket.java index a2a06272..1f0f32c0 100644 --- a/Robust/src/ClassLibrary/Socket.java +++ b/Robust/src/ClassLibrary/Socket.java @@ -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); + } } diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index 60a0d38f..f73c58b4 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -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"); diff --git a/Robust/src/Runtime/runtime.c b/Robust/src/Runtime/runtime.c index 64c2456e..cab31e81 100644 --- a/Robust/src/Runtime/runtime.c +++ b/Robust/src/Runtime/runtime.c @@ -19,9 +19,9 @@ jmp_buf error_handler; #include "SimpleHash.h" #include "GenericHashtable.h" #include -#include #include - +#include +#include #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___) { -- 2.34.1