Skip to content

Commit

Permalink
Initial ops for socket IO using NIO.
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldh committed Aug 30, 2013
1 parent 83ea6f2 commit 59fb7d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -1930,6 +1930,9 @@ QAST::OperationsJAST.map_classlib_core_op('openasync', $TYPE_OPS, 'openasync', [
QAST::OperationsJAST.map_classlib_core_op('slurpasync', $TYPE_OPS, 'slurpasync', [$RT_OBJ, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('linesasync', $TYPE_OPS, 'linesasync', [$RT_OBJ, $RT_OBJ, $RT_INT, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);

QAST::OperationsJAST.map_classlib_core_op('socket', $TYPE_OPS, 'socket', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('connect', $TYPE_OPS, 'connect', [$RT_OBJ, $RT_STR, $RT_INT], $RT_OBJ, :tc);

QAST::OperationsJAST.map_classlib_core_op('debugnoop', $TYPE_OPS, 'debugnoop', [$RT_OBJ], $RT_OBJ, :tc);

# terms
Expand Down
14 changes: 11 additions & 3 deletions src/vm/jvm/runtime/org/perl6/nqp/io/SocketHandle.java
Expand Up @@ -10,13 +10,21 @@

public class SocketHandle extends SyncHandle {

public SocketHandle(ThreadContext tc, String host, int port) {
public SocketHandle(ThreadContext tc) {
try {
InetSocketAddress addr = InetSocketAddress.createUnresolved(host, port);
chan = SocketChannel.open(addr);
chan = SocketChannel.open();
setEncoding(tc, Charset.forName("UTF-8"));
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}

public void connect(ThreadContext tc, String host, int port) {
try {
InetSocketAddress addr = new InetSocketAddress(host, port);
((SocketChannel)chan).connect(addr);
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}
}
18 changes: 17 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -11,11 +11,11 @@
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.perl6.nqp.io.IIOSeekable;
import org.perl6.nqp.io.IIOSyncReadable;
import org.perl6.nqp.io.IIOSyncWritable;
import org.perl6.nqp.io.SocketHandle;
import org.perl6.nqp.io.StandardReadHandle;
import org.perl6.nqp.io.StandardWriteHandle;
import org.perl6.nqp.jast2bc.JASTToJVMBytecode;
Expand Down Expand Up @@ -280,6 +281,21 @@ public static SixModelObject openasync(String path, String mode, ThreadContext t
return h;
}

public static SixModelObject socket(ThreadContext tc) {
SixModelObject IOType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
IOHandleInstance h = (IOHandleInstance)IOType.st.REPR.allocate(tc, IOType.st);
h.handle = new SocketHandle(tc);
return h;
}

public static SixModelObject connect(SixModelObject obj, String host, long port, ThreadContext tc) {
IOHandleInstance h = (IOHandleInstance)obj;
if (h.handle instanceof SocketHandle) {
((SocketHandle)h.handle).connect(tc, host, (int) port);
}
return obj;
}

public static long filereadable(String path, ThreadContext tc) {
Path path_o;
long res;
Expand Down

0 comments on commit 59fb7d2

Please sign in to comment.