View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel;
17  
18  import java.net.SocketAddress;
19  
20  import org.jboss.netty.util.internal.StringUtil;
21  
22  /**
23   * The default downstream {@link MessageEvent} implementation.
24   */
25  public class DownstreamMessageEvent implements MessageEvent {
26  
27      private final Channel channel;
28      private final ChannelFuture future;
29      private final Object message;
30      private final SocketAddress remoteAddress;
31  
32      /**
33       * Creates a new instance.
34       */
35      public DownstreamMessageEvent(
36              Channel channel, ChannelFuture future,
37              Object message, SocketAddress remoteAddress) {
38  
39          if (channel == null) {
40              throw new NullPointerException("channel");
41          }
42          if (future == null) {
43              throw new NullPointerException("future");
44          }
45          if (message == null) {
46              throw new NullPointerException("message");
47          }
48          this.channel = channel;
49          this.future = future;
50          this.message = message;
51          if (remoteAddress != null) {
52              this.remoteAddress = remoteAddress;
53          } else {
54              this.remoteAddress = channel.getRemoteAddress();
55          }
56      }
57  
58      public Channel getChannel() {
59          return channel;
60      }
61  
62      public ChannelFuture getFuture() {
63          return future;
64      }
65  
66      public Object getMessage() {
67          return message;
68      }
69  
70      public SocketAddress getRemoteAddress() {
71          return remoteAddress;
72      }
73  
74      @Override
75      public String toString() {
76          if (getRemoteAddress() == getChannel().getRemoteAddress()) {
77              return getChannel().toString() + " WRITE: " +
78                     StringUtil.stripControlCharacters(getMessage());
79          } else {
80              return getChannel().toString() + " WRITE: " +
81                     StringUtil.stripControlCharacters(getMessage()) + " to " +
82                     getRemoteAddress();
83          }
84      }
85  }