本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketConnector類的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketConnector類的具體用法?Java NioSocketConnector怎麽用?Java NioSocketConnector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NioSocketConnector類屬於org.apache.mina.transport.socket.nio包,在下文中一共展示了NioSocketConnector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public void connect() {
synchronized(mutex) {
if (connected.getFlag()) return;
if (connecting.getFlag()) return;
log.warning("Connecting to TC at " + getHost() + ":" + getPort() + " ...");
connecting.setFlag(true);
}
try {
ioConnector = new NioSocketConnector();
ioConnector.setHandler(this);
ioConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connectFuture = ioConnector.connect(address);
connectFuture.addListener(connectionListener);
} catch (Exception e1) {
try {
connecting.setFlag(false);
} catch (Exception e2) {
}
}
}
示例2: configureConnector
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
@Override
protected void configureConnector ( final NioSocketConnector connector )
{
logger.debug ( "Configuring connector: {}", connector );
switch ( this.protocolType )
{
case TYPE_TCP:
connector.getFilterChain ().addLast ( "modbusPdu", new ProtocolCodecFilter ( new ModbusTcpEncoder (), new ModbusTcpDecoder () ) );
connector.getFilterChain ().addLast ( "modbus", new ModbusMasterProtocolFilter () );
break;
case TYPE_RTU:
// convert milliseconds to microseconds to allow more accurate timing
final ModbusRtuDecoder rtuDecoder = new ModbusRtuDecoder ( getExecutor (), Double.valueOf ( this.interFrameDelay * 1000 ).longValue (), TimeUnit.MICROSECONDS );
connector.getFilterChain ().addLast ( "modbusPdu", new ModbusRtuProtocolCodecFilter ( new ModbusRtuEncoder (), rtuDecoder ) );
connector.getFilterChain ().addLast ( "modbus", new ModbusMasterProtocolFilter () );
break;
default:
throw new IllegalArgumentException ( String.format ( "'%s' is not an allowed modbus device type", this.protocolType ) );
}
if ( Boolean.getBoolean ( "org.eclipse.scada.da.server.osgi.modbus.trace" ) )
{
connector.getFilterChain ().addFirst ( "logger", new LoggingFilter ( ModbusMaster.class.getName () + ".protocol" ) );
}
}
示例3: MinaClient
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
/**
* 初始化
*
* @param ipArray
* ip地址數組
* @param portArray
* 端口數組
* @param nameArray
* 名稱數組
* @throws Exception
*/
public MinaClient(String[] ipArray, int[] portArray, String[] nameArray, Class<?> HandlerClass) throws Exception {
for (int i = 0; i < ipArray.length; i++) {
String ip = ipArray[i];
int port = portArray[i];
String name = nameArray[i];
IoConnector ioConnector = new NioSocketConnector();
ioConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaEncoder(), new MinaDecoder()));
MinaHandler minaHandler = (MinaHandler) HandlerClass.newInstance();
minaHandler.ioConnector = ioConnector;
minaHandler.name = name;
ioConnector.setHandler(minaHandler);
ioConnector.setConnectTimeoutMillis(10000);
InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
ioConnectorMap.put(ioConnector, inetSocketAddress);
ioConnectorStateMap.put(ioConnector, false);
}
start();
}
示例4: clientStart
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
@Override
protected void clientStart() throws RemotingException {
try {
connector = new NioSocketConnector(); //TCP Connector
// connector.getFilterChain().addFirst("logging", new MinaLoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec())));
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.setHandler(new MinaHandler(this));
IoSessionConfig cfg = connector.getSessionConfig();
cfg.setReaderIdleTime(remotingClientConfig.getReaderIdleTimeSeconds());
cfg.setWriterIdleTime(remotingClientConfig.getWriterIdleTimeSeconds());
cfg.setBothIdleTime(remotingClientConfig.getClientChannelMaxIdleTimeSeconds());
} catch (Exception e) {
throw new RemotingException("Mina Client start error", e);
}
}
示例5: init
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.targetIP, config.targetPort);
connector = new NioSocketConnector();
connector.setDefaultRemoteAddress(address);
if (connector.getFilterChain().get(LOGGER) == null) {
connector.getFilterChain().addLast(LOGGER, new LoggingFilter());
}
if (connector.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
connector.getFilterChain().addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
connector.setHandler(new MinaClientHandler());
connector.getSessionConfig().setReadBufferSize(config.bufferSize);
connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
KeepAliveFilter keepAliveFilter = MinaUtil.initClientKeepAlive(config, this);
if (keepAliveFilter != null) {
connector.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例6: MinaDestination
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public MinaDestination(String host, int port, boolean withLogging) throws Exception {
socketAddress = new InetSocketAddress(host, port);
connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);
connector.setConnectTimeoutMillis(10000L);
connectorConfig = connector.getSessionConfig();
executor = new OrderedThreadPoolExecutor(5);
connector.getFilterChain().addLast("threadPool", new ExecutorFilter(executor));
if (withLogging) {
connector.getFilterChain().addLast("logger", new LoggingFilter());
}
SslFilter filter = new SslFilter(SSLContext.getDefault(), true);
filter.setUseClientMode(true);
connector.getFilterChain().addFirst("sslFilter", filter);
}
示例7: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
/**
* 執行連接操作的方法
*
* @throws RedisProtocolException
* 當連接出現問題時拋出該異常
*/
private void connect() throws RedisProtocolException {
connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(connectionTimeOut);
connector.getFilterChain().addFirst("redis-protocol", new ProtocolCodecFilter(new RedisProtocolCodecFactory()));
connector.setHandler(this);
connector.connect(new InetSocketAddress(address, port));
}
示例8: Work
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public Work(){
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast( "logger", new LoggingFilter() );
connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new MessageCodecFactory())); //設置編碼過濾器
connector.setConnectTimeoutMillis(30000);
connector.setHandler(new ClientMessageHandler());//設置事件處理器
ConnectFuture cf = connector.connect(
new InetSocketAddress("127.0.0.1", 50000));//建立連接
cf.awaitUninterruptibly();//等待連接創建完成
ByteArray ba = new ByteArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("parseId", 1);
jsonObject.put("command", 1);
jsonObject.put("text", "塗鴉");
ba.writeUTF(jsonObject.toJSONString());
cf.getSession().write(ba.toArray());
// cf.getSession().write("塗鴉");//發送消息
// cf.getSession().write("quit");//發送消息
// cf.getSession().getCloseFuture().awaitUninterruptibly();//等待連接斷開
// connector.dispose();
}
示例9: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
//網絡鏈接工具
NetSupport support = new NetSupport();
//創建回話鏈
ConversationChain chain = new ConversationChain(support);
buildConversation(chain);
//設置會話鏈
support.setConversationChain(chain);
NioSocketConnector connector = new NioSocketConnector();
SocketAddress address = new InetSocketAddress("localhost", 1101);
boolean isConnected = support.connect(connector, address);
if(isConnected){
support.startConversation();
}
support.quit();
connector.dispose();
}
示例10: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public static void main(String[] args) {
NioSocketConnector connector = new NioSocketConnector(); //TCP Connector
connector.getFilterChain().addLast("logging", new LoggingFilter());
connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.setHandler(new HelloClientHandler());
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, PORT));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
}
}
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
示例11: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的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);
}
示例12: init
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
protected void init() {
DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
decoder.addMessageDecoder(new ConnAckDecoder());
decoder.addMessageDecoder(new SubAckDecoder());
decoder.addMessageDecoder(new UnsubAckDecoder());
decoder.addMessageDecoder(new PublishDecoder());
decoder.addMessageDecoder(new PubAckDecoder());
decoder.addMessageDecoder(new PingRespDecoder());
DemuxingProtocolEncoder encoder = new DemuxingProtocolEncoder();
encoder.addMessageEncoder(ConnectMessage.class, new ConnectEncoder());
encoder.addMessageEncoder(PublishMessage.class, new PublishEncoder());
encoder.addMessageEncoder(SubscribeMessage.class, new SubscribeEncoder());
encoder.addMessageEncoder(UnsubscribeMessage.class, new UnsubscribeEncoder());
encoder.addMessageEncoder(DisconnectMessage.class, new DisconnectEncoder());
encoder.addMessageEncoder(PingReqMessage.class, new PingReqEncoder());
m_connector = new NioSocketConnector();
// m_connector.getFilterChain().addLast("logger", new LoggingFilter());
m_connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(encoder, decoder));
m_connector.setHandler(new DummyClientHandler());
m_connector.getSessionConfig().setReadBufferSize(2048);
m_connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Constants.DEFAULT_CONNECT_TIMEOUT);
}
示例13: buildConnection
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public void buildConnection() {
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(MessageCodecFactory.getInstance()));
connector.setHandler(new ClientHandler());
System.out.println("開始連接socket服務端");
int serverPort = ServerConfig.getInstance().getServerPort();
ConnectFuture future = connector.connect(new InetSocketAddress(serverPort));
future.awaitUninterruptibly();
IoSession session = future.getSession();
this.session = session;
}
示例14: createConnector
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
/**
* Create the connector
*/
private void createConnector() throws LdapException
{
// Use only one thread inside the connector
connector = new NioSocketConnector( 1 );
if ( connectionConfig != null )
{
( ( SocketSessionConfig ) connector.getSessionConfig() ).setAll( connectionConfig );
}
else
{
( ( SocketSessionConfig ) connector.getSessionConfig() ).setReuseAddress( true );
}
// Add the codec to the chain
connector.getFilterChain().addLast( "ldapCodec", ldapProtocolFilter );
// If we use SSL, we have to add the SslFilter to the chain
if ( config.isUseSsl() )
{
addSslFilter();
}
// Inject the protocolHandler
connector.setHandler( this );
}
示例15: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入依賴的package包/類
public static void main(String[] args) {
IoConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new PrefixedStringCodecFactory(Charset.forName("UTF-8"))));
connector.setHandler(new TimeClientHander());
ConnectFuture connectFuture = connector.connect(new InetSocketAddress("127.0.0.1", PORT));
// 等待建立連接
connectFuture.awaitUninterruptibly();
System.out.println("連接成功");
IoSession session = connectFuture.getSession();
Scanner sc = new Scanner(System.in);
boolean quit = false;
while (!quit) {
String str = sc.next();
if (str.equalsIgnoreCase("quit")) {
quit = true;
}
session.write(str);
}
// 關閉
if (session != null) {
if (session.isConnected()) {
session.getCloseFuture().awaitUninterruptibly();
}
connector.dispose(true);
}
}