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; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame^] | 16 | private byte[] payload; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 17 | private transient NettyMessagingService messagingService; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame^] | 18 | public static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGIG_REQUEST_REPLY"; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | |
| 20 | // Must be created using the Builder. |
| 21 | private InternalMessage() {} |
| 22 | |
| 23 | public long id() { |
| 24 | return id; |
| 25 | } |
| 26 | |
| 27 | public String type() { |
| 28 | return type; |
| 29 | } |
| 30 | |
| 31 | public Endpoint sender() { |
| 32 | return sender; |
| 33 | } |
| 34 | |
| 35 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame^] | 36 | public byte[] payload() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 37 | return payload; |
| 38 | } |
| 39 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 40 | protected void setMessagingService(NettyMessagingService messagingService) { |
| 41 | this.messagingService = messagingService; |
| 42 | } |
| 43 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 44 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame^] | 45 | public void respond(byte[] data) throws IOException { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 46 | Builder builder = new Builder(messagingService); |
| 47 | InternalMessage message = builder.withId(this.id) |
| 48 | // FIXME: Sender should be messagingService.localEp. |
| 49 | .withSender(this.sender) |
| 50 | .withPayload(data) |
| 51 | .withType(REPLY_MESSAGE_TYPE) |
| 52 | .build(); |
| 53 | messagingService.sendAsync(sender, message); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Builder for InternalMessages. |
| 59 | */ |
Yuta HIGUCHI | 24fb299 | 2014-10-06 22:51:45 -0700 | [diff] [blame] | 60 | public static final class Builder { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 61 | private InternalMessage message; |
| 62 | |
| 63 | public Builder(NettyMessagingService messagingService) { |
| 64 | message = new InternalMessage(); |
| 65 | message.messagingService = messagingService; |
| 66 | } |
| 67 | |
| 68 | public Builder withId(long id) { |
| 69 | message.id = id; |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public Builder withType(String type) { |
| 74 | message.type = type; |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public Builder withSender(Endpoint sender) { |
| 79 | message.sender = sender; |
| 80 | return this; |
| 81 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame^] | 82 | public Builder withPayload(byte[] payload) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 83 | message.payload = payload; |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | public InternalMessage build() { |
| 88 | return message; |
| 89 | } |
| 90 | } |
Yuta HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 91 | } |