📄 chat.java
字号:
package Chat;
public class Chat
implements
ChatInterface,
Runnable,
net.jxta.discovery.DiscoveryListener
{
private class HandledGroup
{
public String GroupName = "";
public String UserName = null;
public ChatMessageListener Listener = null;
public int Counter = 0;
public boolean creating = false;
HandledGroup(String GroupName,
String UserName,
ChatMessageListener Listener,
boolean creating)
{
this.GroupName = GroupName;
this.UserName = UserName;
this.Listener = Listener;
this.creating = creating;
}
}
private class HandledPipe
{
public String GroupName = "";
public int Counter = 0;
HandledPipe(String GroupName)
{
this.GroupName = GroupName;
}
}
private class Joined implements net.jxta.pipe.PipeMsgListener
{
public net.jxta.peergroup.PeerGroup netPeerGroup = null;
public net.jxta.protocol.PeerGroupAdvertisement netAdvertisement = null;
public net.jxta.pipe.InputPipe netInPipe = null;
public net.jxta.pipe.OutputPipe netOutPipe = null;
public ChatMessageListener Listener = null;
public String UserName = "";
public java.util.Hashtable users = new java.util.Hashtable();
Joined(net.jxta.peergroup.PeerGroup netPeerGroup,
String UserName,
ChatMessageListener Listener,
net.jxta.protocol.PeerGroupAdvertisement netAdvertisement)
{
this.netPeerGroup = netPeerGroup;
this.UserName = UserName;
this.Listener = Listener;
this.netAdvertisement = netAdvertisement;
}
public void pipeMsgEvent(net.jxta.pipe.PipeMsgEvent event) {
net.jxta.endpoint.Message msg = null;
try {
msg = event.getMessage();
if(msg == null) return;
}
catch(Exception e) {
e.printStackTrace();
return;
}
net.jxta.endpoint.MessageElement el;
try {
el = msg.getMessageElement(null, "whoareyou");
el.toString();
iAm();
}
catch(Exception e) { }
try {
el = msg.getMessageElement(null, "mynameis");
if(el.toString() == null)
print("null name received.\n");
else users.put(el.toString(), new Long(System.currentTimeMillis()));
}
catch(Exception e) { }
try {
el = msg.getMessageElement(null, "msg");
if(el.toString() == null)
print("null msg received.\n");
else {
ChatMessageEvent new_event =
new ChatMessageEvent(this, el.toString());
Listener.chatMessageEvent(new_event);
}
}
catch(Exception e) { }
}
private net.jxta.endpoint.StringMessageElement createMsgElmnt(String type,
String msg)
{
return new net.jxta.endpoint.StringMessageElement(type, msg, null);
}
public void send(String Message) throws java.io.IOException
{
net.jxta.endpoint.StringMessageElement sme =
createMsgElmnt("msg", UserName + ": " + Message);
send(sme);
}
private void send(net.jxta.endpoint.StringMessageElement sme)
throws java.io.IOException
{
net.jxta.endpoint.Message msg = new net.jxta.endpoint.Message();
msg.addMessageElement(null, sme);
netOutPipe.send(msg);
}
private void send(net.jxta.endpoint.StringMessageElement[] sme)
throws java.io.IOException
{
net.jxta.endpoint.Message msg = new net.jxta.endpoint.Message();
for(int i = 0; i < sme.length; ++i)
msg.addMessageElement(null, sme[i]);
netOutPipe.send(msg);
}
public void ping() throws java.io.IOException
{
net.jxta.endpoint.StringMessageElement sme =
createMsgElmnt("whoareyou",
UserName);
send(sme);
}
private void iAm() throws java.io.IOException
{
net.jxta.endpoint.StringMessageElement sme =
createMsgElmnt("mynameis",
UserName);
send(sme);
}
}
private boolean uninitialized = true;
private boolean isInitializing = false;
private java.util.Hashtable Groups = new java.util.Hashtable();
private java.util.Hashtable handling = new java.util.Hashtable();
private java.util.Hashtable pipes = new java.util.Hashtable();
private Thread runner;
static net.jxta.peergroup.PeerGroup netPeerGroup = null;
private net.jxta.discovery.DiscoveryService netDiscoveryService;
private net.jxta.pipe.PipeService netPipeService;
private boolean isHandling = false;
private EventPrinter eventprinter = null;
Chat(EventPrinter eventprinter)
{
runner = new Thread(this);
this.eventprinter = eventprinter;
}
private void print(String Message)
{
if(eventprinter != null) eventprinter.print(Message);
else System.out.print(Message);
}
public void JoinGroup(String GroupName,
String UserName,
ChatMessageListener Listener)
{
if(isInitializing) {
print("Cannot join group " +
GroupName +
" : connection has not initialized.\n");
return;
}
if(uninitialized) initialize();
if(Groups.containsKey(GroupName)) {
print("Cannot join group " +
GroupName +
" : you have already joined it.\n");
return;
}
print("Trying to join " + GroupName + '\n');
handling.put(GroupName,
new HandledGroup(GroupName, UserName, Listener, false));
if(!runner.isAlive()) runner.start();
}
public void LeaveGroup(String GroupName)
{
if(isInitializing) {
print("Cannot leave group " +
GroupName +
" : connection has not initialized.\n");
return;
}
if(uninitialized) initialize();
Joined group = (Joined)Groups.get(GroupName);
if(group == null) return;
if(group.netOutPipe != null) {
group.netOutPipe.close();
group.netOutPipe = null;
}
if(group.netInPipe != null) {
group.netInPipe.close();
group.netInPipe = null;
}
net.jxta.membership.MembershipService membership =
group.netPeerGroup.getMembershipService();
try {
membership.resign();
print("You are no longer connected to " + GroupName + '\n');
}
catch(Exception e) {
print("\nFailed to resign from group " + GroupName + '\n');
}
Groups.remove(GroupName);
}
public boolean IsConnectedToGroup(String GroupName)
{
if(isInitializing) {
return false;
}
if(uninitialized) initialize();
Joined group = (Joined)Groups.get(GroupName);
return ((group != null) && (group.netOutPipe != null));
}
public String[] ListMembers(String GroupName)
{
if(isInitializing || !IsConnectedToGroup(GroupName)) {
return new String[0];
}
if(uninitialized) initialize();
try {
Joined group = (Joined)Groups.get(GroupName);
group.ping();
Thread.sleep(5 * 1000);
java.util.LinkedList result = new java.util.LinkedList();
java.util.Enumeration keys = group.users.keys();
long time = System.currentTimeMillis(), update;
String user;
while(keys.hasMoreElements()) {
user = (String)keys.nextElement();
update = ((Long)group.users.get(user)).longValue();
if(time - update < 60 * 1000) result.addLast(user);
else {
group.users.remove(user);
}
}
String[] res = (String[])result.toArray(new String[result.size()]);
return res;
}
catch(Exception e) {
print("Failed!\n");
e.printStackTrace();
}
return new String[0];
}
public void SendMessage(String Message, String GroupName)
{
if(isInitializing) {
print("Cannot send message to group " +
GroupName +
" : connection has not initialized.\n");
return;
}
if(uninitialized) initialize();
if((Message == null) || (Message.compareTo("") == 0)) return;
if(!IsConnectedToGroup(GroupName)) {
print("Cannot send message to group " +
GroupName +
" : You are not connected to the group\n");
return;
}
try {
Joined group = (Joined)Groups.get(GroupName);
group.send(Message);
}
catch(Exception e) {
print("Error sending message\n");
e.printStackTrace();
}
}
private void initialize()
{
isInitializing = true;
try {
print("Initializing Peergroup...");
netPeerGroup = net.jxta.peergroup.PeerGroupFactory.newNetPeerGroup();
print("initialized!\n");
print("Initializing Discovery Service...");
netDiscoveryService = netPeerGroup.getDiscoveryService();
print("initialized!\n");
print("Initializing Pipe Service...");
netPipeService = netPeerGroup.getPipeService();
print("initialized!\n");
print("Initializing Rendezvous Service...");
net.jxta.rendezvous.RendezVousService netRendezVousService =
netPeerGroup.getRendezVousService();
print("initialized!\n");
if(netRendezVousService.isRendezVous()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -