Commit c75c4deb authored by Tommy Olofsson's avatar Tommy Olofsson
Browse files

Restructured DecoderChannel to be easier to subclass.

parent c6627bc8
MODULES=Constant \
Decoder \
DecoderChannel \
DynamicDecoderChannel \
DecoderRegistry \
Encoder \
EncoderChannel \
......
......@@ -8,9 +8,9 @@ import java.io.EOFException;
public class DecoderChannel implements Decoder {
private DataInputStream in;
private DecoderRegistry def_registry = new DecoderRegistry();
private DecoderRegistry ref_registry = new DecoderRegistry();
protected DataInputStream in;
protected DecoderRegistry def_registry = new DecoderRegistry();
protected DecoderRegistry ref_registry = new DecoderRegistry();
public DecoderChannel(InputStream in) throws IOException {
this.in = new DataInputStream(in);
......@@ -55,9 +55,38 @@ public class DecoderChannel implements Decoder {
}
}
protected void processSample(int tag) throws Exception {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
SampleDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
d.decodeAndHandle(this, h);
}
public void runOne() throws Exception {
boolean done = false;
while (!done) {
runOne(true);
}
/**
* Run the decoder.
*
* @param waitForSample Wether to wait until an actual sample has
* been decoded or to return after any complete entity has
* been decoded. Set to <code>true</code> to get the old
* behaviour.
*/
public void runOne(boolean waitForSample) throws Exception {
boolean done = !waitForSample;
do {
int tag = decodePacked32();
int length = decodePacked32();
switch (tag) {
......@@ -84,23 +113,11 @@ public class DecoderChannel implements Decoder {
processPragma(length);
} break;
default: {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
SampleDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
d.decodeAndHandle(this, h);
processSample(tag);
done = true;
}
}
}
} while (!done);
}
public void run() throws Exception {
......
package se.lth.control.labcomm;
import java.io.IOException;
import java.io.InputStream;
public class DynamicDecoderChannel extends DecoderChannel {
public DynamicDecoderChannel(InputStream in) throws IOException {
super(in);
}
public void runOne() throws Exception {
runOne(false);
}
protected void processSample(int tag) throws Exception {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null)
throw new IOException("Have not read any registration for " + tag);
SampleDispatcher d = e.getDispatcher();
if (d != null) {
SampleHandler h = e.getHandler();
if (h == null)
throw new IOException("No handler for '" + e.getName() +"'");
d.decodeAndHandle(this, h);
} else {
dynamicDecode();
}
}
private void dynamicDecode() {
throw new UnsupportedOperationException("Dynamic decoding not implemented yet.");
}
}
package se.lth.control.labcomm2006;
import java.io.IOException;
import java.io.InputStream;
public class DynamicDecoderChannel extends DecoderChannel {
public DynamicDecoderChannel(InputStream in) throws IOException {
super(in);
throw new UnsupportedOperationException("Use 2014.");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment