001: /**
002: * $RCSfile: $
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.muc.spi;
011:
012: import org.dom4j.Element;
013: import org.dom4j.tree.DefaultElement;
014: import org.jivesoftware.openfire.XMPPServer;
015: import org.jivesoftware.openfire.cluster.NodeID;
016: import org.jivesoftware.openfire.muc.MUCRole;
017: import org.jivesoftware.openfire.muc.MUCRoom;
018: import org.jivesoftware.openfire.muc.MultiUserChatServer;
019: import org.jivesoftware.openfire.muc.cluster.OccupantAddedEvent;
020: import org.jivesoftware.util.cache.ExternalizableUtil;
021: import org.xmpp.packet.JID;
022: import org.xmpp.packet.Packet;
023: import org.xmpp.packet.Presence;
024:
025: import java.io.Externalizable;
026: import java.io.IOException;
027: import java.io.ObjectInput;
028: import java.io.ObjectOutput;
029:
030: /**
031: * Representation of a room occupant of a local room that is being hosted by
032: * another cluster node. An instance of this class will exist for each room
033: * occupant that is hosted by another cluster node. Local rooms keep track of
034: * local and remote occupants in a transparent way.
035: *
036: * @author Gaston Dombiak
037: */
038: public class RemoteMUCRole implements MUCRole, Externalizable {
039: private MultiUserChatServer server;
040: private Presence presence;
041: private Role role;
042: private Affiliation affiliation;
043: private String nickname;
044: private boolean voiceOnly;
045: private JID roleAddress;
046: private JID userAddress;
047: private MUCRoom room;
048: private NodeID nodeID;
049:
050: /**
051: * Do not use this constructor. Only used for Externalizable.
052: */
053: public RemoteMUCRole() {
054: }
055:
056: public RemoteMUCRole(MultiUserChatServer server,
057: OccupantAddedEvent event) {
058: this .server = server;
059: presence = event.getPresence();
060: role = event.getRole();
061: affiliation = event.getAffiliation();
062: nickname = event.getNickname();
063: voiceOnly = event.isVoiceOnly();
064: roleAddress = event.getRoleAddress();
065: userAddress = event.getUserAddress();
066: room = event.getRoom();
067: this .nodeID = event.getNodeID();
068: }
069:
070: public Presence getPresence() {
071: return presence;
072: }
073:
074: public void setPresence(Presence presence) {
075: this .presence = presence;
076: }
077:
078: public void setRole(Role newRole) {
079: this .role = newRole;
080: }
081:
082: public Role getRole() {
083: return role;
084: }
085:
086: public void setAffiliation(Affiliation newAffiliation) {
087: this .affiliation = newAffiliation;
088: }
089:
090: public Affiliation getAffiliation() {
091: return affiliation;
092: }
093:
094: public void changeNickname(String nickname) {
095: this .nickname = nickname;
096: setRoleAddress(new JID(room.getName(), server
097: .getServiceDomain(), nickname, true));
098: }
099:
100: private void setRoleAddress(JID jid) {
101: roleAddress = jid;
102: // Set the new sender of the user presence in the room
103: presence.setFrom(jid);
104: }
105:
106: public String getNickname() {
107: return nickname;
108: }
109:
110: public void destroy() {
111: // Do nothing
112: }
113:
114: public boolean isVoiceOnly() {
115: return voiceOnly;
116: }
117:
118: public MUCRoom getChatRoom() {
119: return room;
120: }
121:
122: public JID getRoleAddress() {
123: return roleAddress;
124: }
125:
126: public JID getUserAddress() {
127: return userAddress;
128: }
129:
130: public boolean isLocal() {
131: return false;
132: }
133:
134: public NodeID getNodeID() {
135: return nodeID;
136: }
137:
138: public void send(Packet packet) {
139: XMPPServer.getInstance().getRoutingTable().routePacket(
140: userAddress, packet, false);
141: }
142:
143: public void writeExternal(ObjectOutput out) throws IOException {
144: ExternalizableUtil.getInstance().writeSerializable(out,
145: (DefaultElement) presence.getElement());
146: ExternalizableUtil.getInstance().writeInt(out, role.ordinal());
147: ExternalizableUtil.getInstance().writeInt(out,
148: affiliation.ordinal());
149: ExternalizableUtil.getInstance().writeSafeUTF(out, nickname);
150: ExternalizableUtil.getInstance().writeBoolean(out, voiceOnly);
151: ExternalizableUtil.getInstance().writeSerializable(out,
152: roleAddress);
153: ExternalizableUtil.getInstance().writeSerializable(out,
154: userAddress);
155: ExternalizableUtil.getInstance().writeByteArray(out,
156: nodeID.toByteArray());
157: }
158:
159: public void readExternal(ObjectInput in) throws IOException,
160: ClassNotFoundException {
161: presence = new Presence((Element) ExternalizableUtil
162: .getInstance().readSerializable(in), true);
163: role = Role.values()[ExternalizableUtil.getInstance().readInt(
164: in)];
165: affiliation = Affiliation.values()[ExternalizableUtil
166: .getInstance().readInt(in)];
167: nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
168: voiceOnly = ExternalizableUtil.getInstance().readBoolean(in);
169: roleAddress = (JID) ExternalizableUtil.getInstance()
170: .readSerializable(in);
171: userAddress = (JID) ExternalizableUtil.getInstance()
172: .readSerializable(in);
173: nodeID = NodeID.getInstance(ExternalizableUtil.getInstance()
174: .readByteArray(in));
175: }
176: }
|