mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 08:12:05 +01:00
Remove init() in MultiUserChat
and mark more members 'final' Also use CopyOnWriteArraySet for presenceInterceptors.
This commit is contained in:
parent
e835df5641
commit
fd7b747987
1 changed files with 106 additions and 113 deletions
|
@ -94,12 +94,9 @@ public class MultiUserChat {
|
||||||
private static Map<XMPPConnection, List<String>> joinedRooms =
|
private static Map<XMPPConnection, List<String>> joinedRooms =
|
||||||
new WeakHashMap<XMPPConnection, List<String>>();
|
new WeakHashMap<XMPPConnection, List<String>>();
|
||||||
|
|
||||||
private XMPPConnection connection;
|
private final XMPPConnection connection;
|
||||||
private String room;
|
private final String room;
|
||||||
private String subject;
|
private final Map<String, Presence> occupantsMap = new ConcurrentHashMap<String, Presence>();
|
||||||
private String nickname = null;
|
|
||||||
private boolean joined = false;
|
|
||||||
private Map<String, Presence> occupantsMap = new ConcurrentHashMap<String, Presence>();
|
|
||||||
|
|
||||||
private final List<InvitationRejectionListener> invitationRejectionListeners =
|
private final List<InvitationRejectionListener> invitationRejectionListeners =
|
||||||
new ArrayList<InvitationRejectionListener>();
|
new ArrayList<InvitationRejectionListener>();
|
||||||
|
@ -111,14 +108,17 @@ public class MultiUserChat {
|
||||||
new ArrayList<ParticipantStatusListener>();
|
new ArrayList<ParticipantStatusListener>();
|
||||||
private final Set<MessageListener> messageListeners = new CopyOnWriteArraySet<MessageListener>();
|
private final Set<MessageListener> messageListeners = new CopyOnWriteArraySet<MessageListener>();
|
||||||
private final Set<PresenceListener> presenceListeners = new CopyOnWriteArraySet<PresenceListener>();
|
private final Set<PresenceListener> presenceListeners = new CopyOnWriteArraySet<PresenceListener>();
|
||||||
|
private final Set<PacketInterceptor> presenceInterceptors = new CopyOnWriteArraySet<PacketInterceptor>();
|
||||||
|
private final ConnectionDetachedPacketCollector<Message> messageCollector = new ConnectionDetachedPacketCollector<Message>();
|
||||||
|
|
||||||
private final PacketFilter fromRoomFilter;
|
private final PacketFilter fromRoomFilter;
|
||||||
private final PacketListener messageListener;
|
private final PacketListener messageListener;
|
||||||
private final PacketListener presenceListener;
|
private final PacketListener presenceListener;
|
||||||
|
private final RoomListenerMultiplexor roomListenerMultiplexor;
|
||||||
|
|
||||||
private List<PacketInterceptor> presenceInterceptors = new ArrayList<PacketInterceptor>();
|
private String subject;
|
||||||
private RoomListenerMultiplexor roomListenerMultiplexor;
|
private String nickname = null;
|
||||||
private ConnectionDetachedPacketCollector<Message> messageCollector;
|
private boolean joined = false;
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||||
|
@ -200,7 +200,102 @@ public class MultiUserChat {
|
||||||
};
|
};
|
||||||
fromRoomFilter = FromMatchesFilter.create(room);
|
fromRoomFilter = FromMatchesFilter.create(room);
|
||||||
|
|
||||||
init();
|
// Create a listener for subject updates.
|
||||||
|
PacketListener subjectListener = new PacketListener() {
|
||||||
|
public void processPacket(Packet packet) {
|
||||||
|
Message msg = (Message) packet;
|
||||||
|
// Update the room subject
|
||||||
|
subject = msg.getSubject();
|
||||||
|
// Fire event for subject updated listeners
|
||||||
|
fireSubjectUpdatedListeners(
|
||||||
|
msg.getSubject(),
|
||||||
|
msg.getFrom());
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create a listener for all presence updates.
|
||||||
|
PacketListener presenceListener = new PacketListener() {
|
||||||
|
public void processPacket(Packet packet) {
|
||||||
|
Presence presence = (Presence) packet;
|
||||||
|
String from = presence.getFrom();
|
||||||
|
String myRoomJID = MultiUserChat.this.room + "/" + nickname;
|
||||||
|
boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
|
||||||
|
if (presence.getType() == Presence.Type.available) {
|
||||||
|
Presence oldPresence = occupantsMap.put(from, presence);
|
||||||
|
if (oldPresence != null) {
|
||||||
|
// Get the previous occupant's affiliation & role
|
||||||
|
MUCUser mucExtension = MUCUser.from(packet);
|
||||||
|
MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation();
|
||||||
|
MUCRole oldRole = mucExtension.getItem().getRole();
|
||||||
|
// Get the new occupant's affiliation & role
|
||||||
|
mucExtension = MUCUser.from(packet);
|
||||||
|
MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation();
|
||||||
|
MUCRole newRole = mucExtension.getItem().getRole();
|
||||||
|
// Fire role modification events
|
||||||
|
checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
|
||||||
|
// Fire affiliation modification events
|
||||||
|
checkAffiliationModifications(
|
||||||
|
oldAffiliation,
|
||||||
|
newAffiliation,
|
||||||
|
isUserStatusModification,
|
||||||
|
from);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// A new occupant has joined the room
|
||||||
|
if (!isUserStatusModification) {
|
||||||
|
List<String> params = new ArrayList<String>();
|
||||||
|
params.add(from);
|
||||||
|
fireParticipantStatusListeners("joined", params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (presence.getType() == Presence.Type.unavailable) {
|
||||||
|
occupantsMap.remove(from);
|
||||||
|
MUCUser mucUser = MUCUser.from(packet);
|
||||||
|
if (mucUser != null && mucUser.getStatus() != null) {
|
||||||
|
// Fire events according to the received presence code
|
||||||
|
checkPresenceCode(
|
||||||
|
mucUser.getStatus(),
|
||||||
|
presence.getFrom().equals(myRoomJID),
|
||||||
|
mucUser,
|
||||||
|
from);
|
||||||
|
} else {
|
||||||
|
// An occupant has left the room
|
||||||
|
if (!isUserStatusModification) {
|
||||||
|
List<String> params = new ArrayList<String>();
|
||||||
|
params.add(from);
|
||||||
|
fireParticipantStatusListeners("left", params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Listens for all messages that include a MUCUser extension and fire the invitation
|
||||||
|
// rejection listeners if the message includes an invitation rejection.
|
||||||
|
PacketListener declinesListener = new PacketListener() {
|
||||||
|
public void processPacket(Packet packet) {
|
||||||
|
// Get the MUC User extension
|
||||||
|
MUCUser mucUser = MUCUser.from(packet);
|
||||||
|
// Check if the MUCUser informs that the invitee has declined the invitation
|
||||||
|
if (mucUser.getDecline() != null &&
|
||||||
|
((Message) packet).getType() != Message.Type.error) {
|
||||||
|
// Fire event for invitation rejection listeners
|
||||||
|
fireInvitationRejectionListeners(
|
||||||
|
mucUser.getDecline().getFrom(),
|
||||||
|
mucUser.getDecline().getReason());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener(
|
||||||
|
messageCollector, presenceListener, subjectListener,
|
||||||
|
declinesListener);
|
||||||
|
|
||||||
|
roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection);
|
||||||
|
|
||||||
|
roomListenerMultiplexor.addRoom(room, packetMultiplexor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1938,108 +2033,6 @@ public class MultiUserChat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
|
||||||
// Create a collector for incoming messages.
|
|
||||||
messageCollector = new ConnectionDetachedPacketCollector<Message>();
|
|
||||||
|
|
||||||
// Create a listener for subject updates.
|
|
||||||
PacketListener subjectListener = new PacketListener() {
|
|
||||||
public void processPacket(Packet packet) {
|
|
||||||
Message msg = (Message) packet;
|
|
||||||
// Update the room subject
|
|
||||||
subject = msg.getSubject();
|
|
||||||
// Fire event for subject updated listeners
|
|
||||||
fireSubjectUpdatedListeners(
|
|
||||||
msg.getSubject(),
|
|
||||||
msg.getFrom());
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create a listener for all presence updates.
|
|
||||||
PacketListener presenceListener = new PacketListener() {
|
|
||||||
public void processPacket(Packet packet) {
|
|
||||||
Presence presence = (Presence) packet;
|
|
||||||
String from = presence.getFrom();
|
|
||||||
String myRoomJID = room + "/" + nickname;
|
|
||||||
boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
|
|
||||||
if (presence.getType() == Presence.Type.available) {
|
|
||||||
Presence oldPresence = occupantsMap.put(from, presence);
|
|
||||||
if (oldPresence != null) {
|
|
||||||
// Get the previous occupant's affiliation & role
|
|
||||||
MUCUser mucExtension = MUCUser.from(packet);
|
|
||||||
MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation();
|
|
||||||
MUCRole oldRole = mucExtension.getItem().getRole();
|
|
||||||
// Get the new occupant's affiliation & role
|
|
||||||
mucExtension = MUCUser.from(packet);
|
|
||||||
MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation();
|
|
||||||
MUCRole newRole = mucExtension.getItem().getRole();
|
|
||||||
// Fire role modification events
|
|
||||||
checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
|
|
||||||
// Fire affiliation modification events
|
|
||||||
checkAffiliationModifications(
|
|
||||||
oldAffiliation,
|
|
||||||
newAffiliation,
|
|
||||||
isUserStatusModification,
|
|
||||||
from);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// A new occupant has joined the room
|
|
||||||
if (!isUserStatusModification) {
|
|
||||||
List<String> params = new ArrayList<String>();
|
|
||||||
params.add(from);
|
|
||||||
fireParticipantStatusListeners("joined", params);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (presence.getType() == Presence.Type.unavailable) {
|
|
||||||
occupantsMap.remove(from);
|
|
||||||
MUCUser mucUser = MUCUser.from(packet);
|
|
||||||
if (mucUser != null && mucUser.getStatus() != null) {
|
|
||||||
// Fire events according to the received presence code
|
|
||||||
checkPresenceCode(
|
|
||||||
mucUser.getStatus(),
|
|
||||||
presence.getFrom().equals(myRoomJID),
|
|
||||||
mucUser,
|
|
||||||
from);
|
|
||||||
} else {
|
|
||||||
// An occupant has left the room
|
|
||||||
if (!isUserStatusModification) {
|
|
||||||
List<String> params = new ArrayList<String>();
|
|
||||||
params.add(from);
|
|
||||||
fireParticipantStatusListeners("left", params);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Listens for all messages that include a MUCUser extension and fire the invitation
|
|
||||||
// rejection listeners if the message includes an invitation rejection.
|
|
||||||
PacketListener declinesListener = new PacketListener() {
|
|
||||||
public void processPacket(Packet packet) {
|
|
||||||
// Get the MUC User extension
|
|
||||||
MUCUser mucUser = MUCUser.from(packet);
|
|
||||||
// Check if the MUCUser informs that the invitee has declined the invitation
|
|
||||||
if (mucUser.getDecline() != null &&
|
|
||||||
((Message) packet).getType() != Message.Type.error) {
|
|
||||||
// Fire event for invitation rejection listeners
|
|
||||||
fireInvitationRejectionListeners(
|
|
||||||
mucUser.getDecline().getFrom(),
|
|
||||||
mucUser.getDecline().getReason());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener(
|
|
||||||
messageCollector, presenceListener, subjectListener,
|
|
||||||
declinesListener);
|
|
||||||
|
|
||||||
roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection);
|
|
||||||
|
|
||||||
roomListenerMultiplexor.addRoom(room, packetMultiplexor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fires notification events if the role of a room occupant has changed. If the occupant that
|
* Fires notification events if the role of a room occupant has changed. If the occupant that
|
||||||
* changed his role is your occupant then the <code>UserStatusListeners</code> added to this
|
* changed his role is your occupant then the <code>UserStatusListeners</code> added to this
|
||||||
|
|
Loading…
Reference in a new issue