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    * https://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  import io.netty.channel.ChannelPipeline;
26  import io.netty.util.internal.ObjectUtil;
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          this.sctpChannel = ObjectUtil.checkNotNull(sctpChannel, "sctpChannel");
39      }
40  
41      @Override
42      public HandlerResult handleNotification(AssociationChangeNotification notification, Object o) {
43          fireEvent(notification);
44          return HandlerResult.CONTINUE;
45      }
46  
47      @Override
48      public HandlerResult handleNotification(PeerAddressChangeNotification notification, Object o) {
49          fireEvent(notification);
50          return HandlerResult.CONTINUE;
51      }
52  
53      @Override
54      public HandlerResult handleNotification(SendFailedNotification notification, Object o) {
55          fireEvent(notification);
56          return HandlerResult.CONTINUE;
57      }
58  
59      @Override
60      public HandlerResult handleNotification(ShutdownNotification notification, Object o) {
61          fireEvent(notification);
62          sctpChannel.close();
63          return HandlerResult.RETURN;
64      }
65  
66      private void fireEvent(Notification notification) {
67          sctpChannel.pipeline().fireUserEventTriggered(notification);
68      }
69  }
70