📄 icq.java.svn-base
字号:
/******************************************************************************* Jimm - Mobile Messaging - J2ME ICQ clone Copyright (C) 2003-05 Jimm Project 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ******************************************************************************** File: src/jimm/comm/Icq.java Version: ###VERSION### Date: ###DATE### Author: Manuel Linsmayer, Andreas Rossbacher *******************************************************************************/package jimm.comm;import java.io.ByteArrayOutputStream;import java.util.Hashtable;import java.util.Vector;import javax.microedition.io.Connector;import javax.microedition.io.ContentConnection;import jimm.ContactItem;import jimm.DebugLog;import jimm.Jimm;import jimm.JimmUI;import jimm.JimmException;import jimm.Options;import jimm.SplashCanvas;import jimm.StatusInfo;import jimm.comm.connections.*;import jimm.util.ResourceBundle;import jimm.ContactList;import jimm.MainThread;import jimm.TimerTasks;//#sijapp cond.if modules_TRAFFIC is "true" #import jimm.Traffic;//#sijapp cond.end#public class Icq implements Runnable{ private static Icq _this; public static final byte[] MTN_PACKET_BEGIN = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 }; // Current state static private boolean connected = false; static private boolean disconnected = false; // Requested actions static private Vector reqAction = new Vector(); // Thread static volatile Thread thread; // Current visibility mode static private int currentVisibility; static String lastStatusChangeTime; // Wait object static private Object wait = new Object(); // Connection to peer// #sijapp cond.if (target!="DEFAULT")&(modules_FILES="true")# static PeerConnection peerC;// #sijapp cond.end# // All currently active actions static private Vector actAction; // Action listener static private ActionListener actListener; // Keep alive timer task static private TimerTasks keepAliveTimerTask; public static int reconnect_attempts; public Icq() { _this = this; } static { initClientIndData(); } // Flag for indicate that connection was reset _by_user_ public static boolean isDisconnected() { synchronized (_this) { return disconnected; } } public static void setDisconnected(boolean value) { synchronized (_this) { disconnected = value; } } // Request an action static public void requestAction(Action act) throws JimmException { // Set reference to this ICQ object for callbacks act.setIcq(_this); // Look whether action is executable at the moment if (!act.isExecutable()) { throw (new JimmException(140, 0)); } // Queue requested action synchronized (_this) { reqAction.addElement(act); } // Connect? if ((act instanceof ConnectAction) || (act instanceof RegisterNewUinAction)) { // Create new thread and start thread = new Thread(_this); thread.start(); } // Notify main loop synchronized (wait) { wait.notify(); } } // Connects to the ICQ network static public synchronized void connect() { setDisconnected(false); //#sijapp cond.if target isnot "MOTOROLA"# if (Options.getBoolean(Options.OPTION_SHADOW_CON)) { // Make the shadow connection for Nokia 6230 of other devices if // needed ContentConnection ctemp = null; try { String url = "http://shadow.jimm.org/"; ctemp = (ContentConnection) Connector.open(url); ctemp.openDataInputStream(); } catch (Exception e) { // Do nothing } } //#sijapp cond.end# // Connect ConnectAction act = new ConnectAction( Options.getString(Options.OPTION_UIN), Options.getString(Options.OPTION_PASSWORD), getFirstServerAddr(), Options.getString(Options.OPTION_SRV_PORT)); try { requestAction(act); } catch (JimmException e) { JimmException.handleException(e); } StatusInfo statInfo = JimmUI.findStatus(StatusInfo.TYPE_STATUS, (int)Options.getLong(Options.OPTION_ONLINE_STATUS)); SplashCanvas.setStatusToDraw(statInfo != null ? statInfo.getImage() : null); // Start timer SplashCanvas.addTimerTask("connecting", act, true); lastStatusChangeTime = Util.getDateString(true); } // Connects to the ICQ network for register new uin static public synchronized void connectForNewUIN(String newPassword) { setDisconnected(true); // to prevent reconnect on error // Connect RegisterNewUinAction act = new RegisterNewUinAction( newPassword, getFirstServerAddr(), Options.getString(Options.OPTION_SRV_PORT)); try { requestAction(act); } catch (JimmException e) { JimmException.handleException(e); } RegisterNewUinAction.addTimerTask (act); lastStatusChangeTime = Util.getDateString(true); } /* Disconnects from the ICQ network */ static public synchronized void disconnect(boolean force) { //#sijapp cond.if target!="DEFAULT" & modules_AVATARS="true"#
disconnectBart(force); //#sijapp cond.end#//#sijapp cond.if (target!="DEFAULT")&(modules_FILES="true")# resetPeerCon();//#sijapp cond.end# if (c == null) return; setDisconnected(true); thread = null; synchronized (wait) { wait.notifyAll(); } if (force) c.forceDisconnect(); else c.notifyToDisconnect(); //#sijapp cond.if modules_TRAFFIC is "true" # try { Traffic.save(); } catch (Exception e) { /* Do nothing */ } //#sijapp cond.end# /* Reset all contacts offine */ MainThread.resetContactsOffline(); setNotConnected(); if (c.haveToSetNullAfterDisconnect()) c = null; } /* Disconnects from the ICQ network */ //#sijapp cond.if target!="DEFAULT" & modules_AVATARS="true"#
static public synchronized void disconnectBart(boolean force) { if (bartC == null) return; if (force) bartC.forceDisconnect(); else bartC.notifyToDisconnect(); if (bartC.haveToSetNullAfterDisconnect()) bartC = null; } //#sijapp cond.end# static public void setVisibility(int value) { currentVisibility = value; } static public int getVisibility() { return currentVisibility; } // Checks whether the comm. subsystem is in STATE_NOT_CONNECTED static public synchronized boolean isNotConnected() { return !connected; } // Puts the comm. subsystem into STATE_NOT_CONNECTED static public synchronized void setNotConnected() { connected = false; } // Checks whether the comm. subsystem is in STATE_CONNECTED static public synchronized boolean isConnected() { return connected; } // Puts the comm. subsystem into STATE_CONNECTED static protected synchronized void setConnected() { SplashCanvas.setLastErrCode(null); Icq.reconnect_attempts = (Options.getBoolean(Options.OPTION_RECONNECT)) ? Options.getInt(Options.OPTION_RECONNECT_NUMBER) : 0; connected = true; } // Resets the comm. subsystem static public synchronized void resetServerCon() { // Wake up thread in order to complete synchronized (Icq.wait) { Icq.wait.notify(); } // Reset all variables connected = false; // Reset all timer tasks Jimm.jimm.cancelTimer(); // Delete all actions if (actAction != null) { actAction.removeAllElements(); } if (reqAction != null) { reqAction.removeAllElements(); } }//#sijapp cond.if (target!="DEFAULT")&(modules_FILES="true")# // Resets the comm. subsystem static public synchronized void resetPeerCon() { // Close connection peerC = null; }//#sijapp cond.end# static public Object getWaitObj() { return wait; } // Connection to the ICQ server static Connection c; //#sijapp cond.if target!="DEFAULT" & modules_AVATARS="true"#
static Connection bartC; // #sijapp cond.end# public static boolean isMyConnection(Connection conn) { return (conn == c); } public static void sendPacket(Packet packet) throws JimmException { if (c == null) return; // TODO: may be better to throw exception? c.sendPacket(packet); } public static void connect(String data) throws JimmException { if (c == null) return; // TODO: may be better to throw exception? c.connect(data); } // Main loop public void run() {// #sijapp cond.if (target!="DEFAULT")&(modules_FILES="true")# // Is a DC packet Available boolean dcPacketAvailable;// #sijapp cond.end# //#sijapp cond.if target!="DEFAULT" & modules_AVATARS="true"#
boolean biPacketAvailable; // #sijapp cond.end# // Get thread object Thread thread = Thread.currentThread(); // Required variables Action newAction = null; // Instantiate connections
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -