📄 bsconnectionbean.java
字号:
package edu.ou.kmi.buddyspace.core;
/*
* BSConnectionBean.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 15 July 2002, 17:40
*/
import java.util.*;
import javax.net.ssl.*;
import org.jabber.jabberbeans.*;
import edu.ou.kmi.buddyspace.xml.*;
/**
* <code>BSConnectionBean</code> is the main bean for BuddySpaceBeans.
* A <code>BSConnectionBean</code> mantains connection to a jabber server.
* All the other beans are dependent on this and has to be reset
* (typically using bean.<code>setConnection</code> function) after a new connection
* is setablished (<code>BSConnectionBean.connect</code> function).
* The class is based on <code>ConnectionBean</code> and implements
* <code>ConnectionListener</code> from <code>JabberBeans</code>.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSConnectionBean implements ConnectionListener, PacketListener {
private ConnectionBean connection;
private ConnectionEvent.EState connState;
private final String name = "Connection";
private Vector connectionListeners;
//private boolean aboutDisconnect = false;
/**
* Constructor
*/
BSConnectionBean() {
System.out.println("BSConnectionBean constructor");
connection = null;
connState = ConnectionEvent.STATE_DISCONNECTED;
connectionListeners = new Vector();
}
/** Returns current connection state */
public ConnectionEvent.EState getConnectionState() {
return connState;
}
/**
* Creates new connection to the specified <code>hostName</host>
*
*@return <code>false</code> if the connection cannot be established
*/
public synchronized boolean connect(String hostName, int port, boolean useSSL) {
System.out.println("BSConnectionBean connect(" + hostName + ", " + port + ")");
if (connState != ConnectionEvent.STATE_DISCONNECTED) {
System.out.println("BSConnectionBean already connected/ing - use disconnect first");
return false;
}
connection = useSSL? new ConnectionBeanSSL() : new ConnectionBean();
connection.addConnectionListener(this);
connection.addPacketListener(this);
try {
java.net.InetAddress host;
host = java.net.InetAddress.getByName(hostName);
connection.connect(host, port);
}
catch (java.net.UnknownHostException e) {
BSCore.logEvent(name, "error: unknown host");
notifyConnectionListeners(new ConnectionEvent(this,
ConnectionEvent.STATE_DISCONNECTED,
connState,
ConnectionEvent.REASON_IO_ERROR));
disconnect();
return false;
}
catch (java.io.IOException e) {
BSCore.logEvent(name, "error: IO error while connecting");
if (e instanceof SSLException) System.out.println(e);
notifyConnectionListeners(new ConnectionEvent(this,
ConnectionEvent.STATE_DISCONNECTED,
connState,
ConnectionEvent.REASON_IO_ERROR));
disconnect();
return false;
}
return true;
}
/**
* Creates new connection to the specified <code>hostName</host>
*
*@return <code>false</code> if the connection cannot be established
*/
public boolean connect(String hostName, int port) {
return connect(hostName, port, false);
}
/**
* Creates new connection to the specified <code>hostName</host>
*
*@return <code>false</code> if the connection cannot be established
*/
public boolean connect(String hostName) {
return connect(hostName, ConnectionBean.DEFAULT_PORT);
}
/**
* Creates new connection to the specified <code>hostName</host>
*
*@return <code>false</code> if the connection cannot be established
*/
public boolean connect(String hostName, boolean useSSL) {
return connect(hostName, useSSL? ConnectionBeanSSL.DEFAULT_SSL_PORT :
ConnectionBean.DEFAULT_PORT,
useSSL);
}
/**
* Closes current connection
*/
public synchronized void disconnect() {
System.out.println("BSConnectionBean disconnect()");
if (connection != null) {
ConnectionEvent.EState state = connection.getConnectionState();
if (state == ConnectionEvent.STATE_CONNECTED ||
state == ConnectionEvent.STATE_CONNECTING) {
//XMLStreamFooter f = new XMLStreamFooter();
//connection.send(f);
connection.disconnect();
}
else {
System.out.println("BSConnectionBean already disconnected");
setConnectionState(state);
BSCore.logEvent(name, "disconnected");
}
}
else
System.out.println("BSConnectionBean already disconnected");
}
/**
* Sets that connection state.
*/
protected synchronized void setConnectionState(ConnectionEvent.EState state) {
connState = state;
if (state == ConnectionEvent.STATE_DISCONNECTED) {
if (connection != null) {
connection.delConnectionListener(this);
connection.delPacketListener(this);
}
connection = null;
}
}
/**
* Frees all object bindings to allow object destroy
*/
protected void prepareToDestroy() {
disconnect();
removeAllConnectionListeners();
}
/**
* Handles changes of connection state.
* See ConnectionListener in JabberBeans.
*/
public void connectionChanged(ConnectionEvent ce) {
System.out.println("BSConnectionBean connectionChanged(" + ce.getState() + ", " + ce.getReason() + ")");
ConnectionEvent.EState state = ce.getState();
if (state == ConnectionEvent.STATE_CONNECTED)
BSCore.logEvent(name, "connected");
else if (state == ConnectionEvent.STATE_CONNECTING)
BSCore.logEvent(name, "connecting");
else if (state == ConnectionEvent.STATE_DISCONNECTED)
BSCore.logEvent(name, "disconnected");
else
BSCore.logEvent(name, "unknown");
setConnectionState(state);
notifyConnectionListeners(new ConnectionEvent(this, state,
ce.getOldState(), ce.getReason()));
}
// *** connection listeners ***
/**
* Adds <code>ConnectionListener</code> to listeners notified when
* connection state changes.
*
* @see #removeConnectionListener
* @see #removeAllConnectionListeners
* @see #notifyConnectionListeners
*/
public void addConnectionListener(ConnectionListener listener) {
//assert listener != null : listener;
if (listener == null) return;
if (!connectionListeners.contains(listener)) {
connectionListeners.addElement(listener);
}
}
/**
* Removes <code>ConnectionListener</code> from listeners notified when
* connection state changes.
*
* @see #addConnectionListener
* @see #removeAllConnectionListeners
* @see #notifyConnectionListeners
*/
public void removeConnectionListener(ConnectionListener listener) {
//assert listener != null : listener;
if (listener == null) return;
connectionListeners.removeElement(listener);
}
/**
* Removes all listeners notified when connection state changes.
* This can be used before to free dependencies and allow dispose of
* all objects.
*
* @see #addConnectionListener
* @see #removeConnectionListener
* @see #notifyConnectionListeners
*/
public void removeAllConnectionListeners() {
connectionListeners.clear();
}
/**
* Notifies connection listeners about connection state change.
*
* @see #addConnectionListener
* @see #removeConnectionListener
* @see #removeAllConnectionListeners
*/
private void notifyConnectionListeners(ConnectionEvent ce) {
for (Enumeration e = connectionListeners.elements(); e.hasMoreElements(); ) {
ConnectionListener listener = (ConnectionListener) e.nextElement();
listener.connectionChanged(ce);
}
}
// *** other ***
/**
* Returns current connection state.
*/
public ConnectionEvent.EState getState() {
return connState;
}
/**
* Returns current <code>ConnectionBean</code>.
* Which is typically used for initialization of other beans.
*/
public ConnectionBean getConnection() {
return connection;
}
/** PacketListener method */
public void receivedPacket(PacketEvent pe) {
//BSCore.logReceived(name, pe.getPacket().toString());
}
/** PacketListener method */
public void sendFailed(PacketEvent pe) {
System.out.println("BSConnectionBean sendFailed(...)");
setConnectionState(ConnectionEvent.STATE_DISCONNECTED);
notifyConnectionListeners(new ConnectionEvent(this, connState,
ConnectionEvent.STATE_CONNECTED,
ConnectionEvent.REASON_UNKNOWN));
BSCore.logEvent(name, "disconnected");
}
/** PacketListener method */
public void sentPacket(PacketEvent pe) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -