FileDocCategorySizeDatePackage
TCPOBEXNotifier.javaAPI DocphoneME MR2 API (J2ME)4025Wed May 02 18:00:30 BST 2007com.sun.midp.io.j2me.tcpobex

TCPOBEXNotifier.java

/*
 *   
 *
 * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * This program 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 at /legal/license.txt).
 * 
 * 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
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */
package com.sun.midp.io.j2me.tcpobex;

// jsr082 (impl) classes
import com.sun.kvem.jsr082.obex.ObexTransport;
import com.sun.kvem.jsr082.obex.ObexTransportNotifier;

// cldc API classes
import javax.microedition.io.Connection;
import java.io.IOException;

import com.sun.midp.security.SecurityToken;
import com.sun.midp.io.j2me.socket.Protocol;
import com.sun.midp.io.j2me.serversocket.Socket;
import javax.microedition.io.StreamConnection;

/**
 * Provides TCP OBEX stream notifier to shared obex implementation.
 */
public class TCPOBEXNotifier implements ObexTransportNotifier {
    /** TCP server socket that accepts connections to this notifier. */
    private Socket serverSocket;
    /** The sever socket port this notifier accepts connections on. */
    private int port;

    /** Indicates if this notifier is closed. */
    private boolean isClosed = false;

    /**
     * Constracts TCPOBEXNotifier instance.
     *
     * @param securityToken The security token.
     * @param port The host's port to listen on.
     * @throws IOException if any error occures.
     */
    protected TCPOBEXNotifier(SecurityToken securityToken, int port)
                                                       throws IOException {
        Protocol sp = new Protocol();

        if (port == 0) {
            serverSocket = (Socket) sp.openPrim(securityToken, "//");
        } else {
            serverSocket = (Socket) sp.openPrim(securityToken, "//:" + port);
        }
        
        this.port = serverSocket.getLocalPort();
    }

    /**
     * Accepts new tcpobex transport connection.
     *
     * @return TCPOBEXConnection.
     * @throws IOException if thrown by server socket operations or this 
     *         notifier is closed
     */
    public ObexTransport acceptAndOpen() throws IOException {
        StreamConnection conn = serverSocket.acceptAndOpen();
        Object[] param = new Object[3];
        
        param[0] = conn.openInputStream();
        param[1] = conn.openOutputStream();
        param[2] = null; // close the connection now
        conn.close();
        
        return createTransportConnection(param);
    }

    /**
     * Closes the underlaying tcp/socket notifier.
     *
     * @throws IOException if thrown by server socket methods
     */
    public synchronized void close() throws IOException {
        if (!isClosed) {
            isClosed = true;
            serverSocket.close();
        }
    }

    /**
     * Gets underlying connection.
     *
     * @return Always returns <code>null</code>.
     */
    public Connection getUnderlyingConnection() {
        return null;
    }

    /**     
     * Creates TCP OBEX transport connection.
     *
     * @param sockData An array of the streams.
     * @return TCPOBEXConnection.
     * @throws IOException
     */
    protected TCPOBEXConnection createTransportConnection(Object[] sockData)
            throws IOException {
        return new TCPOBEXConnection(sockData);
    }
}