亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? netaccessmanager.java

?? stun的java實現(xiàn)
?? JAVA
字號:
/*
 * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.stun4j.stack;

import net.java.stun4j.StunException;
import net.java.stun4j.NetAccessPointDescriptor;
import net.java.stun4j.message.Message;

import java.util.Hashtable;
import java.io.*;
import java.util.Vector;
import java.util.*;
import net.java.stun4j.*;
import java.net.DatagramSocket;
import java.util.logging.*;

/**
 * Manages NetAccessPoints and MessageProcessor pooling. This class serves as a
 * layer that masks network primitives and provides equivalent STUN abstractions.
 * Instances that operate with the NetAccessManager are only supposed to
 * understand STUN talk and shouldn't be aware of datagrams sockets, and etc.
 *
 * <p>Organisation: Louis Pasteur University, Strasbourg, France</p>
 *                   <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
 * @author Emil Ivov
 * @version 0.1
 */

class NetAccessManager
    implements ErrorHandler
{
    private static final Logger logger =
        Logger.getLogger(NetAccessManager.class.getName());
    /**
     * All access points currently in use. The table maps NetAccessPointDescriptor-s
     * to NetAccessPoint-s
     */
    private Hashtable    netAccessPoints   = new Hashtable();

    /**
     * A synchronized FIFO where incoming messages are stocked for processing.
     */
    private MessageQueue messageQueue      = new MessageQueue();

    /**
     * A thread pool of message processors.
     */
    private Vector       messageProcessors = new Vector();

    /**
     * The instance that should be notified whan an incoming message has been
     * processed and ready for delivery
     */
    private MessageEventHandler messageEventHandler = null;

    /**
     * Indicates whether the access manager has been started.
     */
    private boolean isRunning = false;

    /**
     * The size of the thread pool to start with.
     */
    private int initialThreadPoolSize = StunStack.DEFAULT_THREAD_POOL_SIZE;

    /**
     * Constructs a NetAccessManager.
     */
    NetAccessManager()
    {
    }



    /**
     * Initializes the message processors pool and sets the status of the manager
     * to running.
     */
    synchronized void start()
    {
        if(isRunning)
            return;

        this.isRunning = true;
        this.initThreadPool();
    }

    /**
     * Stops and deletes all message processors and access points.
     */
    void shutDown()
    {
        if (!isRunning)
            return;

        //stop access points
        logger.info("removing " + netAccessPoints.size() + " access points.");
        Enumeration keys = netAccessPoints.keys();
        while (keys.hasMoreElements())
        {
            NetAccessPointDescriptor apd = (NetAccessPointDescriptor) keys.
                nextElement();
            removeNetAccessPoint(apd);
            logger.info(".");
        }
        logger.info("removed all access points");

        //stop and empty thread pool
        while (!messageProcessors.isEmpty())
        {
            MessageProcessor mp = (MessageProcessor) messageProcessors.remove(0);
            mp.stop();
        }

        //removed a call to messageQueue.notifyAll(). it was useless since it
        //was only releasing a single message processor which then immediately
        //got stuck in a wait since the message queue was empty.(Bug Report
        //originally received from Pascal Maugeri who reported zombie threads
        //still running after a stack shutdown).

        isRunning = false;
    }

    /**
     * Determines whether the NetAccessManager has been started.
     * @return true if this NetAccessManager has been started, and false
     * otherwise.
     */
    boolean isRunning()
    {
        return isRunning;
    }


    /**
     * Sets the instance to notify for incoming message events.
     * @param evtHandler the entity that will handle incoming messages.
     */
    void setEventHandler(MessageEventHandler evtHandler)
    {
        messageEventHandler = evtHandler;
    }

//------------------------ error handling -------------------------------------
    /**
     * A civilized way of not caring!
     * @param message a description of the error
     * @param error   the error that has occurred
     */
    public void handleError(String message, Throwable error)
    {
        /**
         * apart from logging, i am not sure what else we could do here.
         */
        logger.log( Level.WARNING, "The following error occurred", error);
    }

    /**
     * Clears the faulty thread and tries to repair the damage and instanciate
     * a replacement.
     *
     * @param callingThread the thread where the error occurred.
     * @param message       A description of the error
     * @param error         The error itself
     */
    public void handleFatalError(Runnable callingThread, String message, Throwable error)
    {
        if (callingThread instanceof NetAccessPoint)
        {
            NetAccessPoint ap = (NetAccessPoint)callingThread;

            //make sure socket is closed
            removeNetAccessPoint(ap.getDescriptor());

            try
            {
                logger.log( Level.WARNING, "An access point has unexpectedly "
                    +"stopped. AP:" + ap.toString(), error);
                installNetAccessPoint(ap.getDescriptor());
            }
            catch (StunException ex)
            {
                //make sure nothing's left and notify user
                removeNetAccessPoint(ap.getDescriptor());
                logger.log(Level.WARNING, "Failed to relaunch accesspoint:" + ap,
                           ex);
            }
        }
        else if( callingThread instanceof MessageProcessor )
        {
            MessageProcessor mp = (MessageProcessor)callingThread;
            logger.log( Level.WARNING, "A message processor has unexpectedly "
                    +"stopped. AP:" + mp.toString(), error);

            //make sure the guy's dead.
            mp.stop();
            messageProcessors.remove(mp);

            mp = new MessageProcessor(messageQueue, messageEventHandler, this);
            mp.start();
            logger.fine("A message processor has been relaunched because "
                        +"of an error.");
        }
    }

    /**
     * Creates and starts a new access point according to the given descriptor.
     * If the specified access point has already been installed the method
     * has no effect.
     *
     * @param apDescriptor   a description of the access point to create.
     * @throws StunException if we fail to create or start the accesspoint.
     */
    void installNetAccessPoint(NetAccessPointDescriptor apDescriptor)
        throws StunException
    {
        if(netAccessPoints.containsKey(apDescriptor))
            return;

        NetAccessPoint ap = new NetAccessPoint(apDescriptor, messageQueue, this);
        netAccessPoints.put(apDescriptor, ap);

        try
        {
            ap.start();
        }
        catch (IOException ex)
        {
            logger.log(Level.WARNING, "The NAPD("+ap+") failed to bind ", ex);
            throw new StunException(
                      StunException.NETWORK_ERROR,
                      "An IOException occurred while starting access point: "
                      +apDescriptor.toString() ,
                      ex);
        }
    }

    /**
     * Creates and starts a new access point based on the specified socket.
     * If the specified access point has already been installed the method
     * has no effect.
     *
     * @param  socket   the socket that the access point should use.
     * @return an access point descriptor to allow further management of the
     * newly created access point.
     * @throws StunException if we fail to create or start the accesspoint.
     */
    NetAccessPointDescriptor installNetAccessPoint(DatagramSocket socket)
        throws StunException
    {

        //no null check - let it through a null pointer exception
        StunAddress address = new StunAddress(socket.getLocalAddress(), socket.getLocalPort());
        NetAccessPointDescriptor apDescriptor = new NetAccessPointDescriptor(address);

        if(netAccessPoints.containsKey(apDescriptor))
            return apDescriptor;

        NetAccessPoint ap = new NetAccessPoint(apDescriptor, messageQueue, this);
        //call the useExternalSocket method to avoid closing the socket when
        //removing the accesspoint. Bug Report - Dave Stuart - SipQuest
        ap.useExternalSocket(socket);
        netAccessPoints.put(apDescriptor, ap);

        try
        {
            ap.start();
        }
        catch (IOException ex)
        {
            throw new StunException(
                      StunException.NETWORK_ERROR,
                      "An IOException occurred while starting the access point",
                      ex);
        }

        return apDescriptor;
    }


    /**
     * Stops and deletes the specified access point.
     * @param apDescriptor the access  point to remove
     */
    void removeNetAccessPoint(NetAccessPointDescriptor apDescriptor)
    {
        NetAccessPoint ap = (NetAccessPoint)netAccessPoints.remove(apDescriptor);

        if(ap != null)
            ap.stop();
    }

    //---------------thread pool implementation --------------------------------
    /**
     * Adjusts the number of concurrently running MessageProcessors.
     * If the number is smaller or bigger than the number of threads
     * currentlyrunning, then message processors are created/deleted so that their
     * count matches the new value.
     *
     * @param threadPoolSize the number of MessageProcessors that should be running concurrently
     * @throws StunException INVALID_ARGUMENT if threadPoolSize is not a valid size.
     */
    void setThreadPoolSize(int threadPoolSize)
        throws StunException
    {
        if(threadPoolSize < 1)
            throw new StunException(StunException.ILLEGAL_ARGUMENT,
                                    threadPoolSize + " is not a legal thread pool size value.");

        //if we are not running just record the size so that we could init later.
        if(!isRunning)
        {
            initialThreadPoolSize = threadPoolSize;
            return;
        }

        if(messageProcessors.size() < threadPoolSize)
        {
            //create additional processors
            fillUpThreadPool(threadPoolSize);
        }
        else
        {
            //delete extra processors
            shrinkThreadPool(threadPoolSize);
        }
    }

    /**
     * @throws StunException INVALID_ARGUMENT if threadPoolSize is not a valid size.
     */
    private void initThreadPool()
    {
            //create additional processors
            fillUpThreadPool(initialThreadPoolSize);
    }


    /**
     * Starts all message processors
     *
     * @param newSize the new thread poolsize
     */
    private void fillUpThreadPool(int newSize)
    {
        //make sure we don't resize more than once
        messageProcessors.ensureCapacity(newSize);

        for (int i = messageProcessors.size(); i < newSize; i++)
        {
            MessageProcessor mp = new MessageProcessor(messageQueue,
                                                       messageEventHandler,
                                                       this);
            messageProcessors.add(mp);

            mp.start();
        }

    }

    /**
     * Starts all message processors
     *
     * @param newSize the new thread poolsize
     */
    private void shrinkThreadPool(int newSize)
    {
        while(messageProcessors.size() > newSize)
        {
            MessageProcessor mp = (MessageProcessor)messageProcessors.remove(0);
            mp.stop();
        }
    }

    //--------------- SENDING MESSAGES -----------------------------------------
    /**
     * Sends the specified stun message through the specified access point.
     * @param stunMessage the message to send
     * @param apDescriptor the access point to use to send the message
     * @param address the destination of the message.
     * @throws StunException if message encoding fails, ILLEGAL_ARGUMENT if the
     * apDescriptor references an access point that had not been installed,
     * NETWORK_ERROR if an error occurs while sending message bytes through the
     * network socket.
     */
    void sendMessage(Message                  stunMessage,
                     NetAccessPointDescriptor apDescriptor,
                     StunAddress                  address)
        throws StunException
    {
        byte[] bytes = stunMessage.encode();
        NetAccessPoint ap = (NetAccessPoint)netAccessPoints.get(apDescriptor);

        if(ap == null)
            throw new StunException(
                          StunException.ILLEGAL_ARGUMENT,
                          "The specified access point had not been installed.");

        try
        {
            ap.sendMessage(bytes, address);
        }
        catch (Exception ex)
        {
            throw new StunException(StunException.NETWORK_ERROR,
                        "An Exception occurred while sending message bytes "
                        +"through a network socket!",
                        ex);
        }
    }

}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久爱另类一区二区小说| 精品欧美一区二区久久 | 精品一区二区在线视频| 国产乱码精品一品二品| 欧美三级蜜桃2在线观看| 国产欧美日韩亚州综合| 亚洲成人av电影| 成人18视频在线播放| 欧美一区二区三区不卡| 一区二区三区免费看视频| 国产a级毛片一区| 久久久久久久久岛国免费| 日韩中文字幕区一区有砖一区 | 欧美国产乱子伦| 欧美aaa在线| 欧美精品色综合| 无吗不卡中文字幕| 日本电影欧美片| 中文字幕佐山爱一区二区免费| 成人在线一区二区三区| 久久久久久亚洲综合| 亚洲一区视频在线| 欧美日韩国产精品自在自线| 婷婷中文字幕综合| 91精品国产91热久久久做人人| 亚洲一区二区三区中文字幕| 成人黄色在线网站| 亚洲综合在线第一页| 在线视频亚洲一区| 日韩国产精品久久久久久亚洲| 在线亚洲高清视频| 亚洲国产精品久久人人爱蜜臀| 欧美无砖专区一中文字| 秋霞午夜鲁丝一区二区老狼| 欧美美女视频在线观看| 激情综合亚洲精品| 日韩伦理免费电影| 欧美欧美欧美欧美| 国产99久久久国产精品免费看| 久久久亚洲午夜电影| 色天使色偷偷av一区二区| 久久国产人妖系列| 亚洲男人的天堂一区二区| 欧美一区三区四区| av午夜精品一区二区三区| 精品在线观看视频| 一区二区三区在线视频播放| 久久青草欧美一区二区三区| 国产黄色精品网站| 亚洲精品国产a久久久久久| 日韩一区二区影院| 欧美在线观看你懂的| 国产成人亚洲精品狼色在线| 日韩电影免费一区| 一区二区三区影院| 亚洲丝袜制服诱惑| 国产精品久久久久婷婷二区次| 欧美va亚洲va香蕉在线| 欧美日韩极品在线观看一区| 99视频精品全部免费在线| 国产福利精品一区| 盗摄精品av一区二区三区| 激情欧美一区二区三区在线观看| 图片区小说区国产精品视频| 亚洲伦理在线精品| 婷婷开心激情综合| 99久久伊人久久99| 国产成人aaaa| 久久99精品一区二区三区 | 国产精品免费观看视频| 日韩av在线免费观看不卡| 亚洲一区二区不卡免费| 国产精品久久久久久一区二区三区 | 亚洲自拍偷拍九九九| 中文字幕永久在线不卡| 国产日产欧美一区二区三区| 国产性色一区二区| 中文字幕中文字幕一区| 一区二区三区在线看| 首页综合国产亚洲丝袜| 免费高清视频精品| 国产麻豆午夜三级精品| 国产传媒久久文化传媒| 91偷拍与自偷拍精品| 欧美日韩亚洲综合| 国产亚洲精品bt天堂精选| 国产精品成人免费在线| 五月天欧美精品| 国产高清在线观看免费不卡| 99精品在线免费| 日韩欧美一区中文| 亚洲欧洲国产日本综合| 老司机精品视频导航| 色婷婷综合久久久| 26uuu亚洲| 麻豆精品视频在线| 色婷婷久久久综合中文字幕| 精品国产sm最大网站免费看| 亚洲综合精品自拍| 91在线小视频| 国产三级一区二区三区| 日韩国产欧美在线播放| 日本精品视频一区二区| 国产精品国模大尺度视频| 精品影视av免费| 日韩午夜小视频| 免费精品视频在线| 91精选在线观看| 秋霞国产午夜精品免费视频| 欧美日韩一区二区三区四区| 亚洲一级在线观看| 91极品视觉盛宴| 亚洲尤物在线视频观看| www.成人网.com| 亚洲二区视频在线| 欧美成人bangbros| 国产91精品一区二区| 亚洲另类中文字| 欧美成人精精品一区二区频| 国产毛片精品国产一区二区三区| 国产精品国产自产拍在线| 欧美日韩一级大片网址| 日韩精品一二区| 中文字幕在线不卡| 欧美一级高清片| 欧洲国内综合视频| 国产一区免费电影| 亚洲一区二区免费视频| 久久久久久亚洲综合影院红桃| 一本到高清视频免费精品| 久久精品国产精品亚洲红杏 | 欧美一二三四区在线| 国产99久久久国产精品潘金| 肉丝袜脚交视频一区二区| 国产精品电影一区二区三区| 精品久久久久久久久久久久久久久久久| 国产一区二区三区高清播放| 亚洲成a人v欧美综合天堂| 国产精品二区一区二区aⅴ污介绍| 欧美一区国产二区| 欧美日本精品一区二区三区| 97精品久久久午夜一区二区三区 | 亚洲人成网站在线| 亚洲国产精品精华液2区45| 日韩视频一区在线观看| 欧美精品vⅰdeose4hd| 天堂午夜影视日韩欧美一区二区| 日韩av电影免费观看高清完整版 | 91成人在线免费观看| 成人一区二区在线观看| 免费成人美女在线观看.| 麻豆精品精品国产自在97香蕉| 午夜精品福利一区二区蜜股av| 亚洲综合男人的天堂| 一区二区视频免费在线观看| 亚洲乱码国产乱码精品精小说| 综合激情成人伊人| 亚洲国产精品一区二区www在线| 亚洲一区二区三区四区五区黄| 亚洲尤物在线视频观看| 另类专区欧美蜜桃臀第一页| 国产一区二区在线视频| 色婷婷av久久久久久久| 972aa.com艺术欧美| 在线一区二区三区| 日韩一区二区电影| 国产精品国产三级国产aⅴ入口 | 国产亚洲婷婷免费| 亚洲人123区| 久久精品99久久久| 91在线视频播放地址| 日韩欧美国产电影| 亚洲免费伊人电影| 精品一区二区三区av| 色噜噜狠狠成人中文综合| 日韩午夜在线播放| 亚洲欧美综合另类在线卡通| 日韩av不卡一区二区| 99久久久精品| 国产视频视频一区| 亚洲国产欧美日韩另类综合| 国产精品资源在线看| 欧美一区二区在线视频| 一区二区三区国产精品| 成人精品视频.| 欧美一级二级在线观看| 天堂久久久久va久久久久| av中文字幕不卡| 国产精品麻豆久久久| 国产一区高清在线| 久久综合色一综合色88| 亚瑟在线精品视频| 欧美精品 日韩| 视频在线观看国产精品| 欧美久久久一区| 天天色 色综合| 91精品国产福利在线观看| 亚洲6080在线| 欧美xxxxxxxx| 国产成人免费高清|