Skip to content

Commit

Permalink
fix: Clear old state when MUC is re-joined. (#952)
Browse files Browse the repository at this point in the history
* fix: Clear old state when MUC is re-joined.
* fix: Fix syncronization issues (spotbugs).
  • Loading branch information
bgrozev authored Aug 1, 2022
1 parent a8c89ac commit f326b31
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/main/java/org/jitsi/impl/protocol/xmpp/ChatRoomImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.jitsi.impl.protocol.xmpp;

import javax.xml.namespace.*;

import edu.umd.cs.findbugs.annotations.*;
import kotlin.*;
import org.jetbrains.annotations.*;
import org.jitsi.jicofo.*;
Expand All @@ -40,13 +42,18 @@
import org.jxmpp.stringprep.*;

import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;

/**
* Stripped implementation of <tt>ChatRoom</tt> using Smack library.
*
* @author Pawel Domas
*/
@SuppressFBWarnings(
value = "JLM_JSR166_UTILCONCURRENT_MONITORENTER",
justification = "We intentionally synchronize on [members] (a ConcurrentHashMap)."
)
public class ChatRoomImpl
implements ChatRoom, PresenceListener
{
Expand Down Expand Up @@ -96,7 +103,7 @@ public class ChatRoomImpl
*/
private final Consumer<ChatRoomImpl> leaveCallback;

private final Map<EntityFullJid, ChatMemberImpl> members = new HashMap<>();
private final Map<EntityFullJid, ChatMemberImpl> members = new ConcurrentHashMap<>();

/**
* Local user role.
Expand Down Expand Up @@ -212,6 +219,7 @@ public void join()
throws SmackException, XMPPException, InterruptedException
{
// TODO: clean-up the way we figure out what nickname to use.
resetState();
joinAs(xmppProvider.getConfig().getUsername());
}

Expand All @@ -225,6 +233,34 @@ public String getMainRoom() {
return mainRoom;
}

/**
* Prepare this {@link ChatRoomImpl} for a call to {@link #joinAs(Resourcepart)}, which send initial presence to
* the MUC. Resets any state that might have been set the previous time the MUC was joined.
*/
private void resetState()
{
synchronized (members)
{
if (!members.isEmpty())
{
logger.warn("Removing " + members.size() + " stale members.");
members.clear();
}
}

synchronized (this)
{
role = null;
lastPresenceSent = null;
meetingId = null;
logger.addContext("meeting_id", "");
isBreakoutRoom = false;
mainRoom = null;
avModerationEnabled.clear();
whitelists.clear();
}
}

private void joinAs(Resourcepart nickname) throws SmackException, XMPPException, InterruptedException
{
this.myOccupantJid = JidCreate.entityFullFrom(roomJid, nickname);
Expand All @@ -236,7 +272,7 @@ private void joinAs(Resourcepart nickname) throws SmackException, XMPPException,
// it indicates that the client lost its synchronization and causes
// the MUC service to re-send the presence of each occupant in the
// room.
synchronized (this)
synchronized (ChatRoomImpl.this)
{
lastPresenceSent = packet.asBuilder((String) null).removeExtension(
MUCInitialPresence.ELEMENT,
Expand Down Expand Up @@ -387,8 +423,7 @@ private void resetRoleForOccupant(EntityFullJid occupantJid)
}
else
{
logger.error(
"Role reset for: " + occupantJid + " who does not exist");
logger.error("Role reset for: " + occupantJid + " who does not exist");
}
}
}
Expand Down Expand Up @@ -426,10 +461,7 @@ public ChatMemberImpl getChatMember(EntityFullJid occupantJid)
return null;
}

synchronized (members)
{
return members.get(occupantJid);
}
return members.get(occupantJid);
}

@Override
Expand Down

0 comments on commit f326b31

Please sign in to comment.