001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.links;
028:
029: import com.sun.cldc.isolate.Isolate;
030: import java.io.IOException;
031: import java.io.InterruptedIOException;
032:
033: public class Link {
034:
035: private Link emptyLinkCache; // = null
036:
037: private int nativePointer; // set and get only by native code
038:
039: public static Link newLink(Isolate sender, Isolate receiver) {
040: int rid = receiver.id(); // throws NullPointerException
041: int sid = sender.id(); // throws NullPointerException
042:
043: if (rid == -1 || sid == -1 || receiver.isTerminated()
044: || sender.isTerminated()) {
045: throw new IllegalStateException();
046: }
047:
048: /*
049: * IMPL_NOTE - this has race conditions. One of the isolates can
050: * change state between the test and the call to init0().
051: */
052:
053: Link link = new Link();
054: link.init0(sender.id(), receiver.id());
055: return link;
056: }
057:
058: public native void close();
059:
060: public boolean equals(Object obj) {
061: return obj instanceof Link
062: && ((Link) obj).nativePointer == this .nativePointer;
063: }
064:
065: public int hashCode() {
066: return nativePointer;
067: }
068:
069: public native boolean isOpen();
070:
071: /**
072: * Throws IllegalArgumentException if the calling thread is not in the
073: * receiving isolate for this link.
074: *
075: * (Note: this differs from the JSR-121 specification, which states that
076: * UnsupportedOperationException should be thrown in this case. That
077: * exception doesn't appear in CLDC, so IllegalArgumentException is thrown
078: * instead.)
079: */
080: public LinkMessage receive() throws ClosedLinkException,
081: InterruptedIOException, IOException {
082: Link emptyLink;
083: LinkMessage msg = new LinkMessage();
084:
085: synchronized (this ) {
086: if (emptyLinkCache == null) {
087: emptyLink = new Link();
088: } else {
089: emptyLink = emptyLinkCache;
090: emptyLinkCache = null;
091: }
092: }
093:
094: receive0(msg, emptyLink);
095:
096: if (!msg.containsLink()) {
097: synchronized (this ) {
098: if (emptyLinkCache == null) {
099: emptyLinkCache = emptyLink;
100: }
101: }
102: }
103:
104: return msg;
105: }
106:
107: /**
108: * Throws IllegalArgumentException if the calling thread is not in the
109: * sending isolate for this link.
110: *
111: * (Note: this differs from the JSR-121 specification, which states that
112: * UnsupportedOperationException should be thrown in this case. That
113: * exception doesn't appear in CLDC, so IllegalArgumentException is thrown
114: * instead.)
115: */
116: public void send(LinkMessage lm) throws ClosedLinkException,
117: InterruptedIOException, IOException {
118: if (lm == null) {
119: throw new NullPointerException();
120: }
121: send0(lm);
122: }
123:
124: /**
125: * Creates a new, empty link. This link must be filled in by native code
126: * before it can be used.
127: */
128: Link() {
129: }
130:
131: private native void finalize();
132:
133: private native void init0(int sender, int receiver);
134:
135: private native void receive0(LinkMessage msg, Link link)
136: throws ClosedLinkException, InterruptedIOException,
137: IOException;
138:
139: private native void send0(LinkMessage msg)
140: throws ClosedLinkException, InterruptedIOException,
141: IOException;
142: }
|