1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.nio.ch.sctp;
  26 
  27 import com.sun.nio.sctp.Association;
  28 import com.sun.nio.sctp.AssociationChangeNotification;
  29 import javax.tools.annotation.GenerateNativeHeader;
  30 
  31 /**
  32  * An implementation of AssociationChangeNotification
  33  */
  34 /* No native methods here, but the constants are needed in the supporting JNI code */
  35 @GenerateNativeHeader
  36 public class AssociationChange extends AssociationChangeNotification
  37     implements SctpNotification
  38 {
  39     /* static final ints so that they can be referenced from native */
  40     private final static int SCTP_COMM_UP = 1;
  41     private final static int SCTP_COMM_LOST = 2;
  42     private final static int SCTP_RESTART = 3;
  43     private final static int SCTP_SHUTDOWN = 4;
  44     private final static int SCTP_CANT_START = 5;
  45 
  46     private Association association;
  47 
  48     /* assocId is used to lookup the association before the notification is
  49      * returned to user code */
  50     private int assocId;
  51     private AssocChangeEvent event;
  52     private int maxOutStreams;
  53     private int maxInStreams;
  54 
  55     /* Invoked from native */
  56     private AssociationChange(int assocId,
  57                               int intEvent,
  58                               int maxOutStreams,
  59                               int maxInStreams) {
  60         switch (intEvent) {
  61             case SCTP_COMM_UP :
  62                 this.event = AssocChangeEvent.COMM_UP;
  63                 break;
  64             case SCTP_COMM_LOST :
  65                 this.event = AssocChangeEvent.COMM_LOST;
  66                 break;
  67             case SCTP_RESTART :
  68                 this.event = AssocChangeEvent.RESTART;
  69                 break;
  70             case SCTP_SHUTDOWN :
  71                 this.event = AssocChangeEvent.SHUTDOWN;
  72                 break;
  73             case SCTP_CANT_START :
  74                 this.event = AssocChangeEvent.CANT_START;
  75                 break;
  76             default :
  77                 throw new AssertionError(
  78                       "Unknown Association Change Event type: " + intEvent);
  79         }
  80 
  81         this.assocId = assocId;
  82         this.maxOutStreams = maxOutStreams;
  83         this.maxInStreams = maxInStreams;
  84     }
  85 
  86     @Override
  87     public int assocId() {
  88         return assocId;
  89     }
  90 
  91     @Override
  92     public void setAssociation(Association association) {
  93         this.association = association;
  94     }
  95 
  96     @Override
  97     public Association association() {
  98         assert association != null;
  99         return association;
 100     }
 101 
 102     @Override
 103     public AssocChangeEvent event() {
 104         return event;
 105     }
 106 
 107     int maxOutStreams() {
 108         return maxOutStreams;
 109     }
 110 
 111     int maxInStreams() {
 112         return maxInStreams;
 113     }
 114 
 115     @Override
 116     public String toString() {
 117         StringBuilder sb = new StringBuilder();
 118         sb.append(super.toString()).append(" [");
 119         sb.append("Association:").append(association);
 120         sb.append(", Event: ").append(event).append("]");
 121         return sb.toString();
 122     }
 123 }