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 static org.jboss.netty.channel.Channels.*;
19  
20  import java.net.SocketAddress;
21  
22  import org.jboss.netty.util.internal.StringUtil;
23  
24  /**
25   * The default upstream {@link MessageEvent} implementation.
26   */
27  public class UpstreamMessageEvent implements MessageEvent {
28  
29      private final Channel channel;
30      private final Object message;
31      private final SocketAddress remoteAddress;
32  
33      /**
34       * Creates a new instance.
35       */
36      public UpstreamMessageEvent(
37              Channel channel, Object message, SocketAddress remoteAddress) {
38  
39          if (channel == null) {
40              throw new NullPointerException("channel");
41          }
42          if (message == null) {
43              throw new NullPointerException("message");
44          }
45          this.channel = channel;
46          this.message = message;
47          if (remoteAddress != null) {
48              this.remoteAddress = remoteAddress;
49          } else {
50              this.remoteAddress = channel.getRemoteAddress();
51          }
52      }
53  
54      public Channel getChannel() {
55          return channel;
56      }
57  
58      public ChannelFuture getFuture() {
59          return succeededFuture(getChannel());
60      }
61  
62      public Object getMessage() {
63          return message;
64      }
65  
66      public SocketAddress getRemoteAddress() {
67          return remoteAddress;
68      }
69  
70      @Override
71      public String toString() {
72          if (getRemoteAddress() == getChannel().getRemoteAddress()) {
73              return getChannel().toString() + " RECEIVED: " +
74                     StringUtil.stripControlCharacters(getMessage());
75          } else {
76              return getChannel().toString() + " RECEIVED: " +
77                     StringUtil.stripControlCharacters(getMessage()) + " from " +
78                     getRemoteAddress();
79          }
80      }
81  }