APP Build a Social Network with Realtime Event

Joe

Thành viên VIP
21/1/13
3,024
1,336
113
Hi

I have introduced the Realtime Programming in JAVA. As you can see in the RT.zip the package consists of only two little APIs. The first one is an interface (RTEVent) and the second one is the Event-Trigger (or Event-Publisher) and Event-Consumer (RTEventMonitor). The examples show you how to implement an app using RTEvent that bases on the Multicast-IP technology.

The Multicast IP technology is usually applied for the Messaging System (MS). And I have already shown you in my blog Instant Messaging System (IMS) how to build yourself an IMS. The purpose of IMS is to allow you to send and receive messages ONTIME (or in realtime). Today I show you a REAL application using RTEvent and RTEventMonitor. It's about the classic "Chat application" which is usually a Client-Server app.

To do that we have to set up some rules (or conventions). RTEvent conveys a string that has the following formats and rules:
  • Command:RTEvent-ID (or user-ID): activity requests: login, register, quit, unregister
    Code:
    monitor.publishEvent("JoeNartca", "login:JoeNartca"); // userID JoeNartca tries to login
  • [From userID][To] message: userID publishes (sends) a message to another userID
    Code:
    monitor.publishEvent("ErikaSchwarz", "[From JoeNartca][ErikaSchwarz]Hello."); // userID JoeNartca tries to send Hello to userID ErikaSchwarz
  • *[From userID] message: userID publishes (sends) a message to the server
    Code:
    monitor.publishEvent("DudeHiveID", "*[From JoeNartca] Hello."); // userID JoeNartca tries to send Hello to the server admin
  • The package is quite small. It contains TWO applications: Server (DudeHive.java, 238 code lines incl. comments) and Client (Dude.java, 165 code lines incl. comments) and DudeList.txt, a plain text file, used as the "database". The DB-entries are for example:
Code:
DudeHiveQueen
JoeNartca
ErikaSchwarz
JavaDude
Both apps are written in simple SWING. Note: if you want to beautify them with other LAF (such as Nimbus) you have to be carefully to balance and to juggle the threads between SWING, NIMBUS, and RTEventMonitor when SWING components are updated. Otherwise you could run into an ugly situation with a very long exception list. Example:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at java.desktop/javax.swing.plaf.basic.BasicComboBoxUI.isPopupVisible(Unknown Source)
        at java.desktop/javax.swing.JComboBox.isPopupVisible(Unknown Source)
        at java.desktop/javax.swing.plaf.synth.SynthComboBoxUI$ButtonHandler.isPressed(Unknown Source)
        at java.desktop/javax.swing.plaf.synth.SynthComboBoxUI.getComponentState(Unknown Source)
        at java.desktop/javax.swing.plaf.synth.SynthComboBoxUI.getContext(Unknown Source)
        at java.desktop/javax.swing.plaf.synth.SynthComboBoxUI.update(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintComponent(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
        ...
Beside the NullPointerException the GUI appearance could be in some cases distorted (e.g. JComboBox). And that could balloon the apps. Reasons: problems with thread-synchronization. It's an age-old JAVA bug (click HERE for more). The bug is NOT fully eliminated :( The minimal thread synchronization between SWING and NON-SWING thread (RTEventMonitor) can be coded as following:
Java:
SwingUtilities.invokeLater(() -> {
  ...
});
Example: synchronization between JButton/JTexField/JComboBox and RTEventMonitor.
Java:
    ...
    SwingUtilities.invokeLater(() -> {
      if (!isOnline) {
        jta.append(userID+" is online...\n");
        register.setText("UNREGISTER");
        login.setText("QUIT");
        jft.setEditable(false);
        isOnline = true;
      }
      box.removeAllItems();
      onlineList = msg.split("\r");
      for (String id : onlineList) {
        if (!id.equals(userID)) dude.monitorEvent(this, id);
        box.addItem(id);
      }
      pack();
    });
    ...
How the codes work?
What you need is to have 3 computers or 3 consoles (on your computer). Download the dude.zip (and the RT.zip from Realtime Programming in JAVA) to a directory and modify the setcp.bat (WINDOWS) according to the path of realtime.jar. Then compile the codes as following:

socialNetwork_6.png

New user has to register before (s)he can joins the social network DudeHive. Existed users need only to login. It's a realtime communication between users (Dudes) and Server (DudeHive) in Peer-to-Peer principle (P2P). Only the server (DudeHive) can publish an event to ALL online users (dudes) in Publish-Subscriber principle (PS). The ComboBox (containing all on line dudes) is auto-updated whenever a new dude joins the hive, or quits, or unregisters. To "chat" with another dude a dude needs firstly to choose a dude ID (combobox) and hits the DUDE button for the prompted input of message...The following screenshots show you the busy DudeHive...just like the bees and the beehive

socialNetwork_1.png

socialNetwork_2.png

socialNetwork_3.png

socialNetwork_4.png

socialNetwork_5.png

and the app: dude.zip. The 2 last screenshots show you the situation that the server DudeHive is unexpectedly for all online dudes either DOWN or UP again. The RTEvent informs them online in realtime on each event (down or up). And the online Dude app automatically switches LOGIN/REGISTER in case of DOWN.
 

Attachments

Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
3,024
1,336
113
Here are the SWING_Nimbus and JFX
nimbus.png

JFX.png

all.zip contains SWING, Nimbus and JFX examples and the realtime.jar
 

Attachments

Sửa lần cuối: