all may be done
This commit is contained in:
@@ -9,8 +9,7 @@ public class ChatHandler {
|
||||
|
||||
private String userId, corespondent;
|
||||
private Connection connection;
|
||||
private Session /*session,*/ senderSession, receiverSession;
|
||||
private int status = 0; // 0: offline, 1: online
|
||||
private Session senderSession, receiverSession;
|
||||
private final Map<String, MessageProducer> blocked = new ConcurrentHashMap<>();
|
||||
private final String TAG = "Task3_queue_";
|
||||
private final String BROKER_URL = "tcp://localhost:61616";
|
||||
@@ -22,7 +21,6 @@ public class ChatHandler {
|
||||
ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
|
||||
this.connection = factory.createConnection();
|
||||
this.connection.start();
|
||||
//this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
this.senderSession = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
this.receiverSession = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
}
|
||||
@@ -65,17 +63,18 @@ public class ChatHandler {
|
||||
// commands to block or unblock contact
|
||||
if(line.startsWith("-")){
|
||||
delegateBlockingManagement(sc,line);
|
||||
//return true;
|
||||
// handling edge case where instead of user, exit comand is given
|
||||
return !corespondent.equalsIgnoreCase("exit");
|
||||
}
|
||||
|
||||
if (line.equalsIgnoreCase("exit")) {
|
||||
System.out.println("Exiting conversation...");
|
||||
//return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Send message to receiver
|
||||
sendMessage(createProducer(corespondent), line);
|
||||
return !line.equalsIgnoreCase("exit");//true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,8 +87,17 @@ public class ChatHandler {
|
||||
|
||||
// block command is -b followed by contact id
|
||||
if(line.startsWith("-b")){
|
||||
blockContact(line);
|
||||
promptForCorespondent(sc);
|
||||
try {
|
||||
boolean wasCorespondent = blockContact(line);
|
||||
// if we blocked current correspondent, prompt for new one
|
||||
if(wasCorespondent){
|
||||
System.out.println("You have blocked your current correspondent. Please choose a new one.");
|
||||
promptForCorespondent(sc);
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// unblock command is -u followed by contact id
|
||||
@@ -116,7 +124,6 @@ public class ChatHandler {
|
||||
MessageConsumer contact = createConsumer();
|
||||
|
||||
// add consumer to contacts map
|
||||
//contacts.put(receiver, contact);
|
||||
return getThread(contact);
|
||||
}
|
||||
|
||||
@@ -136,21 +143,32 @@ public class ChatHandler {
|
||||
* Block a contact
|
||||
* @param command
|
||||
*/
|
||||
private void blockContact(String command) {
|
||||
private boolean blockContact(String command) throws JMSException {
|
||||
// remove the command part to get the contact id
|
||||
String contact = command.replace("-b", "").trim();
|
||||
try {
|
||||
MessageProducer ignored = createProducer(contact);
|
||||
// remove from contacts map
|
||||
// and close his ass
|
||||
if(ignored != null){
|
||||
ignored.close();
|
||||
blocked.put(contact, ignored);
|
||||
System.out.println(contact + " has been blocked.");
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
System.err.println("Error unblocking consumer: " + e.getMessage());
|
||||
if(contact.isEmpty()){
|
||||
throw new JMSException("No contact specified to block.");
|
||||
}
|
||||
|
||||
boolean isCurrentCorespondent = contact.equals(getCorespondent());
|
||||
MessageProducer ignored = createProducer(contact);
|
||||
|
||||
// check if already blocked
|
||||
if(blocked.containsKey(contact)){
|
||||
System.out.println(contact + " is already blocked.");
|
||||
return isCurrentCorespondent;
|
||||
}
|
||||
// remove from contacts map
|
||||
// and close his ass
|
||||
if(ignored != null){
|
||||
ignored.close();
|
||||
blocked.put(contact, ignored);
|
||||
System.out.println(contact + " has been blocked.");
|
||||
return isCurrentCorespondent;
|
||||
}else{
|
||||
throw new JMSException("Could not block contact: " + contact);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -219,6 +237,7 @@ public class ChatHandler {
|
||||
|
||||
/**
|
||||
* Returns a thread object to handle receiving messages
|
||||
* checks if sender is blocked before displaying
|
||||
* @param consumer
|
||||
* @return Thread
|
||||
*/
|
||||
@@ -232,13 +251,18 @@ public class ChatHandler {
|
||||
// receive message with timeout
|
||||
Message msg = consumer.receive(100); // 100ms timeout
|
||||
if (msg instanceof TextMessage tm) {
|
||||
// handle received message
|
||||
String sender = tm.getStringProperty("sender");
|
||||
|
||||
// display message if sender is not blocked
|
||||
if (!isBlocked(sender)) {
|
||||
String body = tm.getText();
|
||||
displayMessage(sender, body);
|
||||
// message contents
|
||||
String sender = tm.getStringProperty("sender");
|
||||
String body = tm.getText();
|
||||
|
||||
// display conditions
|
||||
boolean isBlocked = blocked.containsKey(sender);
|
||||
boolean isEmptyMessage = body.isBlank() || body.isEmpty();
|
||||
|
||||
// display only if not blocked and not empty
|
||||
if (!isBlocked && !isEmptyMessage) {
|
||||
System.out.println("[" + sender + "]: " + body);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -258,47 +282,6 @@ public class ChatHandler {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display received message
|
||||
* Checks if sender is blocked
|
||||
* checks if message is empty for status update
|
||||
* checks for exit condition
|
||||
* @param tm
|
||||
* @throws JMSException
|
||||
*/
|
||||
|
||||
private void displayMessage(String sender, String body) {
|
||||
|
||||
|
||||
// Exit condition because correspondent went offline
|
||||
if(body.equalsIgnoreCase("exit")) {
|
||||
setStatus(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// display only if not empty
|
||||
if(!body.isBlank() || !body.isEmpty()) {
|
||||
// Status handling for online/offline
|
||||
System.out.println("[" + sender + "]: " + body);
|
||||
}
|
||||
setStatus(1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if sender is blocked
|
||||
* if sender is blocked, print info message
|
||||
* @param sender
|
||||
* @return
|
||||
*/
|
||||
private boolean isBlocked(String sender) {
|
||||
boolean isBlocked = blocked.containsKey(sender);
|
||||
if(isBlocked){
|
||||
System.out.println(" >> Message from '"+sender+"' ignored. because user is blocked.");
|
||||
}
|
||||
return isBlocked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt for user ID
|
||||
* @param sc
|
||||
@@ -306,7 +289,6 @@ public class ChatHandler {
|
||||
public void promptForUser(Scanner sc){
|
||||
|
||||
System.out.print("Enter your user ID: ");
|
||||
//userId = prompt(sc);
|
||||
setUserId(prompt(sc));
|
||||
// Instructions
|
||||
System.out.print("Welcome "+getUserId());
|
||||
@@ -327,6 +309,7 @@ public class ChatHandler {
|
||||
System.out.println(contact + " is currently blocked. Unblock to chat.");
|
||||
return;
|
||||
}
|
||||
System.out.println("You can start with: " + getCorespondent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,30 +350,5 @@ public class ChatHandler {
|
||||
|
||||
public void setCorespondent(String corespondent) {
|
||||
this.corespondent = corespondent;
|
||||
System.out.println("You can chat with: " + corespondent);
|
||||
// reset old status when new corepondent is set
|
||||
setStatus(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates corespondents status
|
||||
* @param status
|
||||
*/
|
||||
private void setStatus(int status) {
|
||||
|
||||
// just in case of duplicate status messages, if status is same, return
|
||||
if(this.status == status) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update status
|
||||
this.status = status;
|
||||
|
||||
// and print accordingly
|
||||
if(status == 0) {
|
||||
System.out.println(getCorespondent()+" is now offline.");
|
||||
} else {
|
||||
System.out.println(getCorespondent()+" is now online.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user