Created
July 3, 2021 20:28
-
-
Save thenriquedb/20c0ef373092858fcbb42401b60aacda to your computer and use it in GitHub Desktop.
Simple text-based chat application (SimpleChat), with the following features:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.simpleChat; | |
import org.jgroups.*; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
// Reference: http://www.jgroups.org/tutorial/#_configuration | |
public class SimpleChat extends ReceiverAdapter{ | |
/* | |
* JGroups uses a JChannel as the main API to connect to a cluster, send and receive messages, and to register | |
* listeners that are called when things (such as member joins) happen. | |
*/ | |
JChannel channel; | |
String userName = ""; | |
private void start(String userName) throws Exception { | |
// Alternatively, we could pass an XML file to configure the channel, e.g. new JChannel("/home/bela/udp.xml"). | |
channel = new JChannel(); // use default config, udp.xml | |
channel.receiver(this); | |
this.userName = userName; | |
/* | |
* Note that we don’t need to explicitly create a cluster beforehand; connect() creates the cluster if it is the | |
* first instance. | |
*/ | |
channel.connect("ChatCluster"); // Joins cluster "ChatCluster" | |
eventLoop(); | |
channel.close(); | |
} | |
/** | |
* Callback is called whenever a new instance joins the cluster, or an existing instance leaves (crashes included). | |
* Its | |
*/ | |
public void viewAccepted(View view) { | |
System.out.println("** view: " + view); | |
} | |
/** | |
* We simply get its buffer as an object (again using Java serialization) and print it to stdout. We also print | |
* the sender’s address (Message.getSrc()). | |
*/ | |
public void receive(Message message) { | |
System.out.println(message.getSrc() + ": " + message.getObject()); | |
} | |
/* | |
* We now run an event loop, which reads input from stdin (a message) and sends it to all instances currently in the | |
* cluster. When "exit" or "quit" are entered, we fall out of the loop and close the channel. | |
*/ | |
private void eventLoop() { | |
InputStreamReader inputStreamReader = new InputStreamReader(System.in); | |
BufferedReader input = new BufferedReader(inputStreamReader); | |
while(true) { | |
try { | |
System.out.println("> "); | |
System.out.flush(); | |
String line = input.readLine(); | |
if(line.startsWith("quit") || line.startsWith("exit")) break; | |
line = "["+userName+"]"+ line; | |
/* | |
* Message constructor | |
* First argument is the destination address | |
* |> A null destination address sends the message to everyone | |
* | |
* First argument is the source address | |
* |> This is null as well, as the stack will insert the correct address anyway. | |
* | |
*/ | |
Message message = new Message(null, null, line); | |
channel.send(message); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
new SimpleChat().start(args[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment