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) { } } }
public static void main(String[] args) throws Exception{ int port=9527; final IoAcceptor acceptor=new SocketAcceptor(Runtime.getRuntime().availableProcessors() + 1, Executors.newCachedThreadPool()); acceptor.getFilterChain().addLast("stringserialize", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); IoHandler handler=new IoHandlerAdapter(){ public void messageReceived(IoSession session, Object message) throws Exception { if("quit".equalsIgnoreCase(message.toString())){ acceptor.unbindAll(); System.out.println("Server has been shutdown!"); System.exit(0); } System.out.println("Message from client: "+message); session.write("Server response��"+message); } }; acceptor.bind(new InetSocketAddress(port), handler); System.out.println("Server listen on port: "+port); }
/** * ������� * @param args * @throws IOException */ public void startServer() { server = new NioSocketAcceptor(); //�������ݹ����� DefaultIoFilterChainBuilder filterChain = server.getFilterChain(); filterChain.addLast("myChin", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); //filterChain.addLast("textCode",new ProtocolCodecFilter( // new TextLineCodecFactory(Charset.forName("UTF-8")))); serverIOHandler = new ServerIOHandler(severFrame); server.setHandler(serverIOHandler); //�����������˿� --- ���������� try { server.bind(new InetSocketAddress(port)); } catch (IOException e) { Tools.show(severFrame, "�˿��Ѿ�ռ�ã��뻻���˿ڣ�"); } }
public void open() throws Exception { if (logger.isDebugEnabled()) { logger.debug("Start logger."); } SocketAcceptorConfig cfg = new SocketAcceptorConfig(); cfg.setReuseAddress(true); if(useFixCodec) { cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ServerProtocolCodecFactory())); } else { cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); } cfg.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.bind(new InetSocketAddress(port), this, cfg); System.out.println("Listening on port " + port); }
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(); }
/** * �����ͻ������� */ public boolean connect() { // ʵ���� �������� Socket���� client = new NioSocketConnector(); // �������ݹ����� DefaultIoFilterChainBuilder filterChain = client.getFilterChain(); //filterChain.addLast("textCode", new ProtocolCodecFilter( // new TextLineCodecFactory(Charset.forName("UTF-8")))); filterChain.addLast("myChin", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); // �ͻ��˴������ ClientIoHandler clientIoHandler = new ClientIoHandler(loginFrame,client); client.setHandler(clientIoHandler); clientIoHandler.setRegisterFrame(registerFrame); // ���ӷ����� ConnectFuture future = client.connect(new InetSocketAddress( IP, Port)); // �ȴ� future.awaitUninterruptibly(); // �õ��Ự���� try { session = future.getSession(); return true; } catch (Exception e) { Tools.show(loginFrame, "�����ӷ�������������û������"); client.dispose(); if(registerFrame!=null) registerFrame.dispose(); return false; } // session.getCloseFuture().awaitUninterruptibly(); }
/** * Open client socket. * <p/> * throws Exception on errors */ public void open() throws Exception { if(logger.isDebugEnabled()) { logger.debug("Start logger."); } connector.setWorkerTimeout(10000); // Configure the service. SocketConnectorConfig cfg = new SocketConnectorConfig(); cfg.setConnectTimeout(CONNECT_TIMEOUT); cfg.setConnectTimeout(10); if(useFixCodec) { cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ServerProtocolCodecFactory())); } else { cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); } cfg.getFilterChain().addLast("logger", new LoggingFilter()); for (int i=0; i < 20; i++) { try { System.out.println("Try connect."); final ConnectFuture future = connector.connect(new InetSocketAddress(host, port), this, cfg); future.join(); session = future.getSession(); return; } catch (RuntimeIOException e) { Thread.sleep(5000); } } throw new BuildException("Failed connect."); }
public void start(int port) throws IOException { acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); acceptor.setHandler(this); acceptor.getSessionConfig().setReadBufferSize( 2048 ); acceptor.getSessionConfig().setIdleTime( IdleStatus.WRITER_IDLE, 10); acceptor.getSessionConfig().setIdleTime( IdleStatus.READER_IDLE, 10); acceptor.setReuseAddress(true); acceptor.bind(new InetSocketAddress(port)); }
public void connect(String host, int port) throws IOException, ExecutionException, InterruptedException { this.host = host; this.port = port; connector = new NioSocketConnector(); connector.setConnectTimeoutMillis(4000); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10); connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 10); connector.getSessionConfig().setReceiveBufferSize(2048); connector.setHandler(this); System.out.println("Connecting.. " + host + ":" + port + DateTime.now().toString()); future = connector.connect(new InetSocketAddress(host, port)); }
public static void main(String[] args) throws Exception { NioDatagramAcceptor acceptor = new NioDatagramAcceptor();//UDP 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); DatagramSessionConfig dcfg = acceptor.getSessionConfig(); dcfg.setReuseAddress(true); acceptor.bind(new InetSocketAddress(PORT)); }
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)); }
public static void main(String[] args) { /* Start listening for incoming connections * (see e.g. https://mina.apache.org/mina-project/quick-start-guide.html) */ SocketAcceptor socketAcceptor = new NioSocketAcceptor(); socketAcceptor.getFilterChain().addLast("logger", new LoggingFilter()); socketAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); //TODO: This is a default codec, should ideally not be used // socketAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); ServerHandler handler = new ServerHandler(); socketAcceptor.setHandler(handler); try { socketAcceptor.setReuseAddress(true); socketAcceptor.bind(new InetSocketAddress(SOCKET_PORT)); } catch (IOException e) { e.printStackTrace(); } /* Make the php-java interface accessible */ PluginWebServicesImpl pws = new PluginWebServicesImpl(handler); Endpoint.publish(PUB_ADDRESS, pws); System.out.println("published"); // try { // Configuration config = new Configuration(); // config.configure(); // // sqlSessionFactory = config.buildSessionFactory(new StandardServiceRegistryBuilder(). // applySettings(config.getProperties()).build()); // } catch (Exception ex) { // ex.printStackTrace(); // } }
/** * 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); }
public PeerlessUdpServer(PeerlessDbSupport peerlessDbSupport, SigHashTable sht) throws IOException { this.sht=sht; this.peerlessDbSupport=peerlessDbSupport; NioDatagramAcceptor acceptor = new NioDatagramAcceptor(); acceptor.setHandler(new PeerlessUdpServerHandler(this)); DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); chain.addLast("logger", new LoggingFilter()); DatagramSessionConfig dcfg = acceptor.getSessionConfig(); dcfg.setReuseAddress(true); chain.addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); acceptor.bind(new InetSocketAddress(PORT)); System.out.println("UDPServer listening on port " + PORT); }
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; } }
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; } }
public static void main(String[] args) throws Exception { NioSocketAcceptor acceptor = new NioSocketAcceptor(); DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter(); chain.addLast("mdc", mdcInjectionFilter); SigHashTable sht = new SigHashTable(); /* * FileSignature d0=new FileSignature("test data"); * SigManager.printByteArray(d0); * * FileSignature d1=new FileSignature(new File("ftphome//test.txt")); * SigManager.printByteArray(d1); * * FileSignature d2=newFileSignature( * "Design and Implementation of Virtualized Network Simulator"); * SigManager.printByteArray(d2); * * sht.addSignature(d0); sht.addSignature(d1); sht.addSignature(d2); */ PeerlessDbSupport peerlessDbSupport = new PeerlessDbSupport(ServerConfig.ServerDb); peerlessDbSupport.connect(); peerlessDbSupport.createSchema(); SuperPeerManager superPeerManager = new SuperPeerManager(); // Add SSL filter if SSL is enabled. if (USE_SSL) { addSSLSupport(chain); } // chain.addLast("codec", new ProtocolCodecFilter(new // TextLineCodecFactory())); chain.addLast("codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory())); addLogger(chain); // Bind acceptor.setHandler(new PeerlessProtocolHandler(sht, peerlessDbSupport, superPeerManager)); acceptor.bind(new InetSocketAddress(ServerConfig.PORT)); System.out.println("Listening on port " + ServerConfig.PORT); try { new PeerlessUdpServer(peerlessDbSupport, sht); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String[] args) throws Throwable { init(args); NioSocketConnector connector = new NioSocketConnector(); // Configure the service. connector.setConnectTimeoutMillis(CONNECT_TIMEOUT); if (USE_CUSTOM_CODEC) { connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new SumUpProtocolCodecFactory(false))); } else { connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); } int[] values = new int[]{}; connector.getFilterChain().addLast("logger", new LoggingFilter()); connector.setHandler(new ClientSessionHandler(values)); long time = System.currentTimeMillis(); IoSession session; for (; ; ) { try { System.out.println(host + " " + port + " " + fileTest); ConnectFuture future = connector.connect(new InetSocketAddress(host, port)); future.awaitUninterruptibly(); session = future.getSession(); File file = new File(fileTest); FileInputStream is = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); int data = br.read(); int count = 0; IoBuffer ib = IoBuffer.allocate(274); ib.setAutoExpand(true); boolean flagcount = false; while(data != -1){ data = br.read(); ib.put((byte)data); if (flagcount){count++;} if (data==13){ count=1; flagcount = true; LOGGER.debug(ib.toString()); } if (count == 4) { ib.flip(); session.write(ib); ib = IoBuffer.allocate(274); ib.setAutoExpand(true); flagcount = false; count = 0; //Thread.sleep(500); } } break; } catch (RuntimeIoException e) { LOGGER.error("Failed to connect."); e.printStackTrace(); Thread.sleep(5000); } } time = System.currentTimeMillis() - time; LOGGER.info("Time " + time); // wait until the summation is done session.getCloseFuture().awaitUninterruptibly(); connector.dispose(); }