|
| 1 | +package io.vertx.core.eventbus.impl; |
| 2 | + |
| 3 | +import io.vertx.core.Handler; |
| 4 | +import io.vertx.core.MultiMap; |
| 5 | +import io.vertx.core.eventbus.DeliveryOptions; |
| 6 | +import io.vertx.core.eventbus.Message; |
| 7 | +import io.vertx.core.eventbus.MessageStream; |
| 8 | +import io.vertx.core.impl.ContextInternal; |
| 9 | + |
| 10 | +class StreamBase extends HandlerRegistration implements MessageStream { |
| 11 | + |
| 12 | + MessageImpl base; |
| 13 | + private Handler<Message<String>> handler; |
| 14 | + private Handler<Void> endHandler; |
| 15 | + final String localAddress; |
| 16 | + String remoteAddress; |
| 17 | + private boolean halfClosed; |
| 18 | + |
| 19 | + StreamBase(String localAddress, ContextInternal context, EventBusImpl bus, String address, boolean src) { |
| 20 | + super(context, bus, address, src); |
| 21 | + this.localAddress = localAddress; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected boolean doReceive(Message msg) { |
| 26 | + if (msg.body() instanceof FinFrame) { |
| 27 | + Handler<Void> h = endHandler; |
| 28 | + if (h != null) { |
| 29 | + h.handle(null); |
| 30 | + } |
| 31 | + if (halfClosed) { |
| 32 | + unregister(); |
| 33 | + } else { |
| 34 | + halfClosed = true; |
| 35 | + } |
| 36 | + return true; |
| 37 | + } else { |
| 38 | + Handler<Message<String>> h = handler; |
| 39 | + if (h != null) { |
| 40 | + h.handle(msg); |
| 41 | + } |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void dispatch(Message msg, ContextInternal context, Handler handler) { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void handler(Handler<Message<String>> handler) { |
| 53 | + this.handler = handler; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void endHandler(Handler<Void> handler) { |
| 58 | + this.endHandler = handler; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void write(String body) { |
| 63 | + MessageImpl msg = base.createReply(body, new DeliveryOptions()); |
| 64 | + bus.sendOrPub(context, msg, new DeliveryOptions(), context.promise()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void end() { |
| 69 | + MessageImpl msg = base.createReply(new FinFrame(remoteAddress), new DeliveryOptions()); |
| 70 | + bus.sendOrPub(context, msg, new DeliveryOptions(), context.promise()); |
| 71 | + if (halfClosed) { |
| 72 | + unregister(); |
| 73 | + } else { |
| 74 | + halfClosed = true; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments