本文整理匯總了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));
}