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;
20392ded55c1678393b555da14289919cbe262ae3cJeff Sharkeyimport java.net.DatagramSocket;
215112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonimport java.net.Socket;
225112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonimport java.net.SocketException;
235112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
245112325117859c7c6cd042c17f519f967c551b20Jesse Wilson/**
255112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * Callbacks for socket assignment and reassignment.
265112325117859c7c6cd042c17f519f967c551b20Jesse Wilson *
275112325117859c7c6cd042c17f519f967c551b20Jesse Wilson * @hide
285112325117859c7c6cd042c17f519f967c551b20Jesse Wilson */
295112325117859c7c6cd042c17f519f967c551b20Jesse Wilsonpublic abstract class SocketTagger {
305112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
315112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    private static SocketTagger tagger = new SocketTagger() {
325112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        @Override public void tag(FileDescriptor socketDescriptor) throws SocketException {}
335112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        @Override public void untag(FileDescriptor socketDescriptor) throws SocketException {}
345112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    };
355112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
365112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    /**
375112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * Notified when {@code socketDescriptor} is either assigned to the current
385112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * thread. The socket is either newly connected or reused from a connection
395112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * pool. Implementations of this method should be thread-safe.
405112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     */
415112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public abstract void tag(FileDescriptor socketDescriptor) throws SocketException;
425112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
435112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    /**
445112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * Notified when {@code socketDescriptor} is released from the current
455112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * thread to a connection pool. Implementations of this method should be
465112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * thread-safe.
475112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     *
485112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * <p><strong>Note:</strong> this method will not be invoked when the socket
495112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * is closed.
505112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     */
515112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public abstract void untag(FileDescriptor socketDescriptor) throws SocketException;
525112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
535112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public final void tag(Socket socket) throws SocketException {
547e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey        if (!socket.isClosed()) {
557e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey            tag(socket.getFileDescriptor$());
567e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey        }
575112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    }
585112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
595112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public final void untag(Socket socket) throws SocketException {
607e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey        if (!socket.isClosed()) {
617e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey            untag(socket.getFileDescriptor$());
627e9a64c1d1abda223774d523acd1dbf895f9421bJeff Sharkey        }
635112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    }
645112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
65392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey    public final void tag(DatagramSocket socket) throws SocketException {
66392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey        if (!socket.isClosed()) {
67392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey            tag(socket.getFileDescriptor$());
68392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey        }
69392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey    }
70392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey
71392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey    public final void untag(DatagramSocket socket) throws SocketException {
72392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey        if (!socket.isClosed()) {
73392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey            untag(socket.getFileDescriptor$());
74392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey        }
75392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey    }
76392ded55c1678393b555da14289919cbe262ae3cJeff Sharkey
775112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    /**
785112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * Sets this process' socket tagger to {@code tagger}.
795112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     */
805112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public static synchronized void set(SocketTagger tagger) {
815112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        if (tagger == null) {
825112325117859c7c6cd042c17f519f967c551b20Jesse Wilson            throw new NullPointerException("tagger == null");
835112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        }
845112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        SocketTagger.tagger = tagger;
855112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    }
865112325117859c7c6cd042c17f519f967c551b20Jesse Wilson
875112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    /**
885112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     * Returns this process socket tagger.
895112325117859c7c6cd042c17f519f967c551b20Jesse Wilson     */
905112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    public static synchronized SocketTagger get() {
915112325117859c7c6cd042c17f519f967c551b20Jesse Wilson        return tagger;
925112325117859c7c6cd042c17f519f967c551b20Jesse Wilson    }
935112325117859c7c6cd042c17f519f967c551b20Jesse Wilson}
94