001: // $Id: DataConverter.java 1546 2007-07-23 06:07:56Z grro $
002:
003: /*
004: * Copyright (c) xcache.org, 2007. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
021: * The latest copy of this software may be found on http://www.xcache.org/
022: */
023: package org.xcache;
024:
025: import java.io.IOException;
026: import java.net.InetAddress;
027: import java.net.InetSocketAddress;
028: import java.net.UnknownHostException;
029: import java.nio.ByteBuffer;
030:
031: import org.xsocket.IDataSink;
032: import org.xsocket.IDataSource;
033:
034: /**
035: *
036: *
037: * @author grro@xsocket.org
038: */
039: final class Address {
040:
041: private InetAddress address = null;
042: private int port = 0;
043:
044: public Address(InetAddress address, int port) {
045: this .address = address;
046: this .port = port;
047: }
048:
049: private Address(IDataSource dataSource) throws IOException {
050: int length = dataSource.readInt();
051: byte[] bytes = dataSource.readBytesByLength(length);
052: deserialize(ByteBuffer.wrap(bytes));
053: }
054:
055: private void deserialize(ByteBuffer buffer)
056: throws UnknownHostException {
057: int packetSize = buffer.getInt();
058:
059: port = buffer.getInt();
060:
061: byte[] addressBytes = new byte[packetSize - 4 - 4];
062: buffer.get(addressBytes, 0, addressBytes.length);
063: address = InetAddress.getByAddress(addressBytes);
064: }
065:
066: private ByteBuffer serialize() {
067: int contentSize = 0;
068:
069: byte[] addressBytes = address.getAddress();
070: if (addressBytes.length == 4) {
071: contentSize = 4 + 4 + 4;
072: } else {
073: contentSize = 4 + 4 + 16;
074: }
075:
076: ByteBuffer buffer = ByteBuffer.allocate(contentSize);
077: serialize(buffer, contentSize, addressBytes);
078:
079: buffer.clear();
080: return buffer;
081: }
082:
083: private void serialize(ByteBuffer buffer, int contentSize,
084: byte[] addressBytes) {
085: buffer.putInt(contentSize);
086: buffer.putInt(port);
087: buffer.put(addressBytes);
088: }
089:
090: public InetAddress getAddress() {
091: return address;
092: }
093:
094: public int getPort() {
095: return port;
096: }
097:
098: InetSocketAddress toSocketAddress() {
099: return new InetSocketAddress(address, port);
100: }
101:
102: static Address readFrom(IDataSource dataSource) throws IOException {
103: return new Address(dataSource);
104: }
105:
106: int writeTo(IDataSink dataSink) throws IOException {
107: ByteBuffer buffer = serialize();
108:
109: int written = dataSink.write(buffer.remaining());
110: written += dataSink.write(buffer);
111:
112: return written;
113: }
114:
115: @Override
116: public String toString() {
117: return "/" + address + ":" + port;
118: }
119:
120: @Override
121: public int hashCode() {
122: return address.hashCode() ^ new Integer(port).hashCode();
123: }
124:
125: @Override
126: public boolean equals(Object other) {
127: if (other instanceof Address) {
128: Address nodeAddress = (Address) other;
129: if (nodeAddress.getPort() == this .getPort()) {
130: return nodeAddress.address.equals(this .address);
131: }
132: }
133: return false;
134: }
135: }
|