Commit 93fb6849 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Fixed update of role/affiliation when user is hosted in another cluster node.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8977 b35dd754-fafc-0310-a699-88a17e54d16e
parent ec7040df
/**
* $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.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.NotAllowedException;
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 requests the cluster node hosting a room occupant to change his
* role and/or affiliation. If the occupant was not found or the change is
* not allowed then a <tt>null</tt> value is returned. Otherwise the DOM
* object representing the new presence of the room occupant is returned.
*
* @author Gaston Dombiak
*/
public class UpdateOccupantRequest extends MUCRoomTask {
private Element answer;
private String nickname;
private int role;
private int affiliation;
public UpdateOccupantRequest() {
}
public UpdateOccupantRequest(LocalMUCRoom room, String nickname, MUCRole.Affiliation newAffiliation,
MUCRole.Role newRole) {
super(room);
this.nickname = nickname;
this.role = newRole.ordinal();
this.affiliation = newAffiliation.ordinal();
}
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 answer;
}
public void run() {
try {
Presence presence = getRoom().updateOccupant(this);
if (presence != null) {
answer = presence.getElement();
}
} catch (NotAllowedException e) {
// Do nothing. A null return value means that the operation failed
}
}
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
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);
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
role = ExternalizableUtil.getInstance().readInt(in);
affiliation = ExternalizableUtil.getInstance().readInt(in);
}
}
\ No newline at end of file
...@@ -1099,12 +1099,28 @@ public class LocalMUCRoom implements MUCRoom { ...@@ -1099,12 +1099,28 @@ public class LocalMUCRoom implements MUCRoom {
// Collect all the updated presences of these roles // Collect all the updated presences of these roles
for (MUCRole role : roles) { for (MUCRole role : roles) {
// Update the presence with the new affiliation and role // Update the presence with the new affiliation and role
role.setAffiliation(newAffiliation); if (role.isLocal()) {
role.setRole(newRole); role.setAffiliation(newAffiliation);
// Notify the othe cluster nodes to update the occupant role.setRole(newRole);
CacheFactory.doClusterTask(new UpdateOccupant(this, role)); // Notify the othe cluster nodes to update the occupant
// Prepare a new presence to be sent to all the room occupants CacheFactory.doClusterTask(new UpdateOccupant(this, role));
presences.add(role.getPresence().createCopy()); // Prepare a new presence to be sent to all the room occupants
presences.add(role.getPresence().createCopy());
}
else {
// Ask the cluster node hosting the occupant to make the changes. Note that if the change
// is not allowed a NotAllowedException will be thrown
Element element = (Element) CacheFactory.doSynchronousClusterTask(
new UpdateOccupantRequest(this, role.getNickname(), newAffiliation, newRole),
role.getNodeID().toByteArray());
if (element != null) {
// Prepare a new presence to be sent to all the room occupants
presences.add(new Presence(element, true));
}
else {
throw new NotAllowedException();
}
}
} }
// Answer all the updated presences // Answer all the updated presences
return presences; return presences;
...@@ -1467,17 +1483,43 @@ public class LocalMUCRoom implements MUCRoom { ...@@ -1467,17 +1483,43 @@ public class LocalMUCRoom implements MUCRoom {
} }
public void occupantUpdated(UpdateOccupant update) { public void occupantUpdated(UpdateOccupant update) {
RemoteMUCRole occupantRole = (RemoteMUCRole) occupants.get(update.getNickname().toLowerCase()); MUCRole occupantRole = occupants.get(update.getNickname().toLowerCase());
if (occupantRole != null) { if (occupantRole != null) {
occupantRole.setPresence(update.getPresence()); if (!occupantRole.isLocal()) {
occupantRole.setRole(update.getRole()); occupantRole.setPresence(update.getPresence());
occupantRole.setAffiliation(update.getAffiliation()); try {
occupantRole.setRole(update.getRole());
occupantRole.setAffiliation(update.getAffiliation());
} catch (NotAllowedException e) {
// Ignore. Should never happen with remote roles
}
}
else {
Log.error("Tried to update local occupant with info of local occupant?. Occupant nickname: " +
update.getNickname());
}
} }
else { else {
Log.debug("Failed to update information of room occupant. Occupant nickname: " + update.getNickname()); Log.debug("Failed to update information of room occupant. Occupant nickname: " + update.getNickname());
} }
} }
public Presence updateOccupant(UpdateOccupantRequest updateRequest) throws NotAllowedException {
MUCRole occupantRole = occupants.get(updateRequest.getNickname().toLowerCase());
if (occupantRole != null) {
occupantRole.setAffiliation(updateRequest.getAffiliation());
occupantRole.setRole(updateRequest.getRole());
// Notify the othe cluster nodes to update the occupant
CacheFactory.doClusterTask(new UpdateOccupant(this, occupantRole));
return occupantRole.getPresence();
}
else {
Log.debug("Failed to update information of local room occupant. Occupant nickname: " +
updateRequest.getNickname());
}
return null;
}
public void nicknameChanged(MUCRole occupantRole, Presence newPresence, String oldNick, String newNick) { public void nicknameChanged(MUCRole occupantRole, Presence newPresence, String oldNick, String newNick) {
// Ask other cluster nodes to update the nickname of the occupant // Ask other cluster nodes to update the nickname of the occupant
ChangeNickname request = new ChangeNickname(this, oldNick, newNick, newPresence.createCopy()); ChangeNickname request = new ChangeNickname(this, oldNick, newNick, newPresence.createCopy());
......
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