001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import java.net.InetSocketAddress;
021import java.util.Objects;
022
023import org.apache.hadoop.hbase.security.User;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * This class holds the address and the user ticket, etc. The client connections
028 * to servers are uniquely identified by <remoteAddress, ticket, serviceName>
029 */
030@InterfaceAudience.Private
031class ConnectionId {
032  private static final int PRIME = 16777619;
033  final User ticket;
034  final String serviceName;
035  final InetSocketAddress address;
036
037  public ConnectionId(User ticket, String serviceName, InetSocketAddress address) {
038    this.address = address;
039    this.ticket = ticket;
040    this.serviceName = serviceName;
041  }
042
043  public String getServiceName() {
044    return this.serviceName;
045  }
046
047  public InetSocketAddress getAddress() {
048    return address;
049  }
050
051  public User getTicket() {
052    return ticket;
053  }
054
055  @Override
056  public String toString() {
057    return this.address.toString() + "/" + this.serviceName + "/" + this.ticket;
058  }
059
060  @Override
061  public boolean equals(Object obj) {
062    if (obj instanceof ConnectionId) {
063      ConnectionId id = (ConnectionId) obj;
064      return address.equals(id.address) &&
065            ((ticket != null && ticket.equals(id.ticket)) ||
066             (ticket == id.ticket)) && Objects.equals(this.serviceName, id.serviceName);
067    }
068    return false;
069  }
070
071  @Override  // simply use the default Object#hashcode() ?
072  public int hashCode() {
073    return hashCode(ticket,serviceName,address);
074  }
075
076  public static int hashCode(User ticket, String serviceName, InetSocketAddress address) {
077    return (address.hashCode() +
078        PRIME * (PRIME * serviceName.hashCode() ^
079            (ticket == null ? 0 : ticket.hashCode())));
080  }
081}