MessageInfoImpl.java 4.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * 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 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24
 */
25
package sun.nio.ch.sctp;
26 27 28 29 30 31 32 33

import java.net.SocketAddress;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.Association;

/**
 * An implementation of a MessageInfo.
 */
34
public class MessageInfoImpl extends MessageInfo {
35 36 37 38 39 40 41 42 43 44 45
    private final SocketAddress address;
    private final int bytes;          /* 0 */

    private Association association;
    private int assocId;
    private int streamNumber;
    private boolean complete = true;
    private boolean unordered;  /* false */
    private long timeToLive;    /* 0L */
    private int ppid;           /* 0 */

46 47 48
    public MessageInfoImpl(Association association,
                           SocketAddress address,
                           int streamNumber) {
49 50 51 52 53 54 55
        this.association = association;
        this.address = address;
        this.streamNumber = streamNumber;
        bytes = 0;
    }

    /* Invoked from native */
56 57 58 59 60 61 62
    private MessageInfoImpl(int assocId,
                            SocketAddress address,
                            int bytes,
                            int streamNumber,
                            boolean complete,
                            boolean unordered,
                            int ppid) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        this.assocId = assocId;
        this.address = address;
        this.bytes = bytes;
        this.streamNumber = streamNumber;
        this.complete = complete;
        this.unordered = unordered;
        this.ppid = ppid;
    }

    @Override
    public Association association() {
        return association;
    }

    /**
78
     * MessageInfoImpl instances created from native will need to have their
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
     * association set from the channel.
     */
    void setAssociation(Association association) {
        this.association = association;
    }

    int associationID() {
        return assocId;
    }

    @Override
    public SocketAddress address() {
        return address;
    }

    @Override
    public int bytes() {
        return bytes;
    }

    @Override
    public int streamNumber() {
        return streamNumber;
    }

    @Override
    public MessageInfo streamNumber(int streamNumber) {
        if (streamNumber < 0 || streamNumber > 65536)
            throw new IllegalArgumentException("Invalid stream number");

        this.streamNumber = streamNumber;
        return this;
    }

    @Override
    public int payloadProtocolID() {
        return ppid;
    }

    @Override
    public MessageInfo payloadProtocolID(int ppid) {
        this.ppid = ppid;
        return this;
    }

    @Override
    public boolean isComplete() {
        return complete;
    }

    @Override
    public MessageInfo complete(boolean complete) {
        this.complete = complete;
        return this;
    }

    @Override
    public boolean isUnordered() {
        return unordered;
    }

    @Override
    public MessageInfo unordered(boolean unordered) {
        this.unordered = unordered;
        return this;
    }

    @Override
    public long timeToLive() {
        return timeToLive;
    }

    @Override
    public MessageInfo timeToLive(long millis) {
        timeToLive = millis;
        return this;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(super.toString());
        sb.append( "[Address: ").append(address)
          .append(", Association: ").append(association)
          .append(", Assoc ID: ").append(assocId)
          .append(", Bytes: ").append(bytes)
          .append(", Stream Number: ").append(streamNumber)
          .append(", Complete: ").append(complete)
          .append(", isUnordered: ").append(unordered)
          .append("]");
        return sb.toString();
    }
}