⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mychatgroup.java

📁 利用jxta编写的聊天室,能在局域网内动态检测参与人员,通过它,可以了解JXTA运行机制.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 /*
 * MyChatGroup.java
 * 
 * @auther: Liu Jingchao
 * @version: 1.0
 * @Copyright: 2006-2008
 * @date:  2006.12.28
 *
 */

package jxtatest;
 
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException;

import net.jxta.protocol.ModuleClassAdvertisement;
import net.jxta.protocol.ModuleSpecAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.protocol.DiscoveryResponseMsg;

import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.document.Advertisement;
import net.jxta.document.TextDocument;

import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;

import net.jxta.pipe.PipeService;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.PipeMsgEvent;

import net.jxta.id.IDFactory;

import net.jxta.platform.ModuleClassID;
import net.jxta.platform.ModuleSpecID;

import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.endpoint.TextDocumentMessageElement;

import java.net.URL;
import java.util.Enumeration;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 * 
 *
 */
public class MyChatGroup implements PipeMsgListener, ActionListener{
	private PeerGroup netGP = null;
	private PeerGroup myGroup = null;
	private DiscoveryService discoverySvc = null;
	private PipeService pipeSvc = null;
	private PipeAdvertisement pipeAdv = null;
	private PipeAdvertisement pipePrivateAdv = null;
	private InputPipe inPipe = null;
	private OutputPipe outPipe = null;
	private String name = "MainChatRoom";
	private long findGroupTimeOut = 3 * 1000;
	private long findGroupTryTimes = 3;
	private long findPipeTimeOut = 3 * 1000;
	private long findPipeTryTimes = 3;
	private long createPipeTimeOut = 6 * 1000;
	private int findPipeCount = 200;
	private Vector pipes = new Vector();
	private Vector pipeId = new Vector();
	private Vector pipesPrivate = new Vector();
	private Vector list = new Vector();
	private Vector pipePrivateId = new Vector();
	
	private JButton jbSend = new JButton("发送");
	private JButton jbRefresh = new JButton("刷新");
	private JButton jbPrivateChat = new JButton("私聊");
	private JTextArea jtaHistoryContent = new JTextArea();
	private JTextArea jtaPrivateContent = new JTextArea();
	private JTextField jtfContent = new JTextField();
	private JTable jlTable = null;
	
	private PipeMsgListener privateMsgListener = null;
	private String advPrivateFileName = "privatepipe.adv";
	
	static String groupURL =
	              "jxta:uuid-04B8173E42A445F0AF6BBB486CEA53D102";
	
	public MyChatGroup(){
		jlTable = new JTable(new DefaultTableModel(){
			                 public int getRowCount(){
			                 	return list.size();
			                 }
			                 
			                 public int getColumnCount(){
			                 	return 1;
			                 }
			                 
			                 public Object getValueAt(int row, int col){
			                 	return list.get(row);
			                 }
			                 
			                 public boolean isCellEditable(int row, int col){
			                 	return false;
			                 }
			                 
			                 public String getColumnName(int col){
			                 	return "在线列表";
			                 }
			               });
		jbSend.addActionListener(this);
		jbRefresh.addActionListener(this);
		jbPrivateChat.addActionListener(this);
		jtaHistoryContent.setEditable(false);
		jtaHistoryContent.setLineWrap(true);
		jtaPrivateContent.setEditable(false);
		privateMsgListener = new PipeMsgListener(){
			 public void pipeMsgEvent(PipeMsgEvent pme){
	    	        privateMessage(pme);
	    	 }
		};
		
	}
	
	public static void main(String[] args) throws Exception{
		MyChatGroup tj = new MyChatGroup();
		tj.startJxta();
		tj.showFrame();
	}
	
	private void startJxta(){
		try{
			netGP = PeerGroupFactory.newNetPeerGroup();
		}catch(Exception e){
			System.out.println(e.getMessage());
			System.exit(0);
		}
		
		try{
			joinMyGroup();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Can't join or create myGroup.");
			System.exit(0);
		}
	}
	
	/** 寻找本地组 */
	private boolean findLocalGroup(DiscoveryService dstmp){
		Enumeration en = null;
		try{
            en = dstmp.getLocalAdvertisements(DiscoveryService.GROUP, "Name", "MyChatGroup");
        }catch(IOException ioe){
        	ioe.printStackTrace();
        	return false;
        }
        if(en==null) return false;
        if(!en.hasMoreElements()) return false;
        
        try{
        	myGroup = netGP.newGroup((PeerGroupAdvertisement)en.nextElement());	
        }catch(PeerGroupException pge){
        	pge.printStackTrace();
        	return false;
        }
		return true;
	}
	
