RemoteMUCRole.java 6.06 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20 21 22 23 24 25 26 27 28
 */

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;
29
import org.jivesoftware.openfire.muc.MultiUserChatService;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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 {
50
    private String serviceDomain;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    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() {
    }

67
    public RemoteMUCRole(MultiUserChatService server, OccupantAddedEvent event) {
68
        this.serviceDomain = server.getServiceDomain();
69 70 71 72 73 74 75 76 77 78 79
        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();
    }

80
    @Override
81 82 83 84
    public Presence getPresence() {
        return presence;
    }

85
    @Override
86 87 88 89
    public void setPresence(Presence presence) {
        this.presence = presence;
    }

90
    @Override
91 92 93 94
    public void setRole(Role newRole) {
        this.role = newRole;
    }

95
    @Override
96 97 98 99
    public Role getRole() {
        return role;
    }

100
    @Override
101 102 103 104
    public void setAffiliation(Affiliation newAffiliation) {
        this.affiliation = newAffiliation;
    }

105
    @Override
106 107 108 109
    public Affiliation getAffiliation() {
        return affiliation;
    }

110
    @Override
111 112
    public void changeNickname(String nickname) {
        this.nickname = nickname;
113
        setRoleAddress(new JID(room.getName(), serviceDomain, nickname, true));
114 115 116 117 118 119 120 121
    }

    private void setRoleAddress(JID jid) {
        roleAddress = jid;
        // Set the new sender of the user presence in the room
        presence.setFrom(jid);
    }

122
    @Override
123 124 125 126
    public String getNickname() {
        return nickname;
    }

127
    @Override
128 129 130 131
    public void destroy() {
        // Do nothing
    }

132
    @Override
133 134 135 136
    public boolean isVoiceOnly() {
        return voiceOnly;
    }

137
    @Override
138 139 140 141
    public MUCRoom getChatRoom() {
        return room;
    }

142
    @Override
143 144 145 146
    public JID getRoleAddress() {
        return roleAddress;
    }

147
    @Override
148 149 150 151
    public JID getUserAddress() {
        return userAddress;
    }

152
    @Override
153 154 155 156
    public boolean isLocal() {
        return false;
    }

157
    @Override
158 159 160 161
    public NodeID getNodeID() {
        return nodeID;
    }

162
    @Override
163
    public void send(Packet packet) {
164
        XMPPServer.getInstance().getRoutingTable().routePacket(userAddress, packet, false);
165 166
    }

167
    @Override
168
    public void writeExternal(ObjectOutput out) throws IOException {
169
        ExternalizableUtil.getInstance().writeSafeUTF(out, serviceDomain);
170 171 172 173 174 175 176 177 178 179
        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());
    }

180
    @Override
181
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
182
        serviceDomain = ExternalizableUtil.getInstance().readSafeUTF(in);
183 184 185 186 187 188 189 190 191 192
        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));
    }
}