Java源码示例:org.apache.mina.transport.socket.DefaultSocketSessionConfig

示例1
private ServerSocketChannel open(InetSocketAddress localAddress) throws IOException {
	if (localAddress == null)
		return null;
	ServerSocketChannel channel = ServerSocketChannel.open();
	try {
		channel.configureBlocking(false);
		channel.setOption(StandardSocketOptions.SO_REUSEADDR, Boolean.valueOf(reuseAddress));
		DefaultSocketSessionConfig config = getSessionConfig();
		if (config.getSendBufferSize() >= 0)
			channel.setOption(StandardSocketOptions.SO_SNDBUF, config.getSendBufferSize());
		if (config.getReceiveBufferSize() >= 0)
			channel.setOption(StandardSocketOptions.SO_RCVBUF, config.getReceiveBufferSize());
		try {
			channel.bind(localAddress, backlog);
		} catch (IOException ioe) {
			close(channel);
			throw new IOException("error while binding on " + localAddress, ioe);
		}
		channel.register(selector, SelectionKey.OP_ACCEPT);
		return channel;
	} catch (Throwable e) {
		close(channel);
		throw e;
	}
}
 
示例2
private void bindWebPort() throws IOException {

		/*
		 * 预制websocket握手请求的处理
		 */
		INNER_HANDLER_MAP.put(CIMConstant.CLIENT_WEBSOCKET_HANDSHAKE, new WebsocketHandler());

		webAcceptor = new NioSocketAcceptor();
		((DefaultSocketSessionConfig) webAcceptor.getSessionConfig()).setKeepAlive(true);
		((DefaultSocketSessionConfig) webAcceptor.getSessionConfig()).setTcpNoDelay(true);
		webAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new WebMessageCodecFactory()));
		webAcceptor.getFilterChain().addLast("logger", new LoggingFilter());
		webAcceptor.getFilterChain().addLast("executor", new ExecutorFilter(createWorkerExecutor()));
		webAcceptor.setHandler(this);

		webAcceptor.bind(new InetSocketAddress(webPort));
		String logBanner = "\n\n" +
				"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
				"*                                                                                   *\n" +
				"*                                                                                   *\n" +
				"*                   Websocket Server started on port {}.                         *\n" +
				"*                                                                                   *\n" +
				"*                                                                                   *\n" +
				"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n";
		LOGGER.info(logBanner, webPort);
	}
 
示例3
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
示例4
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
示例5
private void encodeAndDecodeProxyMessage(XinqiProxyMessage response, boolean checkDecoder) throws Exception {
	ProtobufEncoder encoder = new ProtobufEncoder();
	ProtobufDecoder decoder = new ProtobufDecoder();
	final ArrayList<Object> results = new ArrayList<Object>();
	
	IoSession session = createNiceMock(IoSession.class);
	expect(session.getTransportMetadata()).andReturn(
			new DefaultTransportMetadata("testprovider", "default", 
					false, true, InetSocketAddress.class, DefaultSocketSessionConfig.class, 
					SessionMessage.class)).anyTimes();
	
	IoBuffer buffer = (IoBuffer)ProtobufEncoder.encodeXinqiProxyMessage(response);
	
	ProtocolDecoderOutput deout = createNiceMock(ProtocolDecoderOutput.class);
	if ( checkDecoder ) {
		deout.write(anyObject());
		expectLastCall().andAnswer(new IAnswer<Object>() {
			@Override
			public Object answer() throws Throwable {
				results.add(getCurrentArguments()[0]);
				return null;
			}
		}).times(1);
	}
	
	replay(session);
	replay(deout);
	
	decoder.decode(session, buffer, deout);
	
	verify(session);
	verify(deout);
	
	if ( checkDecoder ) {
		XinqiProxyMessage decodeMsg = (XinqiProxyMessage)results.get(0);
		assertEquals(response.userSessionKey, decodeMsg.userSessionKey);
		assertEquals(response.xinqi.payload.getClass(), decodeMsg.xinqi.payload.getClass());
	}
}
 
示例6
private void bindAppPort() throws IOException {


		appAcceptor = new NioSocketAcceptor();
		((DefaultSocketSessionConfig) appAcceptor.getSessionConfig()).setKeepAlive(true);
		((DefaultSocketSessionConfig) appAcceptor.getSessionConfig()).setTcpNoDelay(true);

		KeepAliveFilter keepAliveFilter = new KeepAliveFilter(this, IdleStatus.BOTH_IDLE);
		keepAliveFilter.setRequestInterval(IDLE_HEART_REQUEST_TIME);
		keepAliveFilter.setRequestTimeout(HEART_RESPONSE_TIME_OUT);
		keepAliveFilter.setForwardEvent(true);

		appAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new AppMessageCodecFactory()));
		appAcceptor.getFilterChain().addLast("logger", new LoggingFilter());
		appAcceptor.getFilterChain().addLast("heartbeat", keepAliveFilter);
		appAcceptor.getFilterChain().addLast("executor", new ExecutorFilter(createWorkerExecutor()));
		appAcceptor.setHandler(this);

		appAcceptor.bind(new InetSocketAddress(appPort));
		String logBanner = "\n\n" +
				"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" +
				"*                                                                                   *\n" +
				"*                                                                                   *\n" +
				"*                   App Socket Server started on port {}.                        *\n" +
				"*                                                                                   *\n" +
				"*                                                                                   *\n" +
				"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n";
		LOGGER.info(logBanner, appPort);
	}
 
