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

📄 chatcontroller.java

📁 基于jxta的peer to peer 聊天程序源代码。
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*; 
import net.jini.space.JavaSpace;
import net.jini.core.lease.*;
import org.jworkplace.*;

/*
 *  The ChatController class is used to create the ChatView and control the
 *  reading and writing of messages to the chat channel
*/

public class ChatController implements ActionListener {

   // The name of the channel
   private String channel = null;

   // name of user in chat session
   private String userName = null;

   // Simple Swing JPanel
   private ChatViewer viewer = null;

   // Thread to read new messages
   private ChannelListener cl = null;
   private volatile Thread listener;

   // Messages exist for 1 hour
   public long CHAT_TIME = 60 * 60 * 1000; // 1 hour

   private JavaSpace space;
   private JFrame frame;

   public ChatController(JFrame frame, JavaSpace space, String channel, String user) {

         this.frame = frame;
         this.space = space;
         this.userName = user;
         this.channel = channel;
         viewer = new ChatViewer(this,channel);

         // start the channel listener thread
         cl = new ChannelListener();
         cl.start();

         // determine if there is already a session open
         if(!activeChannel())
           createChannel();
   }

   // Does Tail entry exist for this channel?
   synchronized boolean activeChannel() {
      Tail template = new Tail();
      template.name = channel;
      try {
         Tail tail = (Tail)space.readIfExists(template, null,
                              JavaSpace.NO_WAIT);
         if(tail == null) return false;
      } catch (Exception e) {}
      return true;
   }

   // if no active channel create and initialize new Tail
   private void createChannel() {
         Tail tail = new Tail();
         tail.name = channel;
         showStatus("Creating channel "+ channel);
         tail.position = new Integer(0);
         try {
             Lease lease = space.write(tail, null, CHAT_TIME);
         } catch (Exception e) {
             e.printStackTrace();
             return;
         }
       }

   // Action to display message and append to channel
   public void actionPerformed(ActionEvent event) {
      Object object = event.getSource();
      if (object == viewer.chatControl) {
         String message = userName+ "> "+viewer.getMessage();
         String to = viewer.getToAddress();
         String from = viewer.getFromAddress();
         if (message.equals("")) {
            JOptionPane.showMessageDialog(frame, "Enter message");
            return;
         }
         viewer.setMessage("");
         append(channel, to, from, message);
       }
   }

      // append message to channel
      private void append(String channel, String to, String from, String msg) {
           // get the next available position
          Integer messageNum = getMessageNumber();
           // create a new message using the new position
          ChatMessage message = new ChatMessage(channel, messageNum, to, from, msg);
          try {
              // write to space
              Lease lease = space.write(message, null, CHAT_TIME);
          } catch (Exception e) { e.printStackTrace();  return; }
      }


   // get the current tail and increment
   private Integer getMessageNumber() {
      try {
         Tail template = new Tail();
         template.name = channel;
         Tail tail = (Tail)space.take(template, null, 10 * 1000);
         // If no tail exists create a new channel
         if (tail == null) {
             createChannel();
             tail = (Tail) space.take(template, null, Long.MAX_VALUE);
         }
         // increment the tail position
         tail.increment();
         // write the tail to space
         Lease lease = space.write(tail, null, CHAT_TIME);
         // return the next position
         return tail.position;
      } catch (Exception e) {
          e.printStackTrace();
          return null;
       }
   }

   public JPanel getChatView() { return viewer; }


   // set the listener to null and interrupt the thread
   public void windowClosing() {
       listener = null;
       cl.interrupt();
   }


// The channel listener
public class ChannelListener extends Thread  {
   int position = 1;
   String newline;

   public ChannelListener() {
      newline = System.getProperty("line.separator");
      // If joining an existing chat display chat history
      if(activeChannel()) {
         try {
           ChatMessage template = new ChatMessage();
           template.channel = channel;
           ChatMessage msg = template;
           // loop through all messages starting at 2
           while(msg != null) {
               template.position = new Integer(position++);
               msg = (ChatMessage)space.readIfExists(template, null,
                                            JavaSpace.NO_WAIT);
               if(msg != null) {
                   viewer.append(msg.text + newline);
               } else {
                   position--;
               }
            }
          } catch (Exception e) { e.printStackTrace(); }
       }
   }

   // run until interrupted
   public void run() {
      listener = Thread.currentThread();
      while(listener != null) {
         ChatMessage template = new ChatMessage();
         template.channel = channel;
         ChatMessage msg = null;
         // increment the current position
         template.position = new Integer(position++);
         try {
             // wait till message arrives
            msg = (ChatMessage)space.read(template, null, Long.MAX_VALUE);
            // display new message
            viewer.append(msg.text + newline);
         } catch (Exception e) { }
      }
   }
 }

}


⌨️ 快捷键说明

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