Skip to content

Commit

Permalink
Add JavaDoc to ConnectedUser
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk committed Mar 7, 2024
1 parent 6e22cc5 commit f4d0f3f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
56 changes: 40 additions & 16 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ConnectedUser extends User {
private Roster roster;
private AbstractXMPPConnection connection = null; // When accessing this property, make sure it's not null
// (or just run InitializeConnection first)
private ChatManager chatManager = null; // Same warning as connection

public ConnectedUser(String username, String password, String serviceName) {
super(username, serviceName);
Expand All @@ -34,6 +35,7 @@ public ConnectedUser(String username, String password, String serviceName) {
/**
* The InitializeConnection method takes no parameters and connects to the server.
* After connecting, it sends a message to the test user.
* This method assigns the connection and chatManager properties.
*/
public void initializeConnection() throws SmackException, InterruptedException, XMPPException, IOException {
// 1. Create the connection configuration
Expand All @@ -49,7 +51,7 @@ public void initializeConnection() throws SmackException, InterruptedException,
connection.login();

// 3. Create a chat manager
ChatManager chatManager = ChatManager.getInstanceFor(connection);
chatManager = ChatManager.getInstanceFor(connection);

// 4. Get the roster
roster = Roster.getInstanceFor(connection);
Expand All @@ -61,11 +63,16 @@ public void initializeConnection() throws SmackException, InterruptedException,
if (roster.getEntry(dummyJid) == null) {
addContact(dummyJid.toString(), "Dummy");
} else {
sendMessage(dummyJid.toString(), "Hello, world!", chatManager);
sendMessage(dummyJid.toString(), "Hello, world!");
}
}
}

/**
* Adds a contact to the user's roster.
* @param contactJid the JID of the contact to add (user@service.name)
* @param nickname the user defined nickname of the contact, defaults to the contact's username
*/
private void addContact(String contactJid, String nickname) {
try {
BareJid bareJid = JidCreate.bareFrom(contactJid);
Expand All @@ -78,6 +85,10 @@ private void addContact(String contactJid, String nickname) {
}
}

/**
* Removes a contact from the user's roster.
* @param contactJid the JID of the contact to remove (user@service.name)
*/
private void removeContact(String contactJid) {
try {
RosterEntry entry = roster.getEntry(JidCreate.entityBareFrom(contactJid));
Expand All @@ -89,7 +100,12 @@ private void removeContact(String contactJid) {
}
}

private void sendMessage(String contactJid, String message, ChatManager chatManager) {
/**
* Sends a message to a contact.
* @param contactJid the JID of the contact to send the message to (user@service.name)
* @param message the message to send
*/
private void sendMessage(String contactJid, String message) {
try {
EntityBareJid recipientJid = JidCreate.entityBareFrom(contactJid);
Chat chat = chatManager.chatWith(recipientJid);
Expand All @@ -99,26 +115,22 @@ private void sendMessage(String contactJid, String message, ChatManager chatMana
}
}

public void disconnect() {
connection.disconnect();
}

/**
* Accepts a subscription request from a contact.
* @param contactJid the JID of the contact to accept the subscription from (user@service.name)
* @param nickname the user defined nickname of the contact, defaults to the contact's username
*/
public void acceptSubscription(String contactJid, String nickname) {
if (nickname == null || nickname.isEmpty()) {
nickname = contactJid.split("@")[0];
}
try {
roster.createItemAndRequestSubscription(JidCreate.bareFrom(contactJid), nickname, null);
} catch (Exception e) {
System.err.println("Error accepting subscription: " + e);
}
}

public Roster getRoster() {
return roster;
}

public boolean isLoggedIn() {
return connection.isAuthenticated();
}


/**
* Saves the user information (excluding password) to a file.
* @param filename The name of the file to save the user information to.
Expand Down Expand Up @@ -161,4 +173,16 @@ public static ConnectedUser loadUserFromFile(String filename) {
}
return connectedUser;
}

public Roster getRoster() {
return roster;
}

public boolean isLoggedIn() {
return connection.isAuthenticated();
}

public void disconnect() {
connection.disconnect();
}
}
2 changes: 1 addition & 1 deletion src/main/java/team/elrant/bubbles/xmpp/DummyUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String[] args) {
try {
user.initializeConnection();
} catch (Exception e){
e.printStackTrace();
e.printStackTrace(); // Replace with more robust error handling in the future
}
// Accept every incoming subscription request
Roster roster = user.getRoster();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main(String[] args) {
user.initializeConnection();
System.out.println("Connection established!");
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace(); // Replace with more robust error handling in the future
}
user.disconnect();
}
Expand Down

0 comments on commit f4d0f3f

Please sign in to comment.