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.services;
028:
029: import com.sun.midp.links.*;
030: import java.io.*;
031: import java.lang.*;
032:
033: final class SystemServiceConnectionImpl implements
034: SystemServiceConnection {
035:
036: private SystemServiceConnectionLinks connectionLinks = null;
037: private SystemServiceConnectionListener listener = null;
038:
039: class ListenerThread extends Thread {
040: public void run() {
041: try {
042: while (true) {
043: SystemServiceMessage msg = receive();
044: listener.onMessage(msg);
045: }
046: } catch (SystemServiceConnectionClosedException e) {
047: listener.onConnectionClosed();
048: }
049: }
050: }
051:
052: SystemServiceConnectionImpl(
053: SystemServiceConnectionLinks connectionLinks) {
054: this .connectionLinks = connectionLinks;
055: }
056:
057: public SystemServiceMessage receive()
058: throws SystemServiceConnectionClosedException {
059:
060: try {
061: Link receiveLink = connectionLinks.getReceiveLink();
062: LinkMessage msg = receiveLink.receive();
063: byte[] data = msg.extractData();
064:
065: return new SystemServiceReadMessage(data);
066: } catch (ClosedLinkException e) {
067: throw new SystemServiceConnectionClosedException();
068: } catch (InterruptedIOException e) {
069: connectionLinks.close();
070: throw new SystemServiceConnectionClosedException();
071: } catch (IOException e) {
072: connectionLinks.close();
073: throw new SystemServiceConnectionClosedException();
074: }
075: }
076:
077: public void send(SystemServiceMessage msg)
078: throws SystemServiceConnectionClosedException {
079:
080: try {
081: SystemServiceWriteMessage writeMsg = (SystemServiceWriteMessage) msg;
082: byte[] data = writeMsg.getData();
083:
084: LinkMessage linkMsg = LinkMessage.newDataMessage(data);
085: Link sendLink = connectionLinks.getSendLink();
086: sendLink.send(linkMsg);
087: } catch (ClosedLinkException e) {
088: throw new SystemServiceConnectionClosedException();
089: } catch (InterruptedIOException e) {
090: connectionLinks.close();
091: throw new SystemServiceConnectionClosedException();
092: } catch (IOException e) {
093: connectionLinks.close();
094: throw new SystemServiceConnectionClosedException();
095: }
096: }
097:
098: public void setConnectionListener(SystemServiceConnectionListener l) {
099: listener = l;
100: new ListenerThread().start();
101: }
102: }
|