⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simtcpconnection.java

📁 High performance DB query
💻 JAVA
字号:
/* * @(#)$Id: SimTCPConnection.java,v 1.6 2004/07/02 23:59:22 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704.  Attention:  Intel License Inquiry. */package simulator.services.network.tcp;import java.net.InetAddress;import java.net.InetSocketAddress;import java.util.HashMap;import java.util.LinkedList;import java.util.ListIterator;import services.network.Payload;import services.network.tcp.TCPConnection;import util.FreeList;import util.FreeListFactory;import util.network.serialization.SerializationManager;/** * Class SimTCPConnection * */public class SimTCPConnection implements TCPConnection {    private static final double RTT_INITIAL_ESTIMATE = 5.0;    private static final double RTT_ESTIMATE_GAIN = .2;    private InetAddress remoteIPAddress;    private Integer remotePortNumber;    private Integer localPortNumber;    private SimTCPMessenger tcpMessenger;    private LinkedList transmitQueue;    private HashMap awaitingACK;    private int sequenceCounter;    private double RTTEstimate;    private boolean connectionComplete, connectionPending, connectionClosed,                    connectionError, connectionOutbound;    private static FreeList freeList =        new FreeList(new SimTCPConnectionFactory());    private void init(InetAddress remoteIPAddress, Integer remotePortNumber,                      Integer localPortNumber, SimTCPMessenger tcpMessenger,                      boolean connectionOutbound) {        this.remoteIPAddress = remoteIPAddress;        this.remotePortNumber = remotePortNumber;        this.localPortNumber = localPortNumber;        this.tcpMessenger = tcpMessenger;        this.connectionOutbound = connectionOutbound;        transmitQueue = new LinkedList();        awaitingACK = new HashMap();        sequenceCounter = 0;        RTTEstimate = RTT_INITIAL_ESTIMATE;        connectionComplete = false;        connectionPending = false;        connectionClosed = false;        connectionError = false;    }    /**     * Method getRemoteIPAddress     * @return     */    public InetSocketAddress getRemoteSocketAddress() {        return new InetSocketAddress(remoteIPAddress,                                     remotePortNumber.intValue());    }    /**     * Method getLocalSocketAddress     * @return     */    public InetSocketAddress getLocalSocketAddress() {        return new InetSocketAddress(localPortNumber.intValue());    }    /**     * Method getRemotePortNumber     * @return     */    public Integer getRemotePortNumber() {        return remotePortNumber;    }    /**     * Method getLocalPortNumber     * @return     */    public Integer getLocalPortNumber() {        return localPortNumber;    }    /**     * Method queueData     *     * @param data     */    protected void queueData(Payload data) {        if ( !connectionClosed) {            transmitQueue.addLast(data);        }    }    /**     * Method dequeueData     * @return     */    protected Payload dequeueData() {        return (Payload) transmitQueue.removeFirst();    }    /**     * Method completeConnection     */    protected void completeConnection() {        connectionPending = false;        connectionComplete = true;    }    /**     * Method closeConnection     */    protected void closeConnection() {        connectionClosed = true;    }    /**     * Method open     */    protected void open() {        connectionPending = true;        connectionComplete = false;    }    /**     * Method error     */    protected void error() {        connectionPending = false;        connectionComplete = false;        connectionError = true;    }    /**     * Method isClosed     * @return     */    public boolean isClosed() {        return connectionClosed;    }    /**     * Method isConnected     * @return     */    public boolean isConnected() {        return connectionComplete;    }    /**     * Method isConnectionPending     * @return     */    public boolean isConnectionPending() {        return connectionPending;    }    /**     * Method isError     * @return     */    public boolean isError() {        return connectionError;    }    /**     * Method isOutbound     * @return     */    protected boolean isOutbound() {        return connectionOutbound;    }    /**     * Method waitingData     * @return     */    protected int waitingData() {        ListIterator iterator = transmitQueue.listIterator();        int waitingData = 0;        while (iterator.hasNext()) {            waitingData += SerializationManager.getPayloadSize(                ((Payload) iterator.next()));        }        return waitingData;    }    /**     * Method sendNext     */    protected void sendNext() {        if (transmitQueue.size() > 0) {            Payload nextItem = (Payload) transmitQueue.removeFirst();            Integer sequenceNumber = new Integer(sequenceCounter);            awaitingACK.put(sequenceNumber, nextItem);            SimTCPDataMessage message =                SimTCPDataMessage.allocate(localPortNumber, remotePortNumber,                                           this, sequenceNumber, nextItem);            tcpMessenger.networkSend(remoteIPAddress, message);        }    }    /**     * Method processACK     *     * @param sequenceNumber     */    protected void processACK(Integer sequenceNumber) {        awaitingACK.remove(sequenceNumber);    }    /**     * Method allocate     *     * @param remoteIPAddress     * @param remotePortNumber     * @param localPortNumber     * @param tcpMessenger     * @param connectionOutbound     * @return     */    protected static SimTCPConnection allocate(InetAddress remoteIPAddress,                                               Integer remotePortNumber,                                               Integer localPortNumber,                                               SimTCPMessenger tcpMessenger,                                               boolean connectionOutbound) {        SimTCPConnection connection = (SimTCPConnection) freeList.allocate();        connection.init(remoteIPAddress, remotePortNumber, localPortNumber,                        tcpMessenger, connectionOutbound);        return connection;    }    /**     * Method free     *     * @param connection     */    public static void free(SimTCPConnection connection) {        freeList.free(connection);    }}/** * Class SimTCPConnectionFactory * */class SimTCPConnectionFactory implements FreeListFactory {    /**     * Method create     * @return     */    public Object create() {        return new SimTCPConnection();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -