View Javadoc

1   /*
2    * Copyright 2011 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 io.netty.channel.sctp;
17  
18  import com.sun.nio.sctp.AbstractNotificationHandler;
19  import com.sun.nio.sctp.AssociationChangeNotification;
20  import com.sun.nio.sctp.HandlerResult;
21  import com.sun.nio.sctp.Notification;
22  import com.sun.nio.sctp.PeerAddressChangeNotification;
23  import com.sun.nio.sctp.SendFailedNotification;
24  import com.sun.nio.sctp.ShutdownNotification;
25  
26  import io.netty.channel.ChannelPipeline;
27  
28  
29  /**
30   * {@link AbstractNotificationHandler} implementation which will handle all {@link Notification}s by trigger a
31   * {@link Notification} user event in the {@link ChannelPipeline} of a {@link SctpChannel}.
32   */
33  public final class SctpNotificationHandler extends AbstractNotificationHandler<Object> {
34  
35      private final SctpChannel sctpChannel;
36  
37      public SctpNotificationHandler(SctpChannel sctpChannel) {
38          if (sctpChannel == null) {
39              throw new NullPointerException("sctpChannel");
40          }
41          this.sctpChannel = sctpChannel;
42      }
43  
44      @Override
45      public HandlerResult handleNotification(AssociationChangeNotification notification, Object o) {
46          fireEvent(notification);
47          return HandlerResult.CONTINUE;
48      }
49  
50      @Override
51      public HandlerResult handleNotification(PeerAddressChangeNotification notification, Object o) {
52          fireEvent(notification);
53          return HandlerResult.CONTINUE;
54      }
55  
56      @Override
57      public HandlerResult handleNotification(SendFailedNotification notification, Object o) {
58          fireEvent(notification);
59          return HandlerResult.CONTINUE;
60      }
61  
62      @Override
63      public HandlerResult handleNotification(ShutdownNotification notification, Object o) {
64          fireEvent(notification);
65          sctpChannel.close();
66          return HandlerResult.RETURN;
67      }
68  
69      private void fireEvent(Notification notification) {
70          sctpChannel.pipeline().fireUserEventTriggered(notification);
71      }
72  }
73