ClientRoute.java 2.42 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * $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.spi;

Gaston Dombiak's avatar
Gaston Dombiak committed
14
import org.jivesoftware.openfire.XMPPServer;
15
import org.jivesoftware.openfire.cluster.NodeID;
16 17
import org.jivesoftware.util.cache.CacheSizes;
import org.jivesoftware.util.cache.Cacheable;
Gaston Dombiak's avatar
Gaston Dombiak committed
18 19 20 21 22 23 24 25 26 27 28 29 30
import org.jivesoftware.util.cache.ExternalizableUtil;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * Internal object used by RoutingTableImpl to keep track of the node that own a ClientSession
 * and whether the session is available or not.
 *
 * @author Gaston Dombiak
 */
31
public class ClientRoute implements Cacheable, Externalizable {
Gaston Dombiak's avatar
Gaston Dombiak committed
32

Gaston Dombiak's avatar
Gaston Dombiak committed
33
    private NodeID nodeID;
Gaston Dombiak's avatar
Gaston Dombiak committed
34 35 36 37 38 39
    private boolean available;

    public ClientRoute() {
    }


Gaston Dombiak's avatar
Gaston Dombiak committed
40
    public NodeID getNodeID() {
41 42 43 44 45 46 47 48
        return nodeID;
    }


    public boolean isAvailable() {
        return available;
    }

49
    public ClientRoute(NodeID nodeID, boolean available) {
Gaston Dombiak's avatar
Gaston Dombiak committed
50
        this.nodeID = nodeID;
Gaston Dombiak's avatar
Gaston Dombiak committed
51 52 53
        this.available = available;
    }

54 55 56 57 58
    public int getCachedSize() {
        // Approximate the size of the object in bytes by calculating the size
        // of each field.
        int size = 0;
        size += CacheSizes.sizeOfObject();      // overhead of object
Gaston Dombiak's avatar
Gaston Dombiak committed
59
        size += nodeID.toByteArray().length;                  // Node ID
60 61 62 63
        size += CacheSizes.sizeOfBoolean();     // available
        return size;
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
64
    public void writeExternal(ObjectOutput out) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
65
        ExternalizableUtil.getInstance().writeByteArray(out, nodeID.toByteArray());
Gaston Dombiak's avatar
Gaston Dombiak committed
66 67 68 69
        ExternalizableUtil.getInstance().writeBoolean(out, available);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
Gaston Dombiak's avatar
Gaston Dombiak committed
70 71 72 73 74 75
        byte[] bytes = ExternalizableUtil.getInstance().readByteArray(in);
        // Retrieve the NodeID but try to use the singleton instance
        if (XMPPServer.getInstance().getNodeID().equals(bytes)) {
            nodeID = XMPPServer.getInstance().getNodeID();
        }
        else {
76
            nodeID = NodeID.getInstance(bytes);
Gaston Dombiak's avatar
Gaston Dombiak committed
77
        }
Gaston Dombiak's avatar
Gaston Dombiak committed
78 79 80
        available = ExternalizableUtil.getInstance().readBoolean(in);
    }
}