Commit 3fb9610d authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Initial version. (MUC cluster support).

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8788 b35dd754-fafc-0310-a699-88a17e54d16e
parent 52a517c6
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.Message;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that broadcasts a message to local room occupants. When a room occupant sends a
* message to the room each cluster node will execute this task and broadcast the message
* to its local room occupants.
*
* @author Gaston Dombiak
*/
public class BroascastMessageRequest extends MUCRoomTask {
private int occupants;
private Message message;
public BroascastMessageRequest() {
}
public BroascastMessageRequest(LocalMUCRoom room, Message message, int occupants) {
super(room);
this.message = message;
this.occupants = occupants;
}
public Message getMessage() {
return message;
}
public int getOccupants() {
return occupants;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().broadcast(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) message.getElement());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
message = new Message(packetElement, true);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.Presence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that broadcasts the presence of a room occupant to the occupants of the room
* being hosted by the cluster node. When a room occupant changes his presence an
* instance of this class will be sent to each cluster node and when executed a broadcast
* of the updated presence will be sent to local room occupants.
*
* @author Gaston Dombiak
*/
public class BroascastPresenceRequest extends MUCRoomTask {
private Presence presence;
public BroascastPresenceRequest() {
}
public BroascastPresenceRequest(LocalMUCRoom room, Presence message) {
super(room);
this.presence = message;
}
public Presence getPresence() {
return presence;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().broadcast(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
presence = new Presence(packetElement, true);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.Presence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that changes the nickname of an existing room occupant in the cluster node. When
* a room occupant changes his nickname the other cluster nodes, that hold a
* {@link org.jivesoftware.openfire.muc.spi.RemoteMUCRole} will need to update their local
* information with the new nickname.
*
* @author Gaston Dombiak
*/
public class ChangeNickname extends MUCRoomTask {
private String oldNick;
private String newNick;
private Presence presence;
public ChangeNickname() {
}
public ChangeNickname(LocalMUCRoom room, String oldNick, String newNick, Presence presence) {
super(room);
this.oldNick = oldNick;
this.newNick = newNick;
this.presence = presence;
}
public String getOldNick() {
return oldNick;
}
public String getNewNick() {
return newNick;
}
public Presence getPresence() {
return presence;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().nicknameChanged(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
ExternalizableUtil.getInstance().writeSafeUTF(out, oldNick);
ExternalizableUtil.getInstance().writeSafeUTF(out, newNick);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
presence = new Presence(packetElement, true);
oldNick = ExternalizableUtil.getInstance().readSafeUTF(in);
newNick = ExternalizableUtil.getInstance().readSafeUTF(in);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that destroys the local room in the cluster node. Local room occupants
* hosted in the cluster node will get the notification of the room being
* destroyed.
*
* @author Gaston Dombiak
*/
public class DestroyRoomRequest extends MUCRoomTask {
private String alternateJID;
private String reason;
public DestroyRoomRequest() {
}
public DestroyRoomRequest(LocalMUCRoom room, String alternateJID, String reason) {
super(room);
this.alternateJID = alternateJID;
this.reason = reason;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().destroyRoom(this);
}
public String getAlternateJID() {
return alternateJID;
}
public String getReason() {
return reason;
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeBoolean(out, alternateJID != null);
if (alternateJID != null) {
ExternalizableUtil.getInstance().writeSafeUTF(out, alternateJID);
}
ExternalizableUtil.getInstance().writeBoolean(out, reason != null);
if (reason != null) {
ExternalizableUtil.getInstance().writeSafeUTF(out, reason);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
if (ExternalizableUtil.getInstance().readBoolean(in)) {
alternateJID = ExternalizableUtil.getInstance().readSafeUTF(in);
}
if (ExternalizableUtil.getInstance().readBoolean(in)) {
reason = ExternalizableUtil.getInstance().readSafeUTF(in);
}
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ClusterTask;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Task requested by each cluster node when a new node joins the cluster. Each existing cluster
* node will request the list of rooms with occupants <tt>hosted by</tt> the new cluster node.
*
* @author Gaston Dombiak
*/
public class GetNewMemberRoomsRequest implements ClusterTask {
private List<RoomInfo> rooms;
public GetNewMemberRoomsRequest() {
}
public Object getResult() {
return rooms;
}
public void run() {
rooms = new ArrayList<RoomInfo>();
// Get rooms that have local occupants and include them in the reply
MultiUserChatServer mucServer = XMPPServer.getInstance().getMultiUserChatServer();
for (MUCRoom room : mucServer.getChatRooms()) {
LocalMUCRoom localRoom = (LocalMUCRoom) room;
Collection<MUCRole> localOccupants = new ArrayList<MUCRole>();
for (MUCRole occupant : room.getOccupants()) {
if (occupant.isLocal()) {
localOccupants.add(occupant);
}
}
if (!localOccupants.isEmpty()) {
rooms.add(new RoomInfo(localRoom, localOccupants));
}
}
}
public void writeExternal(ObjectOutput out) throws IOException {
// Do nothing
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// Do nothing
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.spi.MultiUserChatServerImpl;
import org.jivesoftware.util.cache.ClusterTask;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task to be executed in each cluster node to obtain the total number of
* users using the multi user chat service.
*
* @author Gaston Dombiak
*/
public class GetNumberConnectedUsers implements ClusterTask{
private Integer count;
public Object getResult() {
return count;
}
public void run() {
MultiUserChatServerImpl mucServer = (MultiUserChatServerImpl) XMPPServer.getInstance().getMultiUserChatServer();
count = mucServer.getNumberConnectedUsers(true);
}
public void writeExternal(ObjectOutput out) throws IOException {
// Do nothing
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// Do nothing
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ClusterTask;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task related to a room to be executed in a cluster node. This is a base
* class to specific room tasks. The base class just keeps track of the room
* related to the task.
*
* @author Gaston Dombiak
*/
public abstract class MUCRoomTask implements ClusterTask {
private boolean originator;
private LocalMUCRoom room;
protected MUCRoomTask() {
}
protected MUCRoomTask(LocalMUCRoom room) {
this.room = room;
}
public LocalMUCRoom getRoom() {
return room;
}
public boolean isOriginator() {
return originator;
}
public void setOriginator(boolean originator) {
this.originator = originator;
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeBoolean(out, originator);
ExternalizableUtil.getInstance().writeSafeUTF(out, room.getName());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
originator = ExternalizableUtil.getInstance().readBoolean(in);
String roomName = ExternalizableUtil.getInstance().readSafeUTF(in);
room = (LocalMUCRoom) XMPPServer.getInstance().getMultiUserChatServer().getChatRoom(roomName);
if (room == null) {
throw new IllegalArgumentException("Room not found: " + roomName);
}
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.NodeID;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that will remove a room occupant from the list of occupants in the room.
*
* @author Gaston Dombiak
*/
public class OccupantAddedEvent extends MUCRoomTask {
private Presence presence;
private int role;
private int affiliation;
private boolean voiceOnly;
private JID roleAddress;
private JID userAddress;
private NodeID nodeID;
private boolean sendPresence;
public OccupantAddedEvent() {
}
public OccupantAddedEvent(LocalMUCRoom room, MUCRole occupant) {
super(room);
presence = occupant.getPresence();
role = occupant.getRole().ordinal();
affiliation = occupant.getAffiliation().ordinal();
voiceOnly = occupant.isVoiceOnly();
roleAddress = occupant.getRoleAddress();
userAddress = occupant.getUserAddress();
nodeID = XMPPServer.getInstance().getNodeID();
}
public Presence getPresence() {
return presence;
}
public String getNickname() {
return presence.getTo().getResource();
}
public MUCRole.Role getRole() {
return MUCRole.Role.values()[role];
}
public MUCRole.Affiliation getAffiliation() {
return MUCRole.Affiliation.values()[affiliation];
}
public boolean isVoiceOnly() {
return voiceOnly;
}
public JID getRoleAddress() {
return roleAddress;
}
public JID getUserAddress() {
return userAddress;
}
public NodeID getNodeID() {
return nodeID;
}
/**
* Sets if the room should broadcast presence of the new occupant to occupants
* hosted by this cluster node.
*
* @param sendPresence true if the room should broadcast presence of the new occupant to occupants
* hosted by this cluster node.
*/
public void setSendPresence(boolean sendPresence) {
this.sendPresence = sendPresence;
}
/**
* Returns true if the room should broadcast presence of the new occupant to occupants
* hosted by this cluster node.
*
* @return true if the room should broadcast presence of the new occupant to occupants
* hosted by this cluster node.
*/
public boolean isSendPresence() {
return sendPresence;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().occupantAdded(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
ExternalizableUtil.getInstance().writeInt(out, role);
ExternalizableUtil.getInstance().writeInt(out, affiliation);
ExternalizableUtil.getInstance().writeBoolean(out, voiceOnly);
ExternalizableUtil.getInstance().writeSerializable(out, roleAddress);
ExternalizableUtil.getInstance().writeSerializable(out, userAddress);
ExternalizableUtil.getInstance().writeByteArray(out, nodeID.toByteArray());
ExternalizableUtil.getInstance().writeBoolean(out, sendPresence);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
presence = new Presence(packetElement, true);
role = ExternalizableUtil.getInstance().readInt(in);
affiliation = ExternalizableUtil.getInstance().readInt(in);
voiceOnly = ExternalizableUtil.getInstance().readBoolean(in);
roleAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
userAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
nodeID = NodeID.getInstance(ExternalizableUtil.getInstance().readByteArray(in));
sendPresence = ExternalizableUtil.getInstance().readBoolean(in);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that removes a room occupant from the list of occupants in the room. The
* occupant to remove is actualy a {@link org.jivesoftware.openfire.muc.spi.RemoteMUCRole}.
*
* @author Gaston Dombiak
*/
public class OccupantLeftEvent extends MUCRoomTask {
private MUCRole role;
private String nickname;
public OccupantLeftEvent() {
}
public OccupantLeftEvent(LocalMUCRoom room, MUCRole role) {
super(room);
this.role = role;
this.nickname = role.getNickname();
}
public MUCRole getRole() {
if (role == null) {
try {
role = getRoom().getOccupant(nickname);
} catch (UserNotFoundException e) {
// Ignore
}
}
return role;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().leaveRoom(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSafeUTF(out, nickname);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.openfire.muc.spi.MultiUserChatServerImpl;
import org.jivesoftware.util.cache.ClusterTask;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that adds a new local room to the cluster node. When a room is created in a
* cluster node the rest of the cluster nodes will need to get the new room added
* in their list of existing rooms.
*
* @author Gaston Dombiak
*/
public class RoomAvailableEvent implements ClusterTask {
private LocalMUCRoom room;
public RoomAvailableEvent() {
}
public RoomAvailableEvent(LocalMUCRoom room) {
this.room = room;
}
public Object getResult() {
return null;
}
public void run() {
MultiUserChatServerImpl mucServer = (MultiUserChatServerImpl) XMPPServer.getInstance().getMultiUserChatServer();
mucServer.chatRoomAdded(room);
}
public void writeExternal(ObjectOutput out) throws IOException {
room.writeExternal(out);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
room = new LocalMUCRoom();
room.readExternal(in);
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Representation of a room configuration and its occupants. This information is requested
* by a cluster node when joining the cluster (requested to the senior member) and also from
* each cluster node to the new node joining the cluster. Each cluster node (existing and
* new one) has to merge its local rooms with the new ones.
*
* @author Gaston Dombiak
*/
public class RoomInfo implements Externalizable {
private LocalMUCRoom room;
private List<OccupantAddedEvent> occupants = new ArrayList<OccupantAddedEvent>();
/**
* Do not use this constructor. Needed for Externalizable interface.
*/
public RoomInfo() {
}
public RoomInfo(LocalMUCRoom room, Collection<MUCRole> occupants) {
this.room = room;
for (MUCRole occupant : occupants) {
this.occupants.add(new OccupantAddedEvent(room, occupant));
}
}
public LocalMUCRoom getRoom() {
return room;
}
public List<OccupantAddedEvent> getOccupants() {
return occupants;
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeSerializable(out, room);
ExternalizableUtil.getInstance().writeExternalizableCollection(out, occupants);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
room = (LocalMUCRoom) ExternalizableUtil.getInstance().readSerializable(in);
ExternalizableUtil.getInstance().readExternalizableCollection(in, occupants, getClass().getClassLoader());
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.spi.MultiUserChatServerImpl;
import org.jivesoftware.util.cache.ClusterTask;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that will remove a local room from the cluster node. When a room is destroyed
* in a cluster node the rest of the cluster nodes will need to destroy their copy
* and send notifications to the room occupants hosted in the local cluster node.
*
* @author Gaston Dombiak
*/
public class RoomRemovedEvent implements ClusterTask {
private String roomName;
public RoomRemovedEvent() {
}
public RoomRemovedEvent(String roomName) {
this.roomName = roomName;
}
public Object getResult() {
return null;
}
public void run() {
MultiUserChatServerImpl mucServer = (MultiUserChatServerImpl) XMPPServer.getInstance().getMultiUserChatServer();
mucServer.chatRoomRemoved(roomName);
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeSafeUTF(out, roomName);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
roomName = ExternalizableUtil.getInstance().readSafeUTF(in);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that updates the configuration of a local room. When a room gets updated in a
* cluster node the rest of the cluster nodes will need to update their copy of the
* local room.
*
* @author Gaston Dombiak
*/
public class RoomUpdatedEvent extends MUCRoomTask {
private LocalMUCRoom room;
public RoomUpdatedEvent() {
}
public RoomUpdatedEvent(LocalMUCRoom room) {
super(room);
this.room = room;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().updateConfiguration(room);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
room.writeExternal(out);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
room = new LocalMUCRoom();
room.readExternal(in);
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ClusterTask;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
/**
* Task to be requested by a node that joins a cluster and be executed in the senior cluster member to get
* the rooms with occupants. The list of rooms with occupants is returned to the new cluster node so that
* the new cluster node can be updated and have the same information shared by the cluster.<p>
*
* Moreover, each existing cluster node will also need to learn the rooms with occupants that exist in
* the new cluster node and replicate them. This work is accomplished using {@link GetNewMemberRoomsRequest}.
*
* @author Gaston Dombiak
*/
public class SeniorMemberRoomsRequest implements ClusterTask {
private List<RoomInfo> rooms;
public SeniorMemberRoomsRequest() {
}
public Object getResult() {
return rooms;
}
public void run() {
rooms = new ArrayList<RoomInfo>();
// Get rooms that have occupants and include them in the reply
MultiUserChatServer mucServer = XMPPServer.getInstance().getMultiUserChatServer();
for (MUCRoom room : mucServer.getChatRooms()) {
LocalMUCRoom localRoom = (LocalMUCRoom) room;
if (!room.getOccupants().isEmpty()) {
rooms.add(new RoomInfo(localRoom, localRoom.getOccupants()));
}
}
}
public void writeExternal(ObjectOutput out) throws IOException {
// Do nothing
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// Do nothing
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.HistoryStrategy;
import org.jivesoftware.openfire.muc.spi.MultiUserChatServerImpl;
import org.jivesoftware.util.cache.ClusterTask;
import org.jivesoftware.util.cache.ExternalizableUtil;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Cluster task that will update the history strategy used by the MultiUserChatServer
* service. It is currently not possible to edit the history strategy of a given room
* but only of the service. Therefore, this task will only update the service's strategy.
*
* @author Gaston Dombiak
*/
public class UpdateHistoryStrategy implements ClusterTask {
private int type;
private int maxNumber;
public UpdateHistoryStrategy() {
}
public UpdateHistoryStrategy(HistoryStrategy historyStrategy) {
type = historyStrategy.getType().ordinal();
maxNumber = historyStrategy.getMaxNumber();
}
public Object getResult() {
return null;
}
public void run() {
MultiUserChatServerImpl mucServer = (MultiUserChatServerImpl) XMPPServer.getInstance().getMultiUserChatServer();
HistoryStrategy strategy = mucServer.getHistoryStrategy();
strategy.setType(HistoryStrategy.Type.values()[type]);
strategy.setMaxNumber(maxNumber);
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeInt(out, type);
ExternalizableUtil.getInstance().writeInt(out, maxNumber);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
type = ExternalizableUtil.getInstance().readInt(in);
maxNumber = ExternalizableUtil.getInstance().readInt(in);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.Presence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that updates all information regarding a room occupant. Whenever a room
* occupant gets his affiliation, role, nickname or presence updated the other
* cluster nodes will need to reflect these changes.
*
* @author Gaston Dombiak
*/
public class UpdateOccupant extends MUCRoomTask {
private Presence presence;
private String nickname;
private int role;
private int affiliation;
public UpdateOccupant() {
}
public UpdateOccupant(LocalMUCRoom room, MUCRole role) {
super(room);
this.presence = role.getPresence();
this.nickname = role.getNickname();
this.role = role.getRole().ordinal();
this.affiliation = role.getAffiliation().ordinal();
}
public Presence getPresence() {
return presence;
}
public String getNickname() {
return nickname;
}
public MUCRole.Role getRole() {
return MUCRole.Role.values()[role];
}
public MUCRole.Affiliation getAffiliation() {
return MUCRole.Affiliation.values()[affiliation];
}
public Object getResult() {
return null;
}
public void run() {
getRoom().occupantUpdated(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
ExternalizableUtil.getInstance().writeSafeUTF(out, nickname);
ExternalizableUtil.getInstance().writeInt(out, role);
ExternalizableUtil.getInstance().writeInt(out, affiliation);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
presence = new Presence(packetElement, true);
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
role = ExternalizableUtil.getInstance().readInt(in);
affiliation = ExternalizableUtil.getInstance().readInt(in);
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.cluster;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.Presence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Task that updates the presence of an occupant in a room. Each time an occupant
* changes his presence in the room the other cluster nodes will need to get the
* presence updated too for the occupant.
*
* @author Gaston Dombiak
*/
public class UpdatePresence extends MUCRoomTask {
private Presence presence;
private String nickname;
public UpdatePresence() {
}
public UpdatePresence(LocalMUCRoom room, Presence presence, String nickname) {
super(room);
this.presence = presence;
this.nickname = nickname;
}
public Presence getPresence() {
return presence;
}
public String getNickname() {
return nickname;
}
public Object getResult() {
return null;
}
public void run() {
getRoom().presenceUpdated(this);
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
ExternalizableUtil.getInstance().writeSafeUTF(out, nickname);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
Element packetElement = (Element) ExternalizableUtil.getInstance().readSerializable(in);
presence = new Presence(packetElement, true);
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
}
}
/**
* $RCSfile: LocalMUCRole.java,v $
* $Revision: 3168 $
* $Date: 2005-12-07 13:55:47 -0300 (Wed, 07 Dec 2005) $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.spi;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.openfire.PacketRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.NodeID;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.muc.NotAllowedException;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.ElementUtil;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
/**
* Implementation of a local room occupant.
*
* @author Gaston Dombiak
*/
public class LocalMUCRole implements MUCRole {
/**
* The room this role is valid in.
*/
private LocalMUCRoom room;
/**
* The user of the role.
*/
private LocalMUCUser user;
/**
* The user's nickname in the room.
*/
private String nick;
/**
* The user's presence in the room.
*/
private Presence presence;
/**
* The chatserver that hosts this role.
*/
private MultiUserChatServer server;
/**
* The role.
*/
private MUCRole.Role role;
/**
* The affiliation.
*/
private MUCRole.Affiliation affiliation;
/**
* Flag that indicates if the room occupant is in the room only to send messages or also
* to receive room messages. True means that the room occupant is deaf.
*/
private boolean voiceOnly = false;
/**
* The router used to send packets from this role.
*/
private PacketRouter router;
/**
* The address of the person masquerading in this role.
*/
private JID rJID;
/**
* A fragment containing the x-extension for non-anonymous rooms.
*/
private Element extendedInformation;
/**
* Cache session of local user that is a room occupant. If the room occupant is not a local
* user then nothing will be cached and packets will be sent through the PacketRouter.
*/
private ClientSession session;
/**
* Create a new role.
*
* @param chatserver the server hosting the role.
* @param chatroom the room the role is valid in.
* @param nickname the nickname of the user in the role.
* @param role the role of the user in the room.
* @param affiliation the affiliation of the user in the room.
* @param chatuser the user on the chat server.
* @param presence the presence sent by the user to join the room.
* @param packetRouter the packet router for sending messages from this role.
*/
public LocalMUCRole(MultiUserChatServer chatserver, LocalMUCRoom chatroom, String nickname,
MUCRole.Role role, MUCRole.Affiliation affiliation, LocalMUCUser chatuser, Presence presence,
PacketRouter packetRouter)
{
this.room = chatroom;
this.nick = nickname;
this.user = chatuser;
this.server = chatserver;
this.router = packetRouter;
this.role = role;
this.affiliation = affiliation;
// Cache the user's session (will only work for local users)
this.session = XMPPServer.getInstance().getSessionManager().getSession(presence.getFrom());
extendedInformation =
DocumentHelper.createElement(QName.get("x", "http://jabber.org/protocol/muc#user"));
calculateExtendedInformation();
rJID = new JID(room.getName(), server.getServiceDomain(), nick);
setPresence(presence);
// Check if new occupant wants to be a deaf occupant
Element element = presence.getElement()
.element(QName.get("x", "http://jivesoftware.org/protocol/muc"));
if (element != null) {
voiceOnly = element.element("deaf-occupant") != null;
}
// Add the new role to the list of roles
user.addRole(room.getName(), this);
}
public Presence getPresence() {
return presence;
}
public void setPresence(Presence newPresence) {
// Try to remove the element whose namespace is "http://jabber.org/protocol/muc" since we
// don't need to include that element in future presence broadcasts
Element element = newPresence.getElement().element(QName.get("x", "http://jabber.org/protocol/muc"));
if (element != null) {
newPresence.getElement().remove(element);
}
this.presence = newPresence;
this.presence.setFrom(getRoleAddress());
if (extendedInformation != null) {
extendedInformation.setParent(null);
presence.getElement().add(extendedInformation);
}
}
public void setRole(MUCRole.Role newRole) throws NotAllowedException {
// Don't allow to change the role to an owner or admin unless the new role is moderator
if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin == affiliation) {
if (MUCRole.Role.moderator != newRole) {
throw new NotAllowedException();
}
}
// A moderator cannot be kicked from a room
if (MUCRole.Role.moderator == role && MUCRole.Role.none == newRole) {
throw new NotAllowedException();
}
// TODO A moderator MUST NOT be able to revoke voice from a user whose affiliation is at or
// TODO above the moderator's level.
role = newRole;
if (MUCRole.Role.none == role) {
presence.setType(Presence.Type.unavailable);
presence.setStatus(null);
}
calculateExtendedInformation();
}
public MUCRole.Role getRole() {
return role;
}
public void setAffiliation(MUCRole.Affiliation newAffiliation) throws NotAllowedException {
// Don't allow to ban an owner or an admin
if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin== affiliation) {
if (MUCRole.Affiliation.outcast == newAffiliation) {
throw new NotAllowedException();
}
}
affiliation = newAffiliation;
// TODO The fragment is being calculated twice (1. setting the role & 2. setting the aff)
calculateExtendedInformation();
}
public MUCRole.Affiliation getAffiliation() {
return affiliation;
}
public String getNickname() {
return nick;
}
public void changeNickname(String nickname) {
this.nick = nickname;
setRoleAddress(new JID(room.getName(), server.getServiceDomain(), nick));
}
public void destroy() {
// Notify the user that he/she is no longer in the room
user.removeRole(room.getName());
}
public MUCRoom getChatRoom() {
return room;
}
public JID getRoleAddress() {
return rJID;
}
public JID getUserAddress() {
return user.getAddress();
}
public boolean isLocal() {
return true;
}
public NodeID getNodeID() {
return XMPPServer.getInstance().getNodeID();
}
private void setRoleAddress(JID jid) {
rJID = jid;
// Set the new sender of the user presence in the room
presence.setFrom(jid);
}
public boolean isVoiceOnly() {
return voiceOnly;
}
public void send(Packet packet) {
if (packet == null) {
return;
}
packet.setTo(user.getAddress());
if (session != null && session.getStatus() == Session.STATUS_AUTHENTICATED) {
// Send the packet directly to the local user session
session.process(packet);
}
else {
router.route(packet);
}
}
/**
* Calculates and sets the extended presence information to add to the presence.
* The information to add contains the user's jid, affiliation and role.
*/
private void calculateExtendedInformation() {
ElementUtil.setProperty(extendedInformation, "x.item:jid", user.getAddress().toString());
ElementUtil.setProperty(extendedInformation, "x.item:affiliation", affiliation.toString());
ElementUtil.setProperty(extendedInformation, "x.item:role", role.toString());
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.spi;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.NodeID;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatServer;
import org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Representation of a room occupant of a local room that is being hosted by
* another cluster node. An instance of this class will exist for each room
* occupant that is hosted by another cluster node. Local rooms keep track of
* local and remote occupants in a transparent way.
*
* @author Gaston Dombiak
*/
public class RemoteMUCRole implements MUCRole, Externalizable {
private MultiUserChatServer server;
private Presence presence;
private Role role;
private Affiliation affiliation;
private String nickname;
private boolean voiceOnly;
private JID roleAddress;
private JID userAddress;
private MUCRoom room;
private NodeID nodeID;
/**
* Do not use this constructor. Only used for Externalizable.
*/
public RemoteMUCRole() {
}
public RemoteMUCRole(MultiUserChatServer server, OccupantAddedEvent event) {
this.server = server;
presence = event.getPresence();
role = event.getRole();
affiliation = event.getAffiliation();
nickname = event.getNickname();
voiceOnly = event.isVoiceOnly();
roleAddress = event.getRoleAddress();
userAddress = event.getUserAddress();
room = event.getRoom();
this.nodeID = event.getNodeID();
}
public Presence getPresence() {
return presence;
}
public void setPresence(Presence presence) {
this.presence = presence;
}
public void setRole(Role newRole) {
this.role = newRole;
}
public Role getRole() {
return role;
}
public void setAffiliation(Affiliation newAffiliation) {
this.affiliation = newAffiliation;
}
public Affiliation getAffiliation() {
return affiliation;
}
public void changeNickname(String nickname) {
this.nickname = nickname;
setRoleAddress(new JID(room.getName(), server.getServiceDomain(), nickname, true));
}
private void setRoleAddress(JID jid) {
roleAddress = jid;
// Set the new sender of the user presence in the room
presence.setFrom(jid);
}
public String getNickname() {
return nickname;
}
public void destroy() {
// Do nothing
}
public boolean isVoiceOnly() {
return voiceOnly;
}
public MUCRoom getChatRoom() {
return room;
}
public JID getRoleAddress() {
return roleAddress;
}
public JID getUserAddress() {
return userAddress;
}
public boolean isLocal() {
return false;
}
public NodeID getNodeID() {
return nodeID;
}
public void send(Packet packet) {
XMPPServer.getInstance().getPacketRouter().route(packet);
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeSerializable(out, (DefaultElement) presence.getElement());
ExternalizableUtil.getInstance().writeInt(out, role.ordinal());
ExternalizableUtil.getInstance().writeInt(out, affiliation.ordinal());
ExternalizableUtil.getInstance().writeSafeUTF(out, nickname);
ExternalizableUtil.getInstance().writeBoolean(out, voiceOnly);
ExternalizableUtil.getInstance().writeSerializable(out, roleAddress);
ExternalizableUtil.getInstance().writeSerializable(out, userAddress);
ExternalizableUtil.getInstance().writeByteArray(out, nodeID.toByteArray());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
presence = new Presence((Element)ExternalizableUtil.getInstance().readSerializable(in), true);
role = Role.values()[ExternalizableUtil.getInstance().readInt(in)];
affiliation = Affiliation.values()[ExternalizableUtil.getInstance().readInt(in)];
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
voiceOnly = ExternalizableUtil.getInstance().readBoolean(in);
roleAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
userAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
nodeID = NodeID.getInstance(ExternalizableUtil.getInstance().readByteArray(in));
}
}
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.muc.spi;
import org.jivesoftware.openfire.PacketException;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCUser;
import org.xmpp.packet.*;
/**
* User hosted by another cluster node that is presente in a local room. Remote users are
* only created when processing unavailable presences sent when the node hosting the actual
* user went down. Each cluster node remaining in the cluster will create an unavailable
* presence for each user hosted in the cluster node that went down as a way to indicate
* the remaining room occupants that the user is offline.
*
* @author Gaston Dombiak
*/
public class RemoteMUCUser implements MUCUser {
/**
* JID of the user hosted by other cluster node.
*/
private JID realjid;
/**
* Local room that keep a reference to the RemoteMUCRole for this user.
*/
private LocalMUCRoom room;
public RemoteMUCUser(JID realjid, LocalMUCRoom room) {
this.realjid = realjid;
this.room = room;
}
public JID getAddress() {
return realjid;
}
public void process(Packet packet) throws UnauthorizedException, PacketException {
if (packet instanceof IQ) {
throw new UnsupportedOperationException("Cannot process IQ packets of remote users: " + packet);
}
else if (packet instanceof Message) {
throw new UnsupportedOperationException("Cannot process Message packets of remote users: " + packet);
}
else if (packet instanceof Presence) {
process((Presence)packet);
}
}
private void process(Presence presence) {
if (presence.getType() == Presence.Type.unavailable) {
MUCRole mucRole = room.getOccupantByFullJID(realjid);
if (mucRole != null) {
room.leaveRoom(mucRole);
}
}
else {
throw new UnsupportedOperationException("Cannot process Presence packets of remote users: " + presence);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment