use of org.apache.mina.core.session.IoSession in project camel by apache.
the class Mina2UdpProtocolCodecFactory method getEncoder.
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return new ProtocolEncoder() {
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
IoBuffer buf = toIoBuffer(message);
buf.flip();
out.write(buf);
}
public void dispose(IoSession session) throws Exception {
// do nothing
}
};
}
use of org.apache.mina.core.session.IoSession in project pancm_project by xuwujing.
the class MinaClient method main.
/*
* 测试服务端与客户端程序!
a. 启动服务端,然后再启动客户端(客户端发送的消息是"why are you so diao ")
b. 服务端接收消息并处理成功;
*/
public static void main(String[] args) {
// 创建一个非阻塞的客户端程序
IoConnector connector = new NioSocketConnector();
// 设置链接超时时间
connector.setConnectTimeout(30000);
ProtocolCodecFilter pf = new ProtocolCodecFilter((new MyTextLineCodecFactory(Charset.forName("utf-8"), "\r\n")));
// 添加过滤器
connector.getFilterChain().addLast("codec", pf);
// 添加业务逻辑处理器类
connector.setHandler(new MinaClientHandler());
IoSession session = null;
try {
ConnectFuture future = connector.connect(new InetSocketAddress(HOST, // 创建连接
PORT));
// 等待连接创建完成
future.awaitUninterruptibly();
// 获得session
session = future.getSession();
String msg = "hello \r\n";
// 发送消息
session.write(msg);
logger.info("客户端与服务端建立连接成功...发送的消息为:" + msg);
} catch (Exception e) {
e.printStackTrace();
logger.error("客户端链接异常...", e);
}
// 等待连接断开
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
use of org.apache.mina.core.session.IoSession in project streamsx.topology by IBMStreams.
the class TupleCollection method addTCPServerAndSink.
/**
* Add a TCP server that will list for tuples to be directed to handlers.
* Adds a sink to the topology to capture those tuples and deliver them to
* the current jvm to run Junit type tests.
*/
private void addTCPServerAndSink() throws Exception {
tcpServer = new TCPTestServer(0, new IoHandlerAdapter() {
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
TestTuple tuple = (TestTuple) message;
TestTupleInjector injector = injectors.get(tuple.getTesterId());
injector.tuple(tuple.getTupleData());
}
});
InetSocketAddress testAddr = tcpServer.start();
addTesterSink(testAddr);
}
use of org.apache.mina.core.session.IoSession in project opennms by OpenNMS.
the class AsyncBasicDetectorMinaImpl method isServiceDetected.
/**
* {@inheritDoc}
*/
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
final DetectFutureMinaImpl detectFuture = new DetectFutureMinaImpl(this);
try {
// Set this up here because it can throw an Exception, which we want
// to throw now, not in initializeSession
final SSLContext c = createClientSSLContext();
// Create an IoSessionInitializer that will configure this individual
// session. Previously, all this was done on a new Connector each time
// but that was leaking file handles all over the place. This way gives
// us per-connection settings without the overhead of creating new
// Connectors each time
IoSessionInitializer<ConnectFuture> init = new IoSessionInitializer<ConnectFuture>() {
@Override
public void initializeSession(IoSession session, ConnectFuture future) {
// Add filters to the session
if (isUseSSLFilter()) {
final SslFilter filter = new SslFilter(c);
filter.setUseClientMode(true);
session.getFilterChain().addFirst("SSL", filter);
}
session.getFilterChain().addLast("logger", getLoggingFilter() != null ? getLoggingFilter() : new SlightlyMoreVerboseLoggingFilter());
session.getFilterChain().addLast("codec", getProtocolCodecFilter());
// Make the minimum idle timeout 1 second
int idleTimeInSeconds = Math.max(1, Math.round(getIdleTime() / 1000.0f));
// Set all of the idle time limits. Make sure to specify values in
// seconds!!!
session.getConfig().setReaderIdleTime(idleTimeInSeconds);
session.getConfig().setWriterIdleTime(idleTimeInSeconds);
session.getConfig().setBothIdleTime(idleTimeInSeconds);
}
};
// Start communication
final InetSocketAddress socketAddress = new InetSocketAddress(address, getPort());
final ConnectFuture cf = m_connectionFactory.connect(socketAddress, init, createDetectorHandler(detectFuture));
cf.addListener(retryAttemptListener(detectFuture, socketAddress, init, getRetries()));
} catch (KeyManagementException e) {
detectFuture.setException(e);
} catch (NoSuchAlgorithmException e) {
detectFuture.setException(e);
} catch (Throwable e) {
detectFuture.setException(e);
}
return detectFuture;
}
use of org.apache.mina.core.session.IoSession in project directory-ldap-api by apache.
the class AbstractCodecServiceTest method setupLdapCodecService.
/**
* Initialize the codec service
*/
@BeforeClass
public static void setupLdapCodecService() {
codec = new DefaultLdapCodecService();
codec.registerProtocolCodecFactory(new ProtocolCodecFactory() {
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return null;
}
public ProtocolDecoder getDecoder(IoSession session) throws Exception {
return null;
}
});
if (LdapApiServiceFactory.isInitialized() == false) {
LdapApiServiceFactory.initialize(codec);
}
encoder = new LdapEncoder(codec);
}
Aggregations