示例7
/**
 * Creates a new proxy connector.
 */
public ProxyConnector() {
    super(new DefaultSocketSessionConfig(), null);
}
 
示例8
/**
 * Constructor for {@link NioSocketConnector} with default configuration (multiple thread model).
 */
public NioSocketConnector() {
    super(new DefaultSocketSessionConfig(), NioProcessor.class);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例9
/**
 * Constructor for {@link NioSocketAcceptor} using default parameters (multiple thread model).
 */
public NioSocketAcceptor() {
    super(new DefaultSocketSessionConfig(), NioProcessor.class);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例10
private SessionAIMessage encodeAndDecode(SessionAIMessage sessionMessage) throws Exception {
	
	AIProtobufEncoder encoder = new AIProtobufEncoder();
	AIProtobufDecoder decoder = new AIProtobufDecoder();
	final ArrayList<Object> results = new ArrayList<Object>();
	
	IoSession session = createNiceMock(IoSession.class);
	expect(session.getTransportMetadata()).andReturn(
			new DefaultTransportMetadata("testprovider", "default", 
					false, true, InetSocketAddress.class, DefaultSocketSessionConfig.class, 
					SessionMessage.class)).anyTimes();
	
	ProtocolEncoderOutput out = createNiceMock(ProtocolEncoderOutput.class);
	out.write(anyObject());
	expectLastCall().andAnswer(new IAnswer<Object>() {
		@Override
		public Object answer() throws Throwable {
			results.add(getCurrentArguments()[0]);
			return null;
		}
	}).anyTimes();
	
	replay(session);
	replay(out);
	
	encoder.encode(session, sessionMessage, out);
	
	verify(session);
	verify(out);
	
	assertTrue(results.get(0) instanceof IoBuffer );
	
	IoBuffer buffer = (IoBuffer)results.get(0);
	results.remove(0);
	
	ProtocolDecoderOutput deout = createNiceMock(ProtocolDecoderOutput.class);
	deout.write(anyObject());
	expectLastCall().andAnswer(new IAnswer<Object>() {
		@Override
		public Object answer() throws Throwable {
			results.add(getCurrentArguments()[0]);
			return null;
		}
	}).times(1);
	replay(deout);
	
	decoder.decode(session, buffer, deout);
	
	verify(deout);
	
	SessionAIMessage decodeMsg = (SessionAIMessage)results.get(0);
	return decodeMsg;
}
 
示例11
private void encodeAndDecode(XinqiMessage response, boolean checkDecoder) throws Exception {
	ProtobufEncoder encoder = new ProtobufEncoder();
	ProtobufDecoder decoder = new ProtobufDecoder();
	final ArrayList<Object> results = new ArrayList<Object>();
	
	IoSession session = createNiceMock(IoSession.class);
	expect(session.getTransportMetadata()).andReturn(
			new DefaultTransportMetadata("testprovider", "default", 
					false, true, InetSocketAddress.class, DefaultSocketSessionConfig.class, 
					SessionMessage.class)).anyTimes();
	
	ProtocolEncoderOutput out = createNiceMock(ProtocolEncoderOutput.class);
	out.write(anyObject());
	expectLastCall().andAnswer(new IAnswer<Object>() {
		@Override
		public Object answer() throws Throwable {
			results.add(getCurrentArguments()[0]);
			return null;
		}
	});
	
	replay(session);
	replay(out);
	
	encoder.encode(session, response, out);
	
	verify(session);
	verify(out);
	
	assertTrue(results.get(0) instanceof IoBuffer );
	
	IoBuffer buffer = (IoBuffer)results.get(0);
	results.remove(0);
	
	ProtocolDecoderOutput deout = createNiceMock(ProtocolDecoderOutput.class);
	if ( checkDecoder ) {
		deout.write(anyObject());
		expectLastCall().andAnswer(new IAnswer<Object>() {
			@Override
			public Object answer() throws Throwable {
				results.add(getCurrentArguments()[0]);
				return null;
			}
		}).times(1);
	}
	replay(deout);
	
	decoder.decode(session, buffer, deout);
	
	verify(deout);
	
	if ( checkDecoder ) {
		XinqiMessage decodeMsg = (XinqiMessage)results.get(0);
		assertEquals(response.payload.getClass(), decodeMsg.payload.getClass());
	}
}
 
示例12
/**
 * 获取用于服务器端的mina网络配置并可以修改
 */
public final DefaultSocketSessionConfig getServerConfig()
{
	return getAcceptor().getSessionConfig();
}
 
示例13
/**
 * 获取用于客户端的mina网络配置并可以修改
 */
public final DefaultSocketSessionConfig getClientConfig()
{
	return getConnector().getSessionConfig();
}
 
示例14
@Override
public final DefaultSocketSessionConfig getSessionConfig() {
	return sessionConfig;
}
 
示例15
/**
 * Creates a new proxy connector.
 * 
 * @param connector Connector used to establish proxy connections.
 */
public ProxyConnector(final SocketConnector connector) {
    this(connector, new DefaultSocketSessionConfig(), null);
}
 
示例16
/**
 * Constructor for {@link NioSocketConnector} with default configuration, and 
 * given number of {@link NioProcessor} for multithreading I/O operations
 * @param processorCount the number of processor to create and place in a
 * {@link SimpleIoProcessorPool} 
 */
public NioSocketConnector(int processorCount) {
    super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例17
/**
 *  Constructor for {@link NioSocketConnector} with default configuration but a
 *  specific {@link IoProcessor}, useful for sharing the same processor over multiple
 *  {@link IoService} of the same type.
 * @param processor the processor to use for managing I/O events
 */
public NioSocketConnector(IoProcessor<NioSession> processor) {
    super(new DefaultSocketSessionConfig(), processor);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例18
/**
 *  Constructor for {@link NioSocketConnector} with a given {@link Executor} for handling 
 *  connection events and a given {@link IoProcessor} for handling I/O events, useful for sharing 
 *  the same processor and executor over multiple {@link IoService} of the same type.
 * @param executor the executor for connection
 * @param processor the processor for I/O operations
 */
public NioSocketConnector(Executor executor, IoProcessor<NioSession> processor) {
    super(new DefaultSocketSessionConfig(), executor, processor);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例19
/**
 * Constructor for {@link NioSocketConnector} with default configuration which will use a built-in 
 * thread pool executor to manage the given number of processor instances. The processor class must have 
 * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
 * no-arg constructor.
 * 
 * @param processorClass the processor class.
 * @param processorCount the number of processors to instantiate.
 * @see org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
 * @since 2.0.0-M4
 */
public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount) {
    super(new DefaultSocketSessionConfig(), processorClass, processorCount);
}
 
示例20
/**
 * Constructor for {@link NioSocketConnector} with default configuration with default configuration which will use a built-in 
 * thread pool executor to manage the default number of processor instances. The processor class must have 
 * a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a 
 * no-arg constructor. The default number of instances is equal to the number of processor cores 
 * in the system, plus one.
 * 
 * @param processorClass the processor class.
 * @see org.apache.mina.core.service.SimpleIoProcessorPool#SimpleIoProcessorPool(Class, Executor, int)
 * @see org.apache.mina.core.service.SimpleIoProcessorPool#DEFAULT_SIZE
 * @since 2.0.0-M4
 */
public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass) {
    super(new DefaultSocketSessionConfig(), processorClass);
}
 
示例21
/**
 * Constructor for {@link NioSocketAcceptor} using default parameters, and 
 * given number of {@link NioProcessor} for multithreading I/O operations.
 * 
 * @param processorCount the number of processor to create and place in a
 * {@link SimpleIoProcessorPool} 
 */
public NioSocketAcceptor(int processorCount) {
    super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例22
/**
*  Constructor for {@link NioSocketAcceptor} with default configuration but a
 *  specific {@link IoProcessor}, useful for sharing the same processor over multiple
 *  {@link IoService} of the same type.
 * @param processor the processor to use for managing I/O events
 */
public NioSocketAcceptor(IoProcessor<NioSession> processor) {
    super(new DefaultSocketSessionConfig(), processor);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例23
/**
 *  Constructor for {@link NioSocketAcceptor} with a given {@link Executor} for handling 
 *  connection events and a given {@link IoProcessor} for handling I/O events, useful for 
 *  sharing the same processor and executor over multiple {@link IoService} of the same type.
 * @param executor the executor for connection
 * @param processor the processor for I/O operations
 */
public NioSocketAcceptor(Executor executor, IoProcessor<NioSession> processor) {
    super(new DefaultSocketSessionConfig(), executor, processor);
    ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
 
示例24
/**
 * @return the default configuration of the new {@link IoSession}s created by this service.
 */
DefaultSocketSessionConfig getSessionConfig();