本文整理汇总了Java中org.apache.mina.core.service.IoAcceptor类的典型用法代码示例。如果您正苦于以下问题:Java IoAcceptor类的具体用法?Java IoAcceptor怎么用?Java IoAcceptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IoAcceptor类属于org.apache.mina.core.service包,在下文中一共展示了IoAcceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// 这个过滤器用来记录所有的信息,比如创建session(会话),接收消息,发送消息,关闭会话等
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 用来转换二进制或协议的专用数据到消息对象中
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 实时处理客户端的连接和请求
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
// 方法将定时调用一次会话,保持空闲状态。来设定时间间隔。
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}
示例2: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
// 创建服务器端的监听器对象
IoAcceptor acceptor = new NioSocketAcceptor();
// 增加日志过滤器:用于日志存储
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 增加消息编码过滤器,采用UTF-8编码
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 设置具体的事物逻辑处理器
acceptor.setHandler(new TimeServerHandler());
// 设置IoSession的一些属性
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 设置服务器监听的端口
acceptor.bind(new InetSocketAddress(PORT));
}
示例3: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDELTIMEOUT);
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("myfliter", new MyFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));
KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE);
/** 是否回发 */
kaf.setForwardEvent(true);
acceptor.getFilterChain().addLast("heart", kaf);
acceptor.setHandler(new CalculatorHandler());
acceptor.bind(new InetSocketAddress(PORT));
log.debug("socket通信服务端已启动,端口是" + PORT);
}
示例4: getSessionInfo
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
/**
* An extended toString() method for sessions.
* If the SSL handshake is not yet completed, we will print (ssl) in small caps.
* Once it's completed, we will use SSL capitalized.
*/
public static String getSessionInfo(IoSession session) {
StringBuilder sb = new StringBuilder();
sb.append(session.getService() instanceof IoAcceptor ? "Session Server" : "Session Client");
sb.append('[').append(session.getId()).append(']');
SslHandler sslHandler = (SslHandler) session.getAttribute(SSL_HANDLER);
if (sslHandler == null) {
sb.append("(no sslEngine)");
} else if (sslHandler.isOutboundDone()) {
sb.append("(done)");
} else {
sb.append(sslHandler.isHandshakeComplete() ? "(SSL)" : "(SSL...)");
}
return sb.toString();
}
示例5: stopReceivers
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
/**
* Stops any currently running listeners. This method can be blocked by the sendMessage() method.
* Once all messages have been sent the listeners can be stopped.
*/
protected static void stopReceivers() {
// Dispose of any active listeners
for (Map.Entry<InetSocketAddress,NioSocketAcceptor> entry : receivers.entrySet()) {
IoAcceptor acceptor = entry.getValue();
ServerHandler handler = (ServerHandler)acceptor.getHandler();
try {
while (handler.isStillWaitingToSendMessages()) {
Thread.sleep(500L);
}
} catch (InterruptedException ex) {
logger.error(ex.getMessage(), ex);
}
acceptor.unbind();
acceptor.dispose();
logger.info("Receiver stopped on port {}", entry.getKey());
}
}
示例6: StringBuilder
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
/**
* An extended toString() method for sessions. If the SSL handshake
* is not yet completed, we will print (ssl) in small caps. Once it's
* completed, we will use SSL capitalized.
*/
/* no qualifier */String getSessionInfo(IoSession session) {
StringBuilder sb = new StringBuilder();
if (session.getService() instanceof IoAcceptor) {
sb.append("Session Server");
} else {
sb.append("Session Client");
}
sb.append('[').append(session.getId()).append(']');
SslHandler handler = (SslHandler) session.getAttribute(SSL_HANDLER);
if (handler == null) {
sb.append("(no sslEngine)");
} else if (isSslStarted(session)) {
if (handler.isHandshakeComplete()) {
sb.append("(SSL)");
} else {
sb.append("(ssl...)");
}
}
return sb.toString();
}
示例7: getServiceAddress
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public SocketAddress getServiceAddress() {
IoService service = getService();
if (service instanceof IoAcceptor) {
return ((IoAcceptor) service).getLocalAddress();
}
return getRemoteAddress();
}
示例8: getLocalInterfaces
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static Set<ConnectionInformation> getLocalInterfaces ( final IoAcceptor acceptor, final ConnectionInformation connectionInformation ) throws SocketException
{
final Set<ConnectionInformation> result = new HashSet<ConnectionInformation> ();
for ( final InetSocketAddress address : getLocalAddresses ( acceptor ) )
{
result.add ( cloneFill ( address.getAddress (), connectionInformation ) );
}
return result;
}
示例9: getLocalAddresses
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static Set<InetSocketAddress> getLocalAddresses ( final IoAcceptor acceptor ) throws SocketException
{
final Set<InetSocketAddress> result = new HashSet<InetSocketAddress> ();
for ( final SocketAddress address : acceptor.getLocalAddresses () )
{
logger.info ( "Bound to: {}", address );
if ( ! ( address instanceof InetSocketAddress ) )
{
continue;
}
final InetSocketAddress socketAddress = (InetSocketAddress)address;
if ( socketAddress.getAddress ().isAnyLocalAddress () )
{
final int port = socketAddress.getPort ();
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces ();
while ( interfaces.hasMoreElements () )
{
final NetworkInterface networkInterface = interfaces.nextElement ();
for ( final InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses () )
{
result.add ( new InetSocketAddress ( interfaceAddress.getAddress (), port ) );
}
}
}
else
{
result.add ( socketAddress );
}
}
return result;
}
示例10: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));// 指定编码过滤器
acceptor.setHandler(new TimeServerHandler());// 指定业务逻辑处理器
//读写 通道均在3 秒内无任何操作就进入空闲状态
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3);
acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));// 设置端口号
acceptor.bind();// 启动监听
}
示例11: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.setHandler(new Mina2TesterServerHandler());
try {
acceptor.bind(new InetSocketAddress(7890));
} catch (Exception e) {
System.out.println("Config Error:" + e);
}
System.out.println("监听中...");
}
示例12: printInfos
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
private void printInfos() {
if(Server.getServer() == null){
return;
}
IoAcceptor ioAcceptor = Server.getServer().getIoAcceptor();
int playerSize = this.getPlayers().size();
TestLogger.info("服务器状态 players:[" + playerSize + "] " + "connect:[" + ioAcceptor.getManagedSessionCount() + "]");
System.out.println(ioAcceptor.toString());
}
示例13: tryToInitializeAndBindSensorAcceptor
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
private void tryToInitializeAndBindSensorAcceptor(String protocol, InetSocketAddress s, Map<String, String> aAcceptors, Executor aExecutor) throws IOException, Exception {
if (acceptorsFactory != null && acceptorsFactory.isSupported(protocol)) {
String acceptorModuleName = aAcceptors.get(protocol);
if (acceptorModuleName == null) {
acceptorModuleName = aAcceptors.get(null);
}
if (acceptorModuleName != null) {
Integer sessionIdleTime = portsSessionIdleTimeouts != null ? portsSessionIdleTimeouts.get(s.getPort()) : null;
if (sessionIdleTime == null || sessionIdleTime == 0) {
sessionIdleTime = 360;
}
Integer sessionIdleCheckInterval = portsSessionIdleCheckIntervals != null ? portsSessionIdleCheckIntervals.get(s.getPort()) : null;
if (sessionIdleCheckInterval == null || sessionIdleCheckInterval == 0) {
sessionIdleCheckInterval = 360;
}
IoAcceptor sensorAcceptor = acceptorsFactory.create(protocol, aExecutor, sessionIdleTime, sessionIdleCheckInterval, new com.eas.server.handlers.PositioningPacketReciever(this, acceptorModuleName, retranslateFactory));
if (sensorAcceptor != null) {
sensorAcceptor.bind(s);
Logger.getLogger(PlatypusServer.class.getName()).log(Level.INFO, "Listening {0} protocol on {1}", new Object[]{protocol, s.toString()});
}
} else {
Logger.getLogger(PlatypusServer.class.getName()).log(Level.INFO, "Acceptor module was not found for {0} protocol", protocol);
}
} else {
Logger.getLogger(PlatypusServer.class.getName()).log(Level.INFO, "{0} protocol is not supported", protocol);
}
}
示例14: initializeAndBindPlatypusAcceptor
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
private void initializeAndBindPlatypusAcceptor(InetSocketAddress s) throws IOException, Exception {
//final SslFilter sslFilter = new SslFilter(sslContext); commented out until MINA Sslfilter bugs will be fixed
ThreadPoolExecutor connectionsPollerExecutor = new ThreadPoolExecutor(1, 1,
3L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new PlatypusThreadFactory("nio-polling-", false));
final IoAcceptor acceptor = new NioSocketAcceptor(connectionsPollerExecutor, new NioProcessor(executor));
//acceptor.getFilterChain().addLast("encryption", sslFilter); commented out until MINA Sslfilter bugs will be fixed
acceptor.getFilterChain().addLast("platypusCodec", new ProtocolCodecFilter(new ResponseEncoder(), new RequestDecoder()));
/*
acceptor.getFilterChain().addLast("executor", new ExecutorFilter(executor, IoEventType.EXCEPTION_CAUGHT,
IoEventType.MESSAGE_RECEIVED, IoEventType.MESSAGE_SENT, IoEventType.SESSION_CLOSED, IoEventType.SESSION_IDLE, IoEventType.CLOSE, IoEventType.WRITE));
*/
PlatypusRequestsHandler handler = new PlatypusRequestsHandler(this);
acceptor.setHandler(handler);
Integer sessionIdleTime = portsSessionIdleTimeouts != null ? portsSessionIdleTimeouts.get(s.getPort()) : null;
if (sessionIdleTime == null || sessionIdleTime == 0) {
sessionIdleTime = PlatypusRequestsHandler.SESSION_TIME_OUT;
}
Integer sessionIdleCheckInterval = portsSessionIdleCheckIntervals != null ? portsSessionIdleCheckIntervals.get(s.getPort()) : null;
if (sessionIdleCheckInterval == null || sessionIdleCheckInterval == 0) {
sessionIdleCheckInterval = PlatypusRequestsHandler.IDLE_TIME_EVENT;
}
handler.setSessionIdleCheckInterval(sessionIdleCheckInterval);
handler.setSessionIdleTime(sessionIdleTime);
acceptor.bind(s);
Logger.getLogger(ServerMain.class.getName()).log(Level.INFO, "Listening platypus protocol on {0}", s.toString());
}
示例15: main
import org.apache.mina.core.service.IoAcceptor; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
IoAcceptor acceptor = new NioSocketAcceptor(); //TCP Acceptor
acceptor.getFilterChain().addLast("logging", new LoggingFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter());
acceptor.setHandler(new HelloServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}