SocketTagger.java revision 7e9a64c1d1abda223774d523acd1dbf895f9421b
15112325117859c7c6cd042c17f519f967c551b20Jesse Wilson/* 25112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Copyright (C) 2011 The Android Open Source Project 35112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * 45112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 55112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * you may not use this file except in compliance with the License. 65112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * You may obtain a copy of the License at 75112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * 85112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 95112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * 105112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Unless required by applicable law or agreed to in writing, software 115112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 125112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * See the License for the specific language governing permissions and 145112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * limitations under the License. 155112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 165112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 175112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonpackage dalvik.system; 185112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 195112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonimport java.io.FileDescriptor; 205112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonimport java.net.Socket; 215112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonimport java.net.SocketException; 225112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 235112325117859c7c6cd042c17f519f967c551b20Jesse Wilson/** 245112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Callbacks for socket assignment and reassignment. 255112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * 265112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * @hide 275112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 285112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonpublic abstract class SocketTagger { 295112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 305112325117859c7c6cd042c17f519f967c551b20Jesse Wilson private static SocketTagger tagger = new SocketTagger() { 315112325117859c7c6cd042c17f519f967c551b20Jesse Wilson @Override public void tag(FileDescriptor socketDescriptor) throws SocketException {} 325112325117859c7c6cd042c17f519f967c551b20Jesse Wilson @Override public void untag(FileDescriptor socketDescriptor) throws SocketException {} 335112325117859c7c6cd042c17f519f967c551b20Jesse Wilson }; 345112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 355112325117859c7c6cd042c17f519f967c551b20Jesse Wilson /** 365112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Notified when {@code socketDescriptor} is either assigned to the current 375112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * thread. The socket is either newly connected or reused from a connection 385112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * pool. Implementations of this method should be thread-safe. 395112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 405112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public abstract void tag(FileDescriptor socketDescriptor) throws SocketException; 415112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 425112325117859c7c6cd042c17f519f967c551b20Jesse Wilson /** 435112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Notified when {@code socketDescriptor} is released from the current 445112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * thread to a connection pool. Implementations of this method should be 455112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * thread-safe. 465112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * 475112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * <p><strong>Note:</strong> this method will not be invoked when the socket 485112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * is closed. 495112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 505112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public abstract void untag(FileDescriptor socketDescriptor) throws SocketException; 515112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 525112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public final void tag(Socket socket) throws SocketException { 537e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey if (!socket.isClosed()) { 547e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey tag(socket.getFileDescriptor$()); 557e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey } 565112325117859c7c6cd042c17f519f967c551b20Jesse Wilson } 575112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 585112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public final void untag(Socket socket) throws SocketException { 597e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey if (!socket.isClosed()) { 607e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey untag(socket.getFileDescriptor$()); 617e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey } 625112325117859c7c6cd042c17f519f967c551b20Jesse Wilson } 635112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 645112325117859c7c6cd042c17f519f967c551b20Jesse Wilson /** 655112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Sets this process' socket tagger to {@code tagger}. 665112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 675112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public static synchronized void set(SocketTagger tagger) { 685112325117859c7c6cd042c17f519f967c551b20Jesse Wilson if (tagger == null) { 695112325117859c7c6cd042c17f519f967c551b20Jesse Wilson throw new NullPointerException("tagger == null"); 705112325117859c7c6cd042c17f519f967c551b20Jesse Wilson } 715112325117859c7c6cd042c17f519f967c551b20Jesse Wilson SocketTagger.tagger = tagger; 725112325117859c7c6cd042c17f519f967c551b20Jesse Wilson } 735112325117859c7c6cd042c17f519f967c551b20Jesse Wilson 745112325117859c7c6cd042c17f519f967c551b20Jesse Wilson /** 755112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Returns this process socket tagger. 765112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */ 775112325117859c7c6cd042c17f519f967c551b20Jesse Wilson public static synchronized SocketTagger get() { 785112325117859c7c6cd042c17f519f967c551b20Jesse Wilson return tagger; 795112325117859c7c6cd042c17f519f967c551b20Jesse Wilson } 805112325117859c7c6cd042c17f519f967c551b20Jesse Wilson} 81