private void setPort(EInternalPort port) throws Pausable {
assert(port != null);
assert(this.port == null);
assert(this.port_queue != null);
//System.err.println("EPeer: Patching port for "+node+" to "+port);
EDriverTask task = port.task();
assert(task != null);
/* Note: The following could be simpler if
* EDriverTask.outputv() wasn't Pausable - which it
* shouldn't be, but is as long as we're using
* bounded-size Mailboxes. -- eriksoe */
try {
// Empty port_queue, then set port:
while (true) {
ByteBuffer[] msg;
synchronized (this) {
if (port_queue.isEmpty()) { // Go from queueing mode to direct-port mode:
this.port = port;
this.port_queue = null;
break;
}
msg = port_queue.removeFirst();
assert(msg != null);
}
task.outputv(null, msg); // Pausable so called outside lock
}
} catch (IOException e) {
e.printStackTrace();
close_and_finish(port);
}