	private void showFrame(){
		JFrame jf = new JFrame();
		JPanel jpBottom = new JPanel(new BorderLayout());
		JPanel jpRight = new JPanel(new BorderLayout());
		JPanel jpCenter = new JPanel(new BorderLayout());
		JScrollPane jsp = new JScrollPane(jtaPrivateContent);
		
		jpCenter.setBounds(10, 10, 400, 300);
		jpRight.setBounds(420, 10, 150, 340);
		jpBottom.setBounds(10, 320, 400, 30);
		jsp.setBounds(10, 350, 560, 200);
		jsp.setBorder(BorderFactory.createTitledBorder("私聊记录"));
		
		jpCenter.add(new JScrollPane(jtaHistoryContent, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
		jpBottom.add(jtfContent, BorderLayout.CENTER);
		jpBottom.add(jbSend, BorderLayout.EAST);
		JPanel jpRightBottom = new JPanel(new BorderLayout());
		     // jpRightBottom.add(jbRefresh);
		     jpRightBottom.add(jbPrivateChat);
		jpRight.add(jpRightBottom, BorderLayout.SOUTH);
		jpRight.add(new JScrollPane(jlTable), BorderLayout.CENTER);
		jf.getContentPane().setLayout(null);
		jf.getContentPane().add(jpCenter);
		jf.getContentPane().add(jpBottom);
		jf.getContentPane().add(jpRight);
		jf.getContentPane().add(jsp);
		jf.setSize(580,610);
		jf.setTitle("聊天室 - " + myGroup.getPeerName());
		jf.setLocation(200,200);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.getRootPane().setDefaultButton(jbSend);
		jtfContent.requestFocus(true);
		jf.setVisible(true);
	}
	
		/** 寻找本地通告 */
	private boolean findLocalPipe(){
		Enumeration en = null;
		try{
            en = discoverySvc.getLocalAdvertisements(DiscoveryService.ADV, "Name", "MyChatGroup:PipeAdv:"+name);
        }catch(IOException ioe){
        	ioe.printStackTrace();
        	return false;
        }
        if(en==null) return false;
        if(!en.hasMoreElements()) return false;
        
       	System.out.println("Find a exist pipeadvertisement.");
		pipeAdv = (PipeAdvertisement)en.nextElement();
        
		return true;
	}
	
	/** 加入组,没有找到则建立同时发布通首公告 */
	private void joinMyGroup() throws Exception{
        // find exist group
		DiscoveryService dstmp = netGP.getDiscoveryService();
		
		long tryGroupCount = 1;
		while(!findLocalGroup(dstmp) && tryGroupCount++<=findGroupTryTimes){
			dstmp.getRemoteAdvertisements(null, DiscoveryService.GROUP, "Name", "MyChatGroup", 1);
		    Thread.sleep(findGroupTimeOut);
		}
		
		if(myGroup==null){
		    ModuleImplAdvertisement mia = netGP.getAllPurposePeerGroupImplAdvertisement();
		    myGroup = netGP.newGroup((PeerGroupID)IDFactory.fromURL(new URL("urn", "", groupURL)), mia, "MyChatGroup", "A test chat jxta group.");
	    }
	    
		discoverySvc = myGroup.getDiscoveryService();
		pipeSvc = myGroup.getPipeService();
		
		long tryPipeCount = 1;
		while(!findLocalPipe() && tryPipeCount++<=findPipeTryTimes){
			discoverySvc.getRemoteAdvertisements(null, DiscoveryService.ADV, 
			                                     "Name", "MyChatGroup:PipeAdv:"+name,
			                                     1
			                                    );
			Thread.sleep(findPipeTimeOut);
		}
		
		if(pipeAdv==null){
			System.out.println("Create a new pipeadvertisement.");
			pipeAdv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(
				PipeAdvertisement.getAdvertisementType());
			pipeAdv.setName("MyChatGroup:PipeAdv:"+name);
			pipeAdv.setPipeID(IDFactory.newPipeID(myGroup.getPeerGroupID()));
			pipeAdv.setType(PipeService.PropagateType);
			
			discoverySvc.publish(pipeAdv, PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION);
			discoverySvc.remotePublish(pipeAdv, DiscoveryService.ADV);
		}
		
		// 私自的通道
		try{
			FileInputStream fis = new FileInputStream(advPrivateFileName);
			pipePrivateAdv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(
				                            new MimeMediaType("text/xml"), fis);
		}catch(IOException fe){
			FileOutputStream fos = new FileOutputStream(advPrivateFileName);
			pipePrivateAdv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(
				PipeAdvertisement.getAdvertisementType());
			pipePrivateAdv.setName("MyChatGroup:PipePrivateAdv:"+name);
		    pipePrivateAdv.setPipeID(IDFactory.newPipeID(myGroup.getPeerGroupID()));
		    pipePrivateAdv.setType(PipeService.UnicastType);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -