Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 1 | package org.onlab.netty; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | /** |
| 6 | * Internal message representation with additional attributes |
| 7 | * for supporting, synchronous request/reply behavior. |
| 8 | */ |
| 9 | public final class InternalMessage implements Message { |
| 10 | |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame^] | 11 | public static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGIG_REQUEST_REPLY"; |
| 12 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 13 | private long id; |
| 14 | private Endpoint sender; |
| 15 | private String type; |
| 16 | private Object payload; |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame^] | 17 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 18 | private transient NettyMessagingService messagingService; |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame^] | 19 | // TODO: add transient payload serializer or change payload type to |
| 20 | // byte[], ByteBuffer, etc. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 21 | |
| 22 | // Must be created using the Builder. |
| 23 | private InternalMessage() {} |
| 24 | |
| 25 | public long id() { |
| 26 | return id; |
| 27 | } |
| 28 | |
| 29 | public String type() { |
| 30 | return type; |
| 31 | } |
| 32 | |
| 33 | public Endpoint sender() { |
| 34 | return sender; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public Object payload() { |
| 39 | return payload; |
| 40 | } |
| 41 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 42 | protected void setMessagingService(NettyMessagingService messagingService) { |
| 43 | this.messagingService = messagingService; |
| 44 | } |
| 45 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 46 | @Override |
| 47 | public void respond(Object data) throws IOException { |
| 48 | Builder builder = new Builder(messagingService); |
| 49 | InternalMessage message = builder.withId(this.id) |
| 50 | // FIXME: Sender should be messagingService.localEp. |
| 51 | .withSender(this.sender) |
| 52 | .withPayload(data) |
| 53 | .withType(REPLY_MESSAGE_TYPE) |
| 54 | .build(); |
| 55 | messagingService.sendAsync(sender, message); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Builder for InternalMessages. |
| 61 | */ |
Yuta HIGUCHI | 24fb299 | 2014-10-06 22:51:45 -0700 | [diff] [blame] | 62 | public static final class Builder { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 63 | private InternalMessage message; |
| 64 | |
| 65 | public Builder(NettyMessagingService messagingService) { |
| 66 | message = new InternalMessage(); |
| 67 | message.messagingService = messagingService; |
| 68 | } |
| 69 | |
| 70 | public Builder withId(long id) { |
| 71 | message.id = id; |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | public Builder withType(String type) { |
| 76 | message.type = type; |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | public Builder withSender(Endpoint sender) { |
| 81 | message.sender = sender; |
| 82 | return this; |
| 83 | } |
| 84 | public Builder withPayload(Object payload) { |
| 85 | message.payload = payload; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public InternalMessage build() { |
| 90 | return message; |
| 91 | } |
| 92 | } |
Yuta HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 93 | } |