Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | package org.onlab.netty; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | /** |
| 24 | * Internal message representation with additional attributes |
| 25 | * for supporting, synchronous request/reply behavior. |
| 26 | */ |
| 27 | public final class InternalMessage implements Message { |
| 28 | |
Yuta HIGUCHI | c65f512 | 2014-10-07 10:05:59 -0700 | [diff] [blame] | 29 | public static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGIG_REQUEST_REPLY"; |
| 30 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 31 | private long id; |
| 32 | private Endpoint sender; |
| 33 | private String type; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 34 | private byte[] payload; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 35 | private transient NettyMessagingService messagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 36 | |
| 37 | // Must be created using the Builder. |
| 38 | private InternalMessage() {} |
| 39 | |
| 40 | public long id() { |
| 41 | return id; |
| 42 | } |
| 43 | |
| 44 | public String type() { |
| 45 | return type; |
| 46 | } |
| 47 | |
| 48 | public Endpoint sender() { |
| 49 | return sender; |
| 50 | } |
| 51 | |
| 52 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 53 | public byte[] payload() { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 54 | return payload; |
| 55 | } |
| 56 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 57 | protected void setMessagingService(NettyMessagingService messagingService) { |
| 58 | this.messagingService = messagingService; |
| 59 | } |
| 60 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 61 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 62 | public void respond(byte[] data) throws IOException { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 63 | Builder builder = new Builder(messagingService); |
| 64 | InternalMessage message = builder.withId(this.id) |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 65 | .withSender(messagingService.localEp()) |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 66 | .withPayload(data) |
| 67 | .withType(REPLY_MESSAGE_TYPE) |
| 68 | .build(); |
| 69 | messagingService.sendAsync(sender, message); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Builder for InternalMessages. |
| 75 | */ |
Yuta HIGUCHI | 24fb299 | 2014-10-06 22:51:45 -0700 | [diff] [blame] | 76 | public static final class Builder { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 77 | private InternalMessage message; |
| 78 | |
| 79 | public Builder(NettyMessagingService messagingService) { |
| 80 | message = new InternalMessage(); |
| 81 | message.messagingService = messagingService; |
| 82 | } |
| 83 | |
| 84 | public Builder withId(long id) { |
| 85 | message.id = id; |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public Builder withType(String type) { |
| 90 | message.type = type; |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | public Builder withSender(Endpoint sender) { |
| 95 | message.sender = sender; |
| 96 | return this; |
| 97 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 98 | public Builder withPayload(byte[] payload) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 99 | message.payload = payload; |
| 100 | return this; |
| 101 | } |
| 102 | |
| 103 | public InternalMessage build() { |
| 104 | return message; |
| 105 | } |
| 106 | } |
Yuta HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 107 | } |