use of com.predic8.membrane.core.model.AbstractExchangeViewerListener in project service-proxy by membrane.
the class LimitedMemoryExchangeStore method oldSnap.
private void oldSnap(AbstractExchange exc, Flow flow) {
// TODO: [fix me] support multi-snap
// TODO: [fix me] snap message headers and request *here*, not in observer/response
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void setExchangeFinished() {
inflight.remove(exc);
}
});
if (flow == Flow.REQUEST) {
exc.getRequest().addObserver(new MessageObserver() {
@Override
public void bodyRequested(AbstractBody body) {
}
@Override
public void bodyComplete(AbstractBody body) {
Response r = exc.getResponse();
if (r != null) {
AbstractBody b = r.getBody();
if (b != null && b.isRead())
// request-bodyComplete might occur after response-bodyComplete
return;
}
// System.out.println("Exchange put inflight " + exc.hashCode() + " " + exc.getRequest().getStartLine());
inflight.put(exc, exc.getRequest());
modify();
}
});
return;
}
try {
Message m = exc.getResponse();
if (m != null)
m.addObserver(new MessageObserver() {
public void bodyRequested(AbstractBody body) {
}
public void bodyComplete(AbstractBody body) {
snapInternal(exc, flow);
inflight.remove(exc);
modify();
// System.out.println("Exchange remove inflight " + exc.hashCode());
}
});
else {
inflight.remove(exc);
modify();
// System.out.println("Exchange remove inflight " + exc.hashCode() + " (2)");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of com.predic8.membrane.core.model.AbstractExchangeViewerListener in project service-proxy by membrane.
the class HttpClient method setupConnectionForwarding.
public static void setupConnectionForwarding(Exchange exc, final Connection con, final String protocol, StreamPump.StreamPumpStats streamPumpStats) throws SocketException {
final HttpServerHandler hsr = (HttpServerHandler) exc.getHandler();
String source = hsr.getSourceSocket().getRemoteSocketAddress().toString();
String dest = con.toString();
final StreamPump a;
final StreamPump b;
if ("WebSocket".equals(protocol)) {
WebSocketStreamPump aTemp = new WebSocketStreamPump(hsr.getSrcIn(), con.out, streamPumpStats, protocol + " " + source + " -> " + dest, exc.getRule(), true, exc);
WebSocketStreamPump bTemp = new WebSocketStreamPump(con.in, hsr.getSrcOut(), streamPumpStats, protocol + " " + source + " <- " + dest, exc.getRule(), false, null);
aTemp.init(bTemp);
bTemp.init(aTemp);
a = aTemp;
b = bTemp;
} else {
a = new StreamPump(hsr.getSrcIn(), con.out, streamPumpStats, protocol + " " + source + " -> " + dest, exc.getRule());
b = new StreamPump(con.in, hsr.getSrcOut(), streamPumpStats, protocol + " " + source + " <- " + dest, exc.getRule());
}
hsr.getSourceSocket().setSoTimeout(0);
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void setExchangeFinished() {
String threadName = Thread.currentThread().getName();
new Thread(b, threadName + " " + protocol + " Backward Thread").start();
try {
Thread.currentThread().setName(threadName + " " + protocol + " Onward Thread");
a.run();
} finally {
try {
con.close();
} catch (IOException e) {
log.debug("", e);
}
}
}
});
}
use of com.predic8.membrane.core.model.AbstractExchangeViewerListener in project service-proxy by membrane.
the class DynamicAbstractExchangeSnapshot method addObservers.
public static void addObservers(AbstractExchange exc, AbstractExchangeSnapshot excCopy, Consumer<AbstractExchangeSnapshot> callback) {
MessageObserver obs = new MessageObserver() {
@Override
public void bodyRequested(AbstractBody body) {
}
@Override
public void bodyComplete(AbstractBody body) {
update(callback, excCopy, exc);
}
};
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void addResponse(Response response) {
response.addObserver(obs);
}
@Override
public void setExchangeFinished() {
update(callback, excCopy, exc);
}
});
Stream.of(exc.getRequest(), exc.getResponse()).forEach(msg -> {
if (msg == null)
return;
if (msg.containsObserver(obs))
return;
msg.addObserver(obs);
});
update(callback, excCopy, exc);
}
use of com.predic8.membrane.core.model.AbstractExchangeViewerListener in project service-proxy by membrane.
the class LimitedMemoryExchangeStore method addObservers.
private void addObservers(AbstractExchange exc, AbstractExchange excCopy, Flow flow) throws Exception {
Message msg = null;
if (flow == Flow.REQUEST) {
msg = exc.getRequest();
} else
msg = exc.getResponse();
msg.addObserver(new MessageObserver() {
@Override
public void bodyRequested(AbstractBody body) {
}
@Override
public void bodyComplete(AbstractBody body) {
try {
cleanSnapshot(Exchange.updateCopy(exc, excCopy));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {
@Override
public void setExchangeFinished() {
try {
cleanSnapshot(Exchange.updateCopy(exc, excCopy));
} catch (Exception e) {
e.printStackTrace();
}
}
});
cleanSnapshot(Exchange.updateCopy(exc, excCopy));
}
Aggregations