本文整理匯總了Java中org.apache.mina.core.future.ConnectFuture.isConnected方法的典型用法代碼示例。如果您正苦於以下問題:Java ConnectFuture.isConnected方法的具體用法?Java ConnectFuture.isConnected怎麽用?Java ConnectFuture.isConnected使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.future.ConnectFuture
的用法示例。
在下文中一共展示了ConnectFuture.isConnected方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
* connects to a LLRP device at the host address and port specified. the connect method waits
* for the timeperiod specified (in ms) for a response. If the READER_NOTIFICATION does not arrive
* or the ConnectionAttemptEventStatus
* is not set to 'Success', a LLRPConnectionAttemptFailedException is thrown.
*
* @param timeout time in ms
* @throws LLRPConnectionAttemptFailedException
*/
public void connect(long timeout) throws LLRPConnectionAttemptFailedException{
connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new LLRPProtocolCodecFactory(LLRPProtocolCodecFactory.BINARY_ENCODING)));
// MINA 2.0 method
connector.setHandler(handler);
remoteAddress = new InetSocketAddress(host, port);
ConnectFuture future = connector.connect(remoteAddress);//.connect(remoteAddress,handler);
future.join();// Wait until the connection attempt is finished.
if(future.isConnected()){
session = future.getSession();
}else{
String msg = "failed to connect";
throw new LLRPConnectionAttemptFailedException(msg);
}
// MINA 2.0
//future.awaitUninterruptibly();
//check if llrp reader reply with a status report to indicate connection success.
//the client shall not send any information to the reader until this status report message is received
checkLLRPConnectionAttemptStatus(timeout);
}
示例2: run
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
* 守護,啟動鏈接、斷線重連
*/
@Override
public void run() {
while (true) {
Set<IoConnector> keySet = ioConnectorMap.keySet();
Iterator<IoConnector> iterator = keySet.iterator();
for (int i = 0; i < keySet.size(); i++) {
IoConnector ioConnector = iterator.next();
InetSocketAddress inetSocketAddress = ioConnectorMap.get(ioConnector);
boolean isConnected = ioConnectorStateMap.get(ioConnector);
if (!isConnected) {
ConnectFuture connectFuture = ioConnector.connect(inetSocketAddress);
connectFuture.awaitUninterruptibly();
if (!connectFuture.isConnected()) {
connectFuture.cancel();
if (MinaConfig.log != null) {
MinaConfig.log.info("連接" + inetSocketAddress.toString() + "失敗");
}
} else {
ioConnectorStateMap.put(ioConnector, true);
if (MinaConfig.log != null) {
MinaConfig.log.info("連接" + inetSocketAddress.toString() + "成功");
}
}
}
}
try {
Thread.sleep(MinaClient.MINA_CLIENT_RECONNECT_INTERVAL);
} catch (InterruptedException e) {
if (MinaConfig.log != null) {
MinaConfig.log.error("守護線程minaclient異常", e);
}
}
}
}
示例3: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public IoSession connect() throws Exception {
ConnectFuture future = connector.connect(new InetSocketAddress((String)rule.get("ip"), (Integer)rule.get("port")));
future.awaitUninterruptibly();
/*boolean b = future.awaitUninterruptibly(cfg.getConTimeout());
// 若返回值b==true,則表示設置了ConnectFuture的value,即調用了ConnectFuture的setException、setSession或cancel方法
if (!b)
throw new CommunicateException("連接遠程Tcp服務器超時");*/
if (!future.isConnected())
// 即getValue() instanceof IoSession == false,也就是說出現異常或Canceled
throw new Exception("與遠程Tcp服務器建立連接失敗:超時或異常",
future.getException());
return future.getSession();
}
示例4: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public MessageSession connect(SocketAddress address) throws IOException {
Objects.requireNonNull(address);
// client session
ConnectFuture connectFuture = connector.connect(address);
connectFuture.awaitUninterruptibly();
if (!connectFuture.isConnected() || connectFuture.getException() != null)
throw new IOException("Connection error", connectFuture.getException());
IoSession minaSession = connectFuture.getSession();
return minaAdapter.getMessageSession(minaSession);
}
示例5: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
* Connects to the server.
* @param password The password.
* @param node The node id.
*/
public void connect(final String password, final int node) {
this.password = password;
this.node = node;
logger.info("Connecting to login server : " + address + ":" + CommonConstants.LOGIN_PORT + "...");
ConnectFuture cf = connector.connect(new InetSocketAddress(address, CommonConstants.LOGIN_PORT));
cf.awaitUninterruptibly();
if(!cf.isConnected() && (session == null || !session.isConnected())) {
logger.severe("Connection to login server failed. Retrying...");
// this stops stack overflow errors
World.getWorld().getEngine().submitLogic(new Runnable() {
public void run() {
World.getWorld().getLoginServerConnector().connect(password, node);
}
});
} else {
this.session = cf.getSession();
logger.info("Connected.");
session.getFilterChain().addFirst("protocolCodecFilter", new ProtocolCodecFilter(new LoginCodecFactory()));
// create and send auth packet
IoBuffer buf = IoBuffer.allocate(16);
buf.setAutoExpand(true);
buf.putShort((short) node);
IoBufferUtils.putRS2String(buf, password);
buf.flip();
this.session.write(new LoginPacket(LoginPacket.AUTH, buf));
}
}
示例6: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public boolean connect(NioSocketConnector connector, SocketAddress address) {
if(!isSetChain){
throw new IllegalStateException(
"please set ConservationChain first !");
}
if (session != null && session.isConnected()) {
throw new IllegalStateException(
"Already connected. Disconnect first.");
}
try {
IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new GameProtocolcodecFactory());
connector.getFilterChain().addLast("codec", CODEC_FILTER);
connector.setHandler(handler);
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return false;
}
session = future1.getSession();
return true;
} catch (Exception e) {
return false;
}
}
示例7: operationComplete
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
@Override
public void operationComplete(ConnectFuture future) {
if (!future.isConnected()) {
connector.dispose(false);
return;
}
final IoSession session = future.getSession();
try {
// This does "real" I/O, so can really throw an exception.
transfer.open(session);
} catch (Exception e) {
session.close(true);
}
}
示例8: reconnect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
* reconnect to LLRP device using host, port and handler specified.
*
* @return true if connection could be established
* and ConnectionAttemptEvent Status was set to 'Success', set to false otherwise
*
*/
public boolean reconnect() {
ConnectFuture future = connector.connect(remoteAddress);//,handler);
connector.setHandler(handler);
future.join(); // Wait until the connection attempt is finished.
// MINA 2.0
// future = connector.connect();
// future.awaitUninterruptibly();
if(future.isConnected()){
session = future.getSession();
log.info("new session created:" + session);
} else {
return false;
}
//check if llrp reader reply with a status report to indicate connection success.
//the client shall not send any information to the reader until this status report message is received
try {
checkLLRPConnectionAttemptStatus(CONNECT_TIMEOUT);
} catch (LLRPConnectionAttemptFailedException e) {
return false;
}
return true;
}
示例9: run
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
@Override
public void run() {
IoConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);//設置連接超時時間(毫秒數)
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));
KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.READER_IDLE, KeepAliveRequestTimeoutHandler.CLOSE);
/** 是否回發 */
kaf.setForwardEvent(true);
connector.getFilterChain().addLast("heart", kaf);
connector.setHandler(new CalculatorClientHander());
ConnectFuture connectFuture = connector.connect(new InetSocketAddress(IP, PORT));
//等待建立連接
connectFuture.awaitUninterruptibly();
if(!connectFuture.isConnected()){
log.debug("連接失敗");
return ;
}
log.debug("連接成功");
IoSession session = connectFuture.getSession();
// try {
// Cmd1003 cmd1003 = (Cmd1003) CommandFactory.createCommand(CidConst.C1003);
// cmd1003.getReqMsg().setCpu(0.3f);
// cmd1003.getReqMsg().setDisk(0.24f);
// cmd1003.getReqMsg().setMemory(0.41f);
// session.write(cmd1003);
// } catch (Exception e) {
// e.printStackTrace();
// }
//關閉
if (session != null) {
if (session.isConnected()) {
session.getCloseFuture().awaitUninterruptibly();
}
connector.dispose(true);
}
}
示例10: operationComplete
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
@Override
public void operationComplete(ConnectFuture cfut) {
if (!cfut.isConnected()){
send("CONNERR", cfut.getException());
}
}
示例11: init
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public void init() {
IoConnector connector = new NioSocketConnector();
connector.getSessionConfig().setReadBufferSize(2048);
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
connector.setHandler(new MinaChatClientHandler(listener));
ConnectFuture future = connector.connect(new InetSocketAddress(host, port));
future.awaitUninterruptibly();
if (!future.isConnected()) {
return;
}
session = future.getSession();
}
示例12: connectAndSend
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
* Default constructor.
*/
public void connectAndSend() {
LOGGER.debug(ipAddress +":"+ portNo);
SocketAddress address = parseSocketAddress(ipAddress +":"+ portNo);
LOGGER.debug("UDPClient::UDPClient");
LOGGER.debug("Created a datagram connector");
connector = new NioDatagramConnector();
LOGGER.debug("Setting the handler");
connector.setHandler(this);
IoFilter LOGGING_FILTER = new LoggingFilter();
IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new TextLineCodecFactory());
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getFilterChain().addLast("logger", LOGGING_FILTER);
LOGGER.debug("About to connect to the server...");
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return;
}
session = future1.getSession();
// try {
//sendData(fileSignature);
/*
UdpMessage m = new UdpMessage();
m.setSequence(0);
m.setFileSignature(fileSignature);
session.write(m);*/
RequestSignatureMessage signatureMessage = new RequestSignatureMessage();
FileSignature fileSignature;
try {
fileSignature = new FileSignature(query);
signatureMessage.setSequence(0);
signatureMessage.setFileSignature(fileSignature);
signatureMessage.setMatches(matches);
System.out.println("Sending the query: " + query);
System.out.println("[PeerlessSendUdpMessageSupport] file signature" + fileSignature);
session.write(signatureMessage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//fileSignature.setFileSignature(query);
}
示例13: connect
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public boolean connect(NioSocketConnector connector, SocketAddress address,
boolean useSsl) {
if (session != null && session.isConnected()) {
throw new IllegalStateException(
"Already connected. Disconnect first.");
}
try {
IoFilter LOGGING_FILTER = new LoggingFilter();
IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new TextLineCodecFactory());
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
//connector.getFilterChain().addLast("codec", CODEC_FILTER);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getFilterChain().addLast("logger", LOGGING_FILTER);
if (useSsl) {
SSLContext sslContext = BogusSslContextFactory
.getInstance(false);
SslFilter sslFilter = new SslFilter(sslContext);
sslFilter.setUseClientMode(true);
connector.getFilterChain().addFirst("sslFilter", sslFilter);
}
connector.setHandler(handler);
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return false;
}
session = future1.getSession();
login();
return true;
} catch (Exception e) {
return false;
}
}
示例14: register
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
public boolean register(NioSocketConnector connector, SocketAddress address,
String name, String id, String password, String question,
String passwordReminder, boolean useSsl) {
System.out.println("This is address: " + address);
if (session != null && session.isConnected()) {
throw new IllegalStateException(
"Already connected. Disconnect first.");
}
try {
IoFilter LOGGING_FILTER = new LoggingFilter();
IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new TextLineCodecFactory());
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
//connector.getFilterChain().addLast("codec", CODEC_FILTER);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getFilterChain().addLast("logger", LOGGING_FILTER);
if (useSsl) {
SSLContext sslContext = BogusSslContextFactory
.getInstance(false);
SslFilter sslFilter = new SslFilter(sslContext);
sslFilter.setUseClientMode(true);
connector.getFilterChain().addFirst("sslFilter", sslFilter);
}
connector.setHandler(handler);
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return false;
}
session = future1.getSession();
register m = new register();
m.setSequence(0);
m.setCommand("REGISTER");
m.setUserId(id);
m.setpassword(password);
m.setQuestion(question);
m.setpasswordReminder(passwordReminder);
session.write(m);
/*session.write("REGISTER " + id + CommonConfig.delimeter + name
+ CommonConfig.delimeter + password
+ CommonConfig.delimeter + question
+ CommonConfig.delimeter + passwordReminder);*/
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例15: sessionCreated
import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/** {@inheritDoc} */
@Override
public void sessionCreated(IoSession session) throws Exception {
boolean isClient = session.getRemoteAddress().equals(forward);
if (log.isDebugEnabled()) {
log.debug("Is downstream: " + isClient);
session.getFilterChain().addFirst("protocol", new ProtocolCodecFilter(codecFactory));
}
session.getFilterChain().addFirst("proxy", new ProxyFilter(isClient ? "client" : "server"));
String fileName = System.currentTimeMillis() + '_' + forward.getHostName() + '_' + forward.getPort() + '_' + (isClient ? "DOWNSTREAM" : "UPSTREAM");
File headersFile = loader.getResource(dumpTo + fileName + ".cap").getFile();
headersFile.createNewFile();
File rawFile = loader.getResource(dumpTo + fileName + ".raw").getFile();
rawFile.createNewFile();
FileOutputStream headersFos = null;
FileOutputStream rawFos = null;
try {
headersFos = new FileOutputStream(headersFile);
rawFos = new FileOutputStream(rawFile);
WritableByteChannel headers = headersFos.getChannel();
WritableByteChannel raw = rawFos.getChannel();
IoBuffer header = IoBuffer.allocate(1);
header.put((byte) (isClient ? 0x00 : 0x01));
header.flip();
headers.write(header.buf());
session.getFilterChain().addFirst("dump", new NetworkDumpFilter(headers, raw));
} finally {
if (headersFos != null) {
headersFos.close();
}
if (rawFos != null) {
rawFos.close();
}
}
//session.getFilterChain().addLast("logger", new LoggingFilter() );
if (!isClient) {
log.debug("Connecting..");
IoConnector connector = new NioSocketConnector();
connector.setHandler(this);
ConnectFuture future = connector.connect(forward);
future.awaitUninterruptibly(); // wait for connect, or timeout
if (future.isConnected()) {
if (log.isDebugEnabled()) {
log.debug("Connected: {}", forward);
}
IoSession client = future.getSession();
client.setAttribute(ProxyFilter.FORWARD_KEY, session);
session.setAttribute(ProxyFilter.FORWARD_KEY, client);
}
}
super.sessionCreated(session);
}