ClientRoute.java 2.84 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
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.
Gaston Dombiak's avatar
Gaston Dombiak committed
19 20 21 22
 */

package org.jivesoftware.openfire.spi;

Gaston Dombiak's avatar
Gaston Dombiak committed
23
import org.jivesoftware.openfire.XMPPServer;
24
import org.jivesoftware.openfire.cluster.NodeID;
25 26
import org.jivesoftware.util.cache.CacheSizes;
import org.jivesoftware.util.cache.Cacheable;
Gaston Dombiak's avatar
Gaston Dombiak committed
27 28 29 30 31 32 33 34 35 36 37 38 39
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
 */
40
public class ClientRoute implements Cacheable, Externalizable {
Gaston Dombiak's avatar
Gaston Dombiak committed
41

Gaston Dombiak's avatar
Gaston Dombiak committed
42
    private NodeID nodeID;
Gaston Dombiak's avatar
Gaston Dombiak committed
43 44 45 46 47 48
    private boolean available;

    public ClientRoute() {
    }


Gaston Dombiak's avatar
Gaston Dombiak committed
49
    public NodeID getNodeID() {
50 51 52 53 54 55 56 57
        return nodeID;
    }


    public boolean isAvailable() {
        return available;
    }

58
    public ClientRoute(NodeID nodeID, boolean available) {
Gaston Dombiak's avatar
Gaston Dombiak committed
59
        this.nodeID = nodeID;
Gaston Dombiak's avatar
Gaston Dombiak committed
60 61 62
        this.available = available;
    }

63 64 65 66 67
    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
68
        size += nodeID.toByteArray().length;                  // Node ID
69 70 71 72
        size += CacheSizes.sizeOfBoolean();     // available
        return size;
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
73
    public void writeExternal(ObjectOutput out) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
74
        ExternalizableUtil.getInstance().writeByteArray(out, nodeID.toByteArray());
Gaston Dombiak's avatar
Gaston Dombiak committed
75 76 77 78
        ExternalizableUtil.getInstance().writeBoolean(out, available);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
Gaston Dombiak's avatar
Gaston Dombiak committed
79 80 81 82 83 84
        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 {
85
            nodeID = NodeID.getInstance(bytes);
Gaston Dombiak's avatar
Gaston Dombiak committed
86
        }
Gaston Dombiak's avatar
Gaston Dombiak committed
87 88 89
        available = ExternalizableUtil.getInstance().readBoolean(in);
    }
}