Return-Path: Delivered-To: apmail-incubator-cassandra-commits-archive@minotaur.apache.org Received: (qmail 52294 invoked from network); 21 Apr 2009 14:56:47 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 21 Apr 2009 14:56:47 -0000 Received: (qmail 91350 invoked by uid 500); 21 Apr 2009 14:56:47 -0000 Delivered-To: apmail-incubator-cassandra-commits-archive@incubator.apache.org Received: (qmail 91321 invoked by uid 500); 21 Apr 2009 14:56:47 -0000 Mailing-List: contact cassandra-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cassandra-dev@incubator.apache.org Delivered-To: mailing list cassandra-commits@incubator.apache.org Received: (qmail 91293 invoked by uid 99); 21 Apr 2009 14:56:47 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Apr 2009 14:56:47 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Apr 2009 14:56:38 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0CF9223888F4; Tue, 21 Apr 2009 14:56:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r767167 - /incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java Date: Tue, 21 Apr 2009 14:56:16 -0000 To: cassandra-commits@incubator.apache.org From: jbellis@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090421145617.0CF9223888F4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jbellis Date: Tue Apr 21 14:56:16 2009 New Revision: 767167 URL: http://svn.apache.org/viewvc?rev=767167&view=rev Log: fix strange line endings and indentation so we can apply patches sanely. patch by Per Mellqvist; reviewed by Eric Evans for #43 Modified: incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java Modified: incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java?rev=767167&r1=767166&r2=767167&view=diff ============================================================================== --- incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java (original) +++ incubator/cassandra/trunk/src/org/apache/cassandra/net/EndPoint.java Tue Apr 21 14:56:16 2009 @@ -1 +1,180 @@ -/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ package org.apache.cassandra.net; import java.io.IOException; import java.io.Serializable; import java.net.*; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.u til.HashMap; import java.util.Map; import org.apache.cassandra.utils.FBUtilities; import org.apache.cassandra.utils.LogUtil; import org.apache.log4j.Logger; /** * Author : Avinash Lakshman ( alakshman@facebook.com) & Prashant Malik ( pmalik@facebook.com ) */ public class EndPoint implements Serializable, Comparable { // logging and profiling. private static Logger logger_ = Logger.getLogger(EndPoint.class); private static final long serialVersionUID = -4962625949179835907L; private static Map hostNames_ = new HashMap(); protected static final int randomPort_ = 5555; public static EndPoint randomLocalEndPoint_; static { try { randomLocalEndPoint_ = new EndPoint(FBUtilities.getHostName(), EndPoint.randomPort_); } catch ( IOException ex ) { logger_.warn(LogUtil.throwableToString(ex)); } } private String host_; pri vate int port_; private transient InetSocketAddress ia_; /* Ctor for JAXB. DO NOT DELETE */ private EndPoint() { } public EndPoint(String host, int port) { /* * Attempts to resolve the host, but does not fail if it cannot. */ host_ = host; port_ = port; } // create a local endpoint id public EndPoint(int port) { try { host_ = FBUtilities.getHostName(); port_ = port; } catch (UnknownHostException e) { logger_.warn(LogUtil.throwableToString(e)); } } public String getHost() { return host_; } public int getPort() { return port_; } public void setPort(int port) { port_ = port; } public InetSocketAddress getInetAddress() { if (ia_ == null || ia_.isUnresolved()) { ia_ = new InetSocketAddress(host_, port_); } return ia_; } public boolean equals(Object o) { if (!(o instanceof EndPoint)) return false; EndPoint rhs = (EndPoint) o; return (host_.equals(rhs.host_) && port_ == rhs.port_); } p ublic int hashCode() { return (host_ + port_).hashCode(); } public int compareTo(EndPoint rhs) { return host_.compareTo(rhs.host_); } public String toString() { return (host_ + ":" + port_); } public static EndPoint fromString(String str) { String[] values = str.split(":"); return new EndPoint(values[0], Integer.parseInt(values[1])); } public static byte[] toBytes(EndPoint ep) { ByteBuffer buffer = ByteBuffer.allocate(6); byte[] iaBytes = ep.getInetAddress().getAddress().getAddress(); buffer.put(iaBytes); buffer.put(MessagingService.toByteArray((short) ep.getPort())); buffer.flip(); return buffer.array(); } public static EndPoint fromBytes(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(4); System.arraycopy(bytes, 0, buffer.array(), 0, 4); byte[] portBytes = new byte[2]; System.arraycopy(bytes, 4, portBytes, 0, portBytes.length); try { CharBuffer charBuffer = buffer.asCharBuffer(); String host = hostNames_. get(charBuffer); if (host == null) { host = InetAddress.getByAddress(buffer.array()).getHostName(); hostNames_.put(charBuffer, host); } int port = (int) MessagingService.byteArrayToShort(portBytes); return new EndPoint(host, port); } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } } } \ No newline at end of file +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.cassandra.net; + + +import java.io.IOException; +import java.io.Serializable; +import java.net.*; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.util.HashMap; +import java.util.Map; + +import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.LogUtil; +import org.apache.log4j.Logger; + +/** + * Author : Avinash Lakshman ( alakshman@facebook.com) & Prashant Malik ( pmalik@facebook.com ) + */ + +public class EndPoint implements Serializable, Comparable +{ + // logging and profiling. + private static Logger logger_ = Logger.getLogger(EndPoint.class); + private static final long serialVersionUID = -4962625949179835907L; + private static Map hostNames_ = new HashMap(); + protected static final int randomPort_ = 5555; + public static EndPoint randomLocalEndPoint_; + + static + { + try + { + randomLocalEndPoint_ = new EndPoint(FBUtilities.getHostName(), EndPoint.randomPort_); + } + catch ( IOException ex ) + { + logger_.warn(LogUtil.throwableToString(ex)); + } + } + + private String host_; + private int port_; + + private transient InetSocketAddress ia_; + + /* Ctor for JAXB. DO NOT DELETE */ + private EndPoint() + { + } + + public EndPoint(String host, int port) + { + /* + * Attempts to resolve the host, but does not fail if it cannot. + */ + host_ = host; + port_ = port; + } + + // create a local endpoint id + public EndPoint(int port) + { + try + { + host_ = FBUtilities.getHostName(); + port_ = port; + } + catch (UnknownHostException e) + { + logger_.warn(LogUtil.throwableToString(e)); + } + } + + public String getHost() + { + return host_; + } + + public int getPort() + { + return port_; + } + + public void setPort(int port) + { + port_ = port; + } + + public InetSocketAddress getInetAddress() + { + if (ia_ == null || ia_.isUnresolved()) + { + ia_ = new InetSocketAddress(host_, port_); + } + return ia_; + } + + public boolean equals(Object o) + { + if (!(o instanceof EndPoint)) + return false; + + EndPoint rhs = (EndPoint) o; + return (host_.equals(rhs.host_) && port_ == rhs.port_); + } + + public int hashCode() + { + return (host_ + port_).hashCode(); + } + + public int compareTo(EndPoint rhs) + { + return host_.compareTo(rhs.host_); + } + + public String toString() + { + return (host_ + ":" + port_); + } + + public static EndPoint fromString(String str) + { + String[] values = str.split(":"); + return new EndPoint(values[0], Integer.parseInt(values[1])); + } + + public static byte[] toBytes(EndPoint ep) + { + ByteBuffer buffer = ByteBuffer.allocate(6); + byte[] iaBytes = ep.getInetAddress().getAddress().getAddress(); + buffer.put(iaBytes); + buffer.put(MessagingService.toByteArray((short) ep.getPort())); + buffer.flip(); + return buffer.array(); + } + + public static EndPoint fromBytes(byte[] bytes) + { + ByteBuffer buffer = ByteBuffer.allocate(4); + System.arraycopy(bytes, 0, buffer.array(), 0, 4); + byte[] portBytes = new byte[2]; + System.arraycopy(bytes, 4, portBytes, 0, portBytes.length); + try + { + CharBuffer charBuffer = buffer.asCharBuffer(); + String host = hostNames_.get(charBuffer); + if (host == null) + { + host = InetAddress.getByAddress(buffer.array()).getHostName(); + hostNames_.put(charBuffer, host); + } + int port = (int) MessagingService.byteArrayToShort(portBytes); + return new EndPoint(host, port); + } + catch (UnknownHostException e) + { + throw new IllegalArgumentException(e); + } + } +} +