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