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 */
25package sun.nio.ch.sctp;
26
27import java.net.SocketAddress;
28import com.sun.nio.sctp.MessageInfo;
29import com.sun.nio.sctp.Association;
30
31/**
32 * An implementation of a MessageInfo.
33 */
34public class MessageInfoImpl extends MessageInfo {
35    private final SocketAddress address;
36    private final int bytes;          /* 0 */
37
38    private Association association;
39    private int assocId;
40    private int streamNumber;
41    private boolean complete = true;
42    private boolean unordered;  /* false */
43    private long timeToLive;    /* 0L */
44    private int ppid;           /* 0 */
45
46    public MessageInfoImpl(Association association,
47                           SocketAddress address,
48                           int streamNumber) {
49        this.association = association;
50        this.address = address;
51        this.streamNumber = streamNumber;
52        bytes = 0;
53    }
54
55    /* Invoked from native */
56    private MessageInfoImpl(int assocId,
57                            SocketAddress address,
58                            int bytes,
59                            int streamNumber,
60                            boolean complete,
61                            boolean unordered,
62                            int ppid) {
63        this.assocId = assocId;
64        this.address = address;
65        this.bytes = bytes;
66        this.streamNumber = streamNumber;
67        this.complete = complete;
68        this.unordered = unordered;
69        this.ppid = ppid;
70    }
71
72    @Override
73    public Association association() {
74        return association;
75    }
76
77    /**
78     * MessageInfoImpl instances created from native will need to have their
79     * association set from the channel.
80     */
81    void setAssociation(Association association) {
82        this.association = association;
83    }
84
85    int associationID() {
86        return assocId;
87    }
88
89    @Override
90    public SocketAddress address() {
91        return address;
92    }
93
94    @Override
95    public int bytes() {
96        return bytes;
97    }
98
99    @Override
100    public int streamNumber() {
101        return streamNumber;
102    }
103
104    @Override
105    public MessageInfo streamNumber(int streamNumber) {
106        if (streamNumber < 0 || streamNumber > 65536)
107            throw new IllegalArgumentException("Invalid stream number");
108
109        this.streamNumber = streamNumber;
110        return this;
111    }
112
113    @Override
114    public int payloadProtocolID() {
115        return ppid;
116    }
117
118    @Override
119    public MessageInfo payloadProtocolID(int ppid) {
120        this.ppid = ppid;
121        return this;
122    }
123
124    @Override
125    public boolean isComplete() {
126        return complete;
127    }
128
129    @Override
130    public MessageInfo complete(boolean complete) {
131        this.complete = complete;
132        return this;
133    }
134
135    @Override
136    public boolean isUnordered() {
137        return unordered;
138    }
139
140    @Override
141    public MessageInfo unordered(boolean unordered) {
142        this.unordered = unordered;
143        return this;
144    }
145
146    @Override
147    public long timeToLive() {
148        return timeToLive;
149    }
150
151    @Override
152    public MessageInfo timeToLive(long millis) {
153        timeToLive = millis;
154        return this;
155    }
156
157    @Override
158    public String toString() {
159        StringBuilder sb = new StringBuilder(super.toString());
160        sb.append( "[Address: ").append(address)
161          .append(", Association: ").append(association)
162          .append(", Assoc ID: ").append(assocId)
163          .append(", Bytes: ").append(bytes)
164          .append(", Stream Number: ").append(streamNumber)
165          .append(", Complete: ").append(complete)
166          .append(", isUnordered: ").append(unordered)
167          .append("]");
168        return sb.toString();
169    }
170}
171