blob: da1427ad358662368411fe7e04b9ab8f099f6992 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 Jampaniab6d3112014-10-02 16:30:14 -070019package org.onlab.netty;
20
21import java.io.IOException;
22
23/**
24 * Internal message representation with additional attributes
25 * for supporting, synchronous request/reply behavior.
26 */
27public final class InternalMessage implements Message {
28
Yuta HIGUCHIc65f5122014-10-07 10:05:59 -070029 public static final String REPLY_MESSAGE_TYPE = "NETTY_MESSAGIG_REQUEST_REPLY";
30
Madan Jampaniab6d3112014-10-02 16:30:14 -070031 private long id;
32 private Endpoint sender;
33 private String type;
Madan Jampani53e44e62014-10-07 12:39:51 -070034 private byte[] payload;
Madan Jampaniab6d3112014-10-02 16:30:14 -070035 private transient NettyMessagingService messagingService;
Madan Jampaniab6d3112014-10-02 16:30:14 -070036
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 Jampani53e44e62014-10-07 12:39:51 -070053 public byte[] payload() {
Madan Jampaniab6d3112014-10-02 16:30:14 -070054 return payload;
55 }
56
Madan Jampani86ed0552014-10-03 16:45:42 -070057 protected void setMessagingService(NettyMessagingService messagingService) {
58 this.messagingService = messagingService;
59 }
60
Madan Jampaniab6d3112014-10-02 16:30:14 -070061 @Override
Madan Jampani53e44e62014-10-07 12:39:51 -070062 public void respond(byte[] data) throws IOException {
Madan Jampaniab6d3112014-10-02 16:30:14 -070063 Builder builder = new Builder(messagingService);
64 InternalMessage message = builder.withId(this.id)
Madan Jampani87100932014-10-21 16:46:12 -070065 .withSender(messagingService.localEp())
Madan Jampaniab6d3112014-10-02 16:30:14 -070066 .withPayload(data)
67 .withType(REPLY_MESSAGE_TYPE)
68 .build();
69 messagingService.sendAsync(sender, message);
70 }
71
72
73 /**
74 * Builder for InternalMessages.
75 */
Yuta HIGUCHI24fb2992014-10-06 22:51:45 -070076 public static final class Builder {
Madan Jampaniab6d3112014-10-02 16:30:14 -070077 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 Jampani53e44e62014-10-07 12:39:51 -070098 public Builder withPayload(byte[] payload) {
Madan Jampaniab6d3112014-10-02 16:30:14 -070099 message.payload = payload;
100 return this;
101 }
102
103 public InternalMessage build() {
104 return message;
105 }
106 }
Yuta HIGUCHI92626c02014-10-06 15:46:18 -0700107}