?? chatservice.java
字號:
package connex.plugins.chat;
import connex.session.plugin.Plugin;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import connex.core.net.ConnectionFactory;
import connex.core.net.ConnectionClient;
import net.jxta.endpoint.Message;
import connex.core.net.WorkspaceConnection;
import net.jxta.endpoint.StringMessageElement;
import connex.core.Presence.PresenceService;
import net.jxta.endpoint.MessageElement;
import java.io.IOException;
import org.apache.log4j.Level;
import connex.core.net.WorkspaceConnection;
import connex.core.WS.Workspace;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ChatService
implements Plugin, ConnectionClient {
private final static Logger LOG = org.apache.log4j.Logger.getLogger(
ChatService.class.getName());
private static ChatService instance = null;
private ChatBoard gui;
private boolean started = false;
private WorkspaceConnection sess;
private Workspace ws;
public static int maxRetry = 3;
private int retry = 1;
public ChatService() {
instance = this;
}
public void initPlugin(Object param) {
ws= (Workspace)param;
sess = (WorkspaceConnection) ConnectionFactory.newWorkspaceConnection(ws,this);
LOG.setLevel(Level.INFO);
gui = new ChatBoard();
}
public void startPlugin() {
LOG.info("starting the Chat Service");
if (!started) {
try {
connect();
LOG.info(" Chat Service started");
}
catch (IOException ex) {
}
}
else {
if (LOG.isEnabledFor(Level.INFO)) {
LOG.info(" ChateService already started");
}
}
}
private void connect() throws IOException {
if (sess.connect()) {
started = true;
if (LOG.isEnabledFor(Level.INFO)) {
LOG.info(" ChateService started successfully");
}
}
else {
if (LOG.isEnabledFor(Level.INFO)) {
LOG.info(" ChatService can't connect.. ");
}
if (retry <= maxRetry) {
LOG.info(" retry to connect ChatService;" + retry + " .. ");
sess.disConnect();
try {
Thread.currentThread().sleep(2000);
}
catch (InterruptedException ex) {
}
connect();
retry++;
}
else {
throw new IOException("ChatService can't connect...");
}
}
}
public JPanel getMainUI() {
return gui;
}
public void stopPlugin() {
if(started){
sess.disConnect();
gui = null;
started = false;
if (LOG.isEnabledFor(Level.INFO)) {
LOG.info(" ChatService stoped ");
}
}
}
public void createSession(String id) {
}
public String getClientName() {
return "ChatService";
}
public void sendMessage(String txt) {
sess.send(ChatProtocol.createChatMessage(txt,ws.getPeerID().toString(),ws.getPeerAdv().getName()));
}
public void reciveMessage(Message msg) {
process(msg);
}
public void closedFromRemote() {
}
public static ChatService getInstance() {
if (instance == null) {
instance = new ChatService();
}
return instance;
}
private void process(Message msg) {
String version = null;
String membId = null;
String membName = null;
String type = null;
String data = null;
/* Message header*/
MessageElement el = msg.getMessageElement(ChatProtocol.nameSpace,
ChatProtocol.version);
if (el != null) {
version = el.toString();
}
el = msg.getMessageElement(ChatProtocol.nameSpace,
ChatProtocol.membIDTag);
if (el != null) {
membId = el.toString();
if (membId.equals(PresenceService.getInstance().
getmOwnPeerAdv().getPeerID().
toString())) {
return;
}
}
el = msg.getMessageElement(ChatProtocol.nameSpace,
ChatProtocol.membNameTag);
if (el != null) {
membName = el.toString();
}
el = msg.getMessageElement(ChatProtocol.nameSpace,
ChatProtocol.typeTag);
if (el != null) {
type = el.toString();
/* if (LOG.isEnabledFor(Level.INFO)) {
LOG.info("Recived Message Type = " + type);
}*/
/*** Messagetype*****************/
if (type.equals(ChatProtocol.chatMessage)) {
el = msg.getMessageElement(ChatProtocol.nameSpace,
ChatProtocol.messageTag);
if (el != null) {
data = el.toString();
}
gui.showMsg(membName, data);
return;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -