1 /*
2  * Copyright (C) 2005-2008 Jive Software. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.jivesoftware.openfire.handler;
18 
19 import org.jivesoftware.openfire.XMPPServer;
20 import org.jivesoftware.util.cache.ExternalizableUtil;
21 import org.xmpp.packet.JID;
22 
23 import java.io.Externalizable;
24 import java.io.IOException;
25 import java.io.ObjectInput;
26 import java.io.ObjectOutput;
27 import java.util.HashSet;
28 import java.util.Set;
29 
30 /**
31  * Represents a directed presence sent from a session hosted in a cluster node
32  * to another entity (e.g. user or MUC service) hosted in some other cluster
33  * node.<p>
34  *
35  * This information needs to be shared by all cluster nodes so that if a
36  * cluster node goes down then directed presences can be correctly cleaned
37  * up.<p>
38  *
39  * Note that an instance of this class will be created and kept in the clustered
40  * cache only when entities hosted by different cluster nodes are involved.
41  *
42  * @author Gaston Dombiak
43  */
44 public class DirectedPresence implements Externalizable {
45     /**
46      * ID of the node that received the request to send a directed presence. This is the
47      * node ID that hosts the sender.
48      */
49     private byte[] nodeID;
50     /**
51      * Full JID of the entity that received the directed presence.
52      * E.g.: paul@js.com/Spark or conference.js.com
53      */
54     private JID handler;
55     /**
56      * List of JIDs with the TO value of the directed presences.
57      * E.g.: paul@js.com or room1@conference.js.com
58      */
59     private Set<String> receivers = new HashSet<>();
60 
DirectedPresence()61     public DirectedPresence() {
62     }
63 
DirectedPresence(JID handlerJID)64     public DirectedPresence(JID handlerJID) {
65         this.handler = handlerJID;
66         this.nodeID = XMPPServer.getInstance().getNodeID().toByteArray();
67     }
68 
getNodeID()69     public byte[] getNodeID() {
70         return nodeID;
71     }
72 
getHandler()73     public JID getHandler() {
74         return handler;
75     }
76 
getReceivers()77     public Set<String> getReceivers() {
78         return receivers;
79     }
80 
addReceiver(String receiver)81     public void addReceiver(String receiver) {
82         receivers.add(receiver);
83     }
84 
removeReceiver(String receiver)85     public void removeReceiver(String receiver) {
86         receivers.remove(receiver);
87     }
88 
isEmpty()89     public boolean isEmpty() {
90         return receivers.isEmpty();
91     }
92 
93     @Override
writeExternal(ObjectOutput out)94     public void writeExternal(ObjectOutput out) throws IOException {
95         ExternalizableUtil.getInstance().writeByteArray(out, nodeID);
96         ExternalizableUtil.getInstance().writeSerializable(out, handler);
97         ExternalizableUtil.getInstance().writeStrings(out, receivers);
98     }
99 
100     @Override
readExternal(ObjectInput in)101     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
102         nodeID = ExternalizableUtil.getInstance().readByteArray(in);
103         handler = (JID) ExternalizableUtil.getInstance().readSerializable(in);
104         ExternalizableUtil.getInstance().readStrings(in, receivers);
105     }
106 }
107