pipeserviceprotocol.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 412 行 · 第 1/2 页
JAVA
412 行
/* * * * Copyright 1990-2008 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.pipe.serviceProtocol;import com.sun.cldc.isolate.Isolate;import com.sun.midp.links.Link;import com.sun.midp.links.LinkMessage;import com.sun.midp.security.SecurityToken;import com.sun.midp.services.SystemServiceConnection;import com.sun.midp.services.SystemServiceConnectionClosedException;import com.sun.midp.services.SystemServiceDataMessage;import com.sun.midp.services.SystemServiceLinkMessage;import com.sun.midp.services.SystemServiceManager;import com.sun.midp.services.SystemServiceMessage;import com.sun.midp.services.SystemServiceRequestor;import java.io.ByteArrayInputStream;import java.io.DataInput;import java.io.DataInputStream;import java.io.DataOutput;import java.io.IOException;import java.io.InterruptedIOException;import javax.microedition.io.ConnectionNotFoundException;/** * Implementation of low-level inter-isolate protocol to manage Pipes. * Both AMS-side and client-side code is provided by this class * to minimize chance of making protocol out-of-sync. */public class PipeServiceProtocol { static final String SERVICE_ID = "com.sun.midp.io.pipe"; static final int MAGIC_BIND_PIPE_SERVER = 0x49587001; static final int MAGIC_BIND_PIPE_CLIENT = 0x49587002; static final int MAGIC_CLOSE_PIPE_SERVER = 0x49587003; static final int MAGIC_ACCEPT_PIPE_SERVER = 0x49587004; static final int MAGIC_OK = 0x49587011; static final int MAGIC_FAIL = 0x49587012; static final int MAGIC_WOULDBLOCK = 0x49587013; private static final boolean DEBUG = false; private static long nextEndpointIdToIssue; private int debugInstanceId; private static int nextDebugInstanceId; private SecurityToken token; private String serverName; private String serverVersionRequested; private String serverVersionActual; private Link inboundLink; private Link outboundLink; private static SystemServiceConnection serviceConn; // connection to/from pipe service private static final Object serviceConnLock = new Object(); // lock which guards serviceConn private long serverInstanceId; private Link acceptLink; private final Object acceptLock = new Object(); // guards method accept() private PipeServiceProtocol(SecurityToken token) { this.token = token; debugInstanceId = nextDebugInstanceId++; } /** * On the side of user task (i.e. in MIDlet's execusion context) obtains protocol instance * connected to pipe service. Shall not be used on side of service task. * * @param token priviledged instance of SecurityToken * @return instance of PipeServiceProtocol properly initialized to be able to communicate * to pipe service */ public static synchronized PipeServiceProtocol getService(SecurityToken token) { if (serviceConn == null) { SystemServiceRequestor serviceRequestor = SystemServiceRequestor.getInstance(token); serviceConn = serviceRequestor.requestService(SERVICE_ID); if (serviceConn == null) { if (DEBUG) debugPrintS(" ERR: service not found"); throw new IllegalStateException("Pipe service not found"); } } PipeServiceProtocol service = new PipeServiceProtocol(token); return service; } /** * Registers pipe service with System Service API. To be used only in context of service task * (e.g. AMS Isolate). * * @param token priviledged instance of SecurityToken */ public static void registerService(SecurityToken token) { Dispatcher dispatcher = new Dispatcher(token); SystemServiceManager manager = SystemServiceManager.getInstance(token); manager.registerService(dispatcher); if (DEBUG) debugPrintS(" service registered"); } /** * Binds given task as pipe client and tries to establish pipe connection. * Parameters provided are used by pipe service to search for * appropriate pipe server connection. If appopriate server it's connected with data Links so * {@link #getInboundLink()} and {@link #getOutboundLink()} to be used to fetch them. * * @param serverName name of pipe server to search for * @param serverVersionRequested version of the server to search for * @throws java.io.IOException if requested server cannot be found (i.e. connection not found) or * there is some problem commnucating with pipe service. */ public void bindClient(String serverName, String serverVersionRequested) throws IOException { if (DEBUG) debugPrint(" connectClient " + serverName + ' ' + serverVersionRequested); synchronized (serviceConnLock) { try { SystemServiceDataMessage msg = SystemServiceMessage.newDataMessage(); DataOutput out = msg.getDataOutput(); out.writeInt(MAGIC_BIND_PIPE_CLIENT); out.writeUTF(serverName); out.writeUTF(serverVersionRequested); out.writeLong(Isolate.currentIsolate().uniqueId()); serviceConn.send(msg); msg = (SystemServiceDataMessage) serviceConn.receive(); DataInput in = msg.getDataInput(); int result = in.readInt(); if (result != MAGIC_OK) { throw new ConnectionNotFoundException(in.readUTF()); } serverVersionActual = in.readUTF(); this.serverName = serverName; this.serverVersionRequested = serverVersionRequested; SystemServiceLinkMessage linkMsg = (SystemServiceLinkMessage) serviceConn.receive(); inboundLink = linkMsg.getLink(); linkMsg = (SystemServiceLinkMessage) serviceConn.receive(); outboundLink = linkMsg.getLink(); } catch (SystemServiceConnectionClosedException cause) { if (DEBUG) { debugPrint(" ERR:"); cause.printStackTrace(); } throw new IOException("Cannot communicate with pipe service: " + cause); } } if (DEBUG) debugPrint(" connectClient " + serverName + ' ' + serverVersionRequested + " completed"); } /** * Binds given task as pipe server and registers server within pipe service. * * @param serverName name of the server to register * @param serverVersion version of the server to register * @throws java.io.IOException if server cannot be bound with pipe service */ public void bindServer(String serverName, String serverVersion) throws IOException { if (DEBUG) debugPrint(" connectServer " + serverName + ' ' + serverVersion); synchronized (serviceConnLock) { try { SystemServiceDataMessage msg = SystemServiceMessage.newDataMessage(); DataOutput out = msg.getDataOutput(); out.writeInt(MAGIC_BIND_PIPE_SERVER); out.writeUTF(serverName); out.writeUTF(serverVersion); out.writeLong(Isolate.currentIsolate().uniqueId()); serviceConn.send(msg); msg = (SystemServiceDataMessage) serviceConn.receive(); DataInput in = msg.getDataInput(); int result = in.readInt(); if (result != MAGIC_OK) { throw new ConnectionNotFoundException(in.readUTF()); } serverInstanceId = in.readLong(); this.serverName = serverName; this.serverVersionActual = serverVersion; } catch (SystemServiceConnectionClosedException cause) { if (DEBUG) { debugPrint(" ERR:");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?