`
收藏列表
标题 标签 来源
打开默认浏览器浏览页面
if (Program.launch(wsDesc.getAddress())) { //$NON-NLS-1$
    return;
} else {
    try {
        Runtime.getRuntime().exec("firefox " + wsDesc.getAddress()); //$NON-NLS-1$
    } catch (Exception e1) {
        String title = Messages.getString("WebServiceMeansComposite.Error"); //$NON-NLS-1$
        String message = Messages.getString("WebServiceMeansComposite.CannoOpen"); //$NON-NLS-1$
        MessageDialog.openError(addressLink.getShell(), title, message);
        CommonPlugin.log(e1);
    }
}
Netty Chat Server
public class EchoServerHandler extends SimpleChannelUpstreamHandler {

    private static final Logger logger = Logger.getLogger(EchoServerHandler.class.getName());

    private final AtomicLong transferredBytes = new AtomicLong();
    
    private static final ChannelGroup channels = new DefaultChannelGroup();

    public long getTransferredBytes() {
        return transferredBytes.get();
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        // Send back the received message to the remote peer.
//        transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
//        e.getChannel().write(e.getMessage());
        
        System.out.println("Server --> ");
        String bufferStr = (String) e.getMessage();
        System.out.println(new String(bufferStr));
        for (Channel channel : channels) {
            channel.write(e.getMessage());
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        logger.log(Level.WARNING, "Unexpected exception from downstream.", e.getCause());
        channels.remove(e.getChannel());
        e.getChannel().close();
    }
    
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
     // Get the SslHandler in the current pipeline.
        // We added it in SecureChatPipelineFactory.
        final SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);

        // Get notified when SSL handshake is done.
        ChannelFuture handshakeFuture = sslHandler.handshake();
        handshakeFuture.addListener(new Greeter(sslHandler));
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        channels.remove(e.getChannel());
        super.channelDisconnected(ctx, e);
    }
    
    private static final class Greeter implements ChannelFutureListener {

        private final SslHandler sslHandler;

        Greeter(SslHandler sslHandler) {
            this.sslHandler = sslHandler;
        }

        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // Once session is secured, send a greeting.
                future.getChannel().write(
                        "Welcome to " + InetAddress.getLocalHost().getHostName() +
                        " secure chat service!\n");
                future.getChannel().write(
                        "Your session is protected by " +
                        sslHandler.getEngine().getSession().getCipherSuite() +
                        " cipher suite.\n");

                // Register the channel to the global channel list
                // so the channel received the messages from others.
                channels.add(future.getChannel());
            } else {
                future.getChannel().close();
            }
        }
    }
}

bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline cp = Channels.pipeline();

                SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
                engine.setUseClientMode(false);

                cp.addLast("ssl", new SslHandler(engine));
                cp.addLast("decoder", new StringDecoder());
                cp.addLast("encoder", new StringEncoder());
                cp.addLast("handler", new EchoServerHandler());

                return cp;
            }
        });
Netty Chat Client
public class EchoClient {

    private final String host;
    private final int port;
    private final int firstMessageSize;

    public EchoClient(String host, int port, int firstMessageSize) {
        this.host = host;
        this.port = port;
        this.firstMessageSize = firstMessageSize;
    }

    public ChannelFuture run() {
        // Configure the client.
        ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

        // Set up the pipeline factory.
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                
                ChannelPipeline cp = Channels.pipeline();

                SSLEngine engine =
                    SecureChatSslContextFactory.getClientContext().createSSLEngine();
                engine.setUseClientMode(true);

                cp.addLast("ssl", new SslHandler(engine));
                cp.addLast("decoder", new StringDecoder());
                cp.addLast("encoder", new StringEncoder());
                cp.addLast("handler", new EchoClientHandler(firstMessageSize));
                
                return cp;
            }
        });

        // Start the connection attempt.
        /*ChannelFuture future = */return bootstrap.connect(new InetSocketAddress(host, port));

        // Wait until the connection is closed or the connection attempt fails.
        // future.getChannel().getCloseFuture().awaitUninterruptibly();

        // Shut down thread pools to exit.
        // bootstrap.releaseExternalResources();
    }

    public static void main(String[] args) throws Exception {
        // Parse options.
        String host;
        int port;
        int firstMessageSize;

        // Print usage if no argument is specified.
        if (args.length < 2 || args.length > 3) {
            host = "localhost";
            port = 8787;
            firstMessageSize = 256;
        } else {
            host = args[0];
            port = Integer.parseInt(args[1]);
            if (args.length == 3) {
                firstMessageSize = Integer.parseInt(args[2]);
            } else {
                firstMessageSize = 256;
            }
        }
        ChannelFuture channelFutrue = new EchoClient(host, port, firstMessageSize).run();

        Channel channel = channelFutrue.awaitUninterruptibly().getChannel();

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String lineStr = reader.readLine();
            if ("11".equals(lineStr)) {
                break;
            }
            channel.write(lineStr);
        }

    }
}

// Set up the pipeline factory.
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                
                ChannelPipeline cp = Channels.pipeline();

                SSLEngine engine =
                    SecureChatSslContextFactory.getClientContext().createSSLEngine();
                engine.setUseClientMode(true);

                cp.addLast("ssl", new SslHandler(engine));
                cp.addLast("decoder", new StringDecoder());
                cp.addLast("encoder", new StringEncoder());
                cp.addLast("handler", new EchoClientHandler(firstMessageSize));
                
                return cp;
            }
        });
获取APP的文件路径
public static File RESOURCES_BASE_FILE = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile());
Global site tag (gtag.js) - Google Analytics