📄 movinoconnection.java
字号:
/* * Movino J2ME Client * Copyright (C) 2007 Johannes Berg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package org.movino.connection;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import javax.bluetooth.RemoteDevice;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import org.movino.Options;import org.movino.device.Camera;public abstract class MovinoConnection{ public static MovinoConnection currentConnection=null; public static String BLUETOOTH_SERVICE_CLASS = "1027392E"; public static String BLUETOOTH_SERVICE_NAME = "Movino"; public static int DEFAULT_TCP_PORT = 30710; private StreamConnection streamConnection; private DataOutputStream outStream; private DataInputStream inStream; private int bytesWritten; private int millisPassed; private int connectionState; public static final int CONNECTION_STATE_ERROR=-1; public static final int CONNECTION_STATE_CREATED=0; public static final int CONNECTION_STATE_OPENED=1; public static final int CONNECTION_STATE_HANDSHAKING=2; public static final int CONNECTION_STATE_CONNECTED=3; public static final int CONNECTION_STATE_DISCONNECTED=4; public static final int CONNECTION_ABORTED=5; private ConnectionListener connectionListener; private String errorString; private boolean isAborted; private HandShakePacket serverHandShake; private HandShakePacket clientHandShake; private PacketList packetList; public MovinoConnection(){ packetList = new PacketList(); serverHandShake=null; clientHandShake=null; streamConnection=null; connectionState = CONNECTION_STATE_CREATED; errorString = ""; isAborted=false; } public HandShakePacket getServerHandShake(){ return serverHandShake; } public void setClientHandShake(HandShakePacket p){ clientHandShake = p; } public void setListener(ConnectionListener listener){ connectionListener = listener; } public int getState(){ return connectionState; } public void connect() throws IOException{ if(connectionState!=CONNECTION_STATE_CREATED) return; System.out.println("connecting"); streamConnection = openStream(); outStream = streamConnection.openDataOutputStream(); inStream = streamConnection.openDataInputStream(); bytesWritten=0; millisPassed=0; System.out.println("connected"); connectionState = CONNECTION_STATE_OPENED; } public abstract StreamConnection openStream() throws IOException; public void doServerHandshake() throws IOException { if(connectionState!=CONNECTION_STATE_OPENED) return; connectionState = CONNECTION_STATE_HANDSHAKING; System.out.println("starting handshake"); MovinoPacket p = MovinoPacket.readPacket(inStream); if(!(p instanceof HandShakePacket)){ throw new IOException("Bad server handshake"); } System.out.println("server handshake ok"); serverHandShake = (HandShakePacket)p; connectionListener.connectionSuccess(this); } public void doClientHandshake() throws IOException{ if(connectionState!=CONNECTION_STATE_HANDSHAKING) return; if(clientHandShake==null) return; writePacket(clientHandShake); System.out.println("client handshake ok"); //send stream info Camera.SnapshotFormat sf = Options.optionSnapshotFormat; if(sf!=null){ sendPacket(new StreamInfoPacket(sf.getWidth(),sf.getHeight(),false,Options.optionAuthor,Options.optionTitle,Options.optionArchiveStream)); } connectionState = CONNECTION_STATE_CONNECTED; } public void sendPacket(MovinoPacket packet){ packetList.addPacket(packet); } public void flushPackets() throws IOException{ packetList.flushPackets(this); } public StreamConnection getConnection(){ return streamConnection; } /** * get the average speed of the link for all data sent. * @return bytes/second */ public int getSpeed(){ if(millisPassed==0) return 0; return (bytesWritten/millisPassed)/1000; } public int getNrBytesSent(){ return bytesWritten; } public synchronized void writePacket(MovinoPacket packet) throws IOException{ if(connectionState!=CONNECTION_STATE_CONNECTED && connectionState!=CONNECTION_STATE_HANDSHAKING) return; packet.writePacket(outStream); outStream.flush(); } public synchronized MovinoPacket readPacket() throws IOException{ if(connectionState!=CONNECTION_STATE_CONNECTED) return null; return MovinoPacket.readPacket(inStream); } public boolean isIndataAvailable() throws IOException{ if(inStream==null) return false; return inStream.available()>0; } public void close() throws IOException{ inStream.close(); outStream.close(); streamConnection.close(); if(connectionState==CONNECTION_STATE_CONNECTED || connectionState==CONNECTION_STATE_HANDSHAKING){ connectionState = CONNECTION_STATE_DISCONNECTED; } } public void setError(String error_string) { int error_state = connectionState; connectionState = CONNECTION_STATE_ERROR; errorString = error_string; connectionListener.connectionError(this,error_state); } public String getError(){ return errorString; } public void abort() { isAborted=true; } public boolean isAborted(){ return isAborted; } public boolean hasPendingPackets() { return packetList.hasPackets(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -