Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added server socket and process pipe to IO.
- Loading branch information
Showing
6 changed files
with
246 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.perl6.nqp.io; | ||
|
|
||
| import org.perl6.nqp.runtime.ThreadContext; | ||
|
|
||
| public interface IIOBindable { | ||
|
|
||
| public void bind(ThreadContext tc, String host, int port); | ||
| public SocketHandle accept(ThreadContext tc); | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.perl6.nqp.io; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.lang.ProcessBuilder.Redirect; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.ByteChannel; | ||
| import java.nio.channels.Channels; | ||
| import java.nio.channels.ReadableByteChannel; | ||
| import java.nio.channels.WritableByteChannel; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Map; | ||
|
|
||
| import org.perl6.nqp.runtime.ExceptionHandling; | ||
| import org.perl6.nqp.runtime.ThreadContext; | ||
|
|
||
| public class ProcessHandle extends SyncHandle { | ||
|
|
||
| Process process; | ||
|
|
||
| public ProcessHandle(ThreadContext tc, String cmd, String dir, Map<String, String> env) { | ||
| ProcessBuilder pb = new ProcessBuilder("sh", "-c", cmd); | ||
| pb.directory(new File(dir)); | ||
| pb.redirectError(Redirect.INHERIT); | ||
|
|
||
| // Clear the JVM inherited environment and use provided only | ||
| Map<String, String> pbEnv = pb.environment(); | ||
| pbEnv.clear(); | ||
| pbEnv.putAll(env); | ||
|
|
||
| try { | ||
| process = pb.start(); | ||
| chan = new ProcessChannel(process.getOutputStream(), process.getInputStream()); | ||
| setEncoding(tc, Charset.forName("UTF-8")); | ||
| } catch (IOException e) { | ||
| throw ExceptionHandling.dieInternal(tc, e); | ||
| } | ||
| } | ||
|
|
||
| static class ProcessChannel implements ByteChannel { | ||
| protected WritableByteChannel stdin; | ||
| protected ReadableByteChannel stdout; | ||
|
|
||
| public ProcessChannel(OutputStream stdin, InputStream stdout) { | ||
| this.stdin = Channels.newChannel(stdin); | ||
| this.stdout = Channels.newChannel(stdout); | ||
| } | ||
|
|
||
| public int read(ByteBuffer dst) throws IOException { | ||
| return stdout.read(dst); | ||
| } | ||
|
|
||
| public boolean isOpen() { | ||
| return stdin.isOpen(); | ||
| } | ||
|
|
||
| public void close() throws IOException { | ||
| stdin.close(); | ||
| stdout.close(); | ||
| } | ||
|
|
||
| public int write(ByteBuffer src) throws IOException { | ||
| return stdin.write(src); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/vm/jvm/runtime/org/perl6/nqp/io/ServerSocketHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package org.perl6.nqp.io; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.channels.ServerSocketChannel; | ||
| import java.nio.channels.SocketChannel; | ||
|
|
||
| import org.perl6.nqp.runtime.ExceptionHandling; | ||
| import org.perl6.nqp.runtime.ThreadContext; | ||
|
|
||
| public class ServerSocketHandle implements IIOBindable { | ||
|
|
||
| ServerSocketChannel listenChan; | ||
|
|
||
| public ServerSocketHandle(ThreadContext tc) { | ||
| try { | ||
| listenChan = ServerSocketChannel.open(); | ||
| } catch (IOException e) { | ||
| ExceptionHandling.dieInternal(tc, e); | ||
| } | ||
| } | ||
|
|
||
| public void bind(ThreadContext tc, String host, int port) { | ||
| try { | ||
| InetSocketAddress addr = new InetSocketAddress(host, port); | ||
| listenChan.bind(addr); | ||
| } catch (IOException e) { | ||
| throw ExceptionHandling.dieInternal(tc, e); | ||
| } | ||
| } | ||
|
|
||
| public SocketHandle accept(ThreadContext tc) { | ||
| try { | ||
| SocketChannel chan = listenChan.accept(); | ||
| return chan == null ? null : new SocketHandle(tc, chan); | ||
| } catch (IOException e) { | ||
| throw ExceptionHandling.dieInternal(tc, e); | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters