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

📄 blockingsessionacceptor.java

📁 银行项目为后台socket通信写的程序
💻 JAVA
字号:
/****************************************************************************
 * Package		: com.ecSolutions.ecAppServer.server.session.bio
 * File			: BlockingSessionAcceptor.java
 * Create Date  : 2007-7-20
 * Author		: Steven Chen
 * 
 * Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
 *			
 ***************************************************************************/
package com.ecSolutions.ecAppServer.server.session.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import com.ecSolutions.ecAppServer.server.Session;
import com.ecSolutions.ecAppServer.server.SessionType;
import com.ecSolutions.ecAppServer.server.session.AbstractSessionAcceptor;
import com.ecSolutions.ecAppServer.server.session.nio.SocketChannelSession;
import com.ecSolutions.ecAppServer.server.util.ChannelUtils;
import com.ecSolutions.ecAppServer.server.util.LogThreadGroup;
import com.ecSolutions.ecAppServer.server.util.NamedThreadFactory;
import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;

/**
 * Blocking session acceptor.
 * 
 * @author Steven Chen
 * @version $Id: BlockingSessionAcceptor.java,v 1.2 2007/07/26 03:35:13 stevenchen Exp $
 */
public class BlockingSessionAcceptor extends AbstractSessionAcceptor {

    private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory(
            new LogThreadGroup("SessionAcceptor"), false, "SessionAcceptor");

    private final AtomicInteger counter = new AtomicInteger();

    private ServerSocketChannel channel;
    private Thread thread;

    public SessionType getSessionType() {
        return SessionType.TCP;
    }

    public boolean isStarted() {
        return thread != null;
    }

    public SocketAddress getListenAddress() {
        if (isStarted())
            return channel.socket().getLocalSocketAddress();
        return super.getListenAddress();
    }

    public int getListenPort() {
        if (isStarted())
            return channel.socket().getLocalPort();
        return super.getListenPort();
    }

    public ServerSocket getSocket() {
        return isStarted() ? channel.socket() : null;
    }

    public synchronized void start() {
        if (getAcceptorHandler() == null)
            throw new IllegalStateException("acceptor handler is null");
        if (isStarted())
            return;
        try {
            channel = ServerSocketChannel.open();
            setServerSocketOptions(channel.socket());
            counter.set(0);

            thread = THREAD_FACTORY.newThread(new Runnable() {

                public void run() {
                    while (true) {
                        try {
                            Session session = accept();
                            if (session == null)
                                break;
                            sessionAccepted(session);
                        } catch (IOException e) {
                            exceptionCaught(e);
                        }
                    }
                }
            });
            thread.start();
        } catch (Throwable e) {
            exceptionCaught(e);
            close();
        }
    }

    private Session accept() throws IOException {
        SocketChannel sc = null;
        try {
            sc = channel.accept();
        } catch (ClosedChannelException e) {
            return null;
        }
        counter.incrementAndGet();
        setSocketOptions(sc.socket());
        return newSession(sc);
    }

    public int getAcceptedCount() {
        return counter.get();
    }

    protected Session newSession(SocketChannel sc) {
        SocketChannelSession session = new SocketChannelSession();
        session.setChannel(sc);
        return session;
    }

    public synchronized void close() {
        ChannelUtils.close(channel);
        if (thread != null && thread != Thread.currentThread()) {
            while (thread.isAlive())
                try {
                    thread.join();
                } catch (InterruptedException e) {
                }
        }
        channel = null;
    }
}

⌨️ 快捷键说明

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