Skip to content

Commit 77672d0

Browse files
committed
Implement failed callbacks in async IO on JVM.
1 parent 65bcfae commit 77672d0

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

src/vm/jvm/runtime/org/perl6/nqp/io/AsyncServerSocketHandle.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.channels.NotYetBoundException;
99

1010
import org.perl6.nqp.runtime.ExceptionHandling;
11+
import org.perl6.nqp.runtime.HLLConfig;
1112
import org.perl6.nqp.runtime.Ops;
1213
import org.perl6.nqp.runtime.ThreadContext;
1314
import org.perl6.nqp.sixmodel.SixModelObject;
@@ -42,15 +43,17 @@ public void accept(final ThreadContext tc, final AsyncTaskInstance task) {
4243
final CompletionHandler<AsynchronousSocketChannel, AsyncTaskInstance> handler
4344
= new CompletionHandler<AsynchronousSocketChannel, AsyncTaskInstance>() {
4445

46+
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
47+
final SixModelObject IOType = hllConfig.ioType;
48+
final SixModelObject Array = hllConfig.listType;
49+
final SixModelObject Null = hllConfig.nullValue;
50+
final SixModelObject Str = hllConfig.strBoxType;
51+
4552
@Override
4653
public void completed(AsynchronousSocketChannel channel, AsyncTaskInstance task) {
4754
listenChan.accept(task, this);
48-
49-
SixModelObject Array = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.listType;
50-
SixModelObject IOType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
51-
SixModelObject Null = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.nullValue;
52-
5355
ThreadContext curTC = tc.gc.getCurrentThreadContext();
56+
5457
AsyncSocketHandle handle = new AsyncSocketHandle(curTC, channel);
5558
IOHandleInstance ioHandle = (IOHandleInstance) IOType.st.REPR.allocate(curTC,
5659
IOType.st);
@@ -66,12 +69,7 @@ public void completed(AsynchronousSocketChannel channel, AsyncTaskInstance task)
6669

6770
@Override
6871
public void failed(Throwable exc, AsyncTaskInstance task) {
69-
70-
SixModelObject Array = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.listType;
71-
SixModelObject IOType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
72-
SixModelObject Str = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.strBoxType;
7372
ThreadContext curTC = tc.gc.getCurrentThreadContext();
74-
7573
SixModelObject result = Array.st.REPR.allocate(curTC, Array.st);
7674
result.push_boxed(curTC, task.schedulee);
7775
result.push_boxed(curTC, IOType);
@@ -85,5 +83,4 @@ public void failed(Throwable exc, AsyncTaskInstance task) {
8583
throw ExceptionHandling.dieInternal(tc, e);
8684
}
8785
}
88-
8986
}

src/vm/jvm/runtime/org/perl6/nqp/io/AsyncSocketHandle.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.charset.CharsetDecoder;
1111
import java.nio.charset.CharsetEncoder;
1212
import java.nio.charset.CoderResult;
13+
import java.nio.charset.CodingErrorAction;
1314

1415
import org.perl6.nqp.runtime.ExceptionHandling;
1516
import org.perl6.nqp.runtime.HLLConfig;
@@ -47,26 +48,33 @@ public void connect(final ThreadContext tc, String host, int port,
4748
final CompletionHandler<Void, AsyncTaskInstance> handler
4849
= new CompletionHandler<Void, AsyncTaskInstance>() {
4950

51+
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
52+
final SixModelObject IOType = hllConfig.ioType;
53+
final SixModelObject Array = hllConfig.listType;
54+
final SixModelObject Str = hllConfig.strBoxType;
55+
5056
@Override
5157
public void completed(Void v, AsyncTaskInstance task) {
5258
ThreadContext curTC = tc.gc.getCurrentThreadContext();
5359

54-
SixModelObject IOType = curTC.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
5560
IOHandleInstance ioHandle = (IOHandleInstance) IOType.st.REPR.allocate(curTC,
5661
IOType.st);
5762
ioHandle.handle = task.handle;
58-
59-
SixModelObject Array = curTC.curFrame.codeRef.staticInfo.compUnit.hllConfig.listType;
60-
SixModelObject result = Array.st.REPR.allocate(curTC, Array.st);
61-
result.push_boxed(curTC, task.schedulee);
62-
result.push_boxed(curTC, ioHandle);
63-
64-
((ConcBlockingQueueInstance) task.queue).push_boxed(curTC, result);
63+
callback(curTC, task, ioHandle, Str);
6564
}
6665

6766
@Override
68-
public void failed(Throwable exc, AsyncTaskInstance task) {
67+
public void failed(Throwable t, AsyncTaskInstance task) {
68+
ThreadContext curTC = tc.gc.getCurrentThreadContext();
69+
callback(curTC, task, IOType, Ops.box_s(t.toString(), Str, curTC));
70+
}
6971

72+
protected void callback(ThreadContext tc, AsyncTaskInstance task, SixModelObject ioHandle, SixModelObject err) {
73+
SixModelObject result = Array.st.REPR.allocate(tc, Array.st);
74+
result.push_boxed(tc, task.schedulee);
75+
result.push_boxed(tc, ioHandle);
76+
result.push_boxed(tc, err);
77+
((ConcBlockingQueueInstance) task.queue).push_boxed(tc, result);
7078
}
7179
};
7280

@@ -82,28 +90,33 @@ public void writeStr(final ThreadContext tc, final AsyncTaskInstance task, Strin
8290
try {
8391
ByteBuffer buffer = enc.encode(CharBuffer.wrap(toWrite));
8492

93+
HLLConfig hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
94+
final SixModelObject Array = hllConfig.listType;
95+
final SixModelObject Int = hllConfig.intBoxType;
96+
final SixModelObject Null = hllConfig.nullValue;
97+
final SixModelObject Str = hllConfig.strBoxType;
98+
8599
CompletionHandler<Integer, AsyncTaskInstance> handler
86100
= new CompletionHandler<Integer, AsyncTaskInstance>() {
87101

88102
@Override
89-
public void completed(Integer bytesWritten, AsyncTaskInstance attachment) {
90-
SixModelObject Array = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.listType;
91-
SixModelObject Int = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.intBoxType;
92-
SixModelObject Null = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.nullValue;
93-
103+
public void completed(Integer bytesWritten, AsyncTaskInstance task) {
94104
ThreadContext curTC = tc.gc.getCurrentThreadContext();
95-
96-
SixModelObject result = Array.st.REPR.allocate(curTC, Array.st);
97-
result.push_boxed(curTC, task.schedulee);
98-
result.push_boxed(curTC, Ops.box_i(bytesWritten, Int, curTC));
99-
result.push_boxed(curTC, Null);
100-
101-
((ConcBlockingQueueInstance) task.queue).push_boxed(curTC, result);
105+
callback(curTC, task, Ops.box_i(bytesWritten, Int, curTC), Null);
102106
}
103107

104108
@Override
105-
public void failed(Throwable exc, AsyncTaskInstance attachment) {
106-
// TODO Auto-generated method stub
109+
public void failed(Throwable t, AsyncTaskInstance attachment) {
110+
ThreadContext curTC = tc.gc.getCurrentThreadContext();
111+
callback(curTC, task, Str, Ops.box_s(t.toString(), Str, curTC));
112+
}
113+
114+
protected void callback(ThreadContext tc, AsyncTaskInstance task, SixModelObject bytesWritten, SixModelObject err) {
115+
SixModelObject result = Array.st.REPR.allocate(tc, Array.st);
116+
result.push_boxed(tc, task.schedulee);
117+
result.push_boxed(tc, bytesWritten);
118+
result.push_boxed(tc, err);
119+
((ConcBlockingQueueInstance) task.queue).push_boxed(tc, result);
107120
}
108121
};
109122

@@ -162,7 +175,7 @@ public void completed(Integer numRead, AsyncTaskInstance task) {
162175
@Override
163176
public void failed(Throwable t, AsyncTaskInstance task) {
164177
ThreadContext curTC = tc.gc.getCurrentThreadContext();
165-
callback(curTC, task, -1, Str, Ops.box_s(t.getMessage(), Str, tc));
178+
callback(curTC, task, -1, Str, Ops.box_s(t.toString(), Str, tc));
166179
}
167180

168181
protected void callback(ThreadContext tc, AsyncTaskInstance task, long seq, SixModelObject str, SixModelObject err) {

0 commit comments

Comments
 (0)