Code Monkey home page Code Monkey logo

channeling's Introduction

Project Has Moved

This project idea has moved to a new location. Please visit the new repository at new repository link.

if you are owner, please visit to this link

Channeling (archive)

The java Nio Based, Async Non Block tcp / http client.

HTTP

public void testRequestBuilder() throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ChannelingSocket cs = channeling.wrap(null);

        HttpRequestBuilder requestBuilder = new HttpRequestBuilder();

        requestBuilder.setMethod("GET");
        requestBuilder.addHeader("Host", "127.0.0.1:80");
        requestBuilder.setPath("/");



        HttpRequest httpSingleRequest = new HttpRequest(
                cs,
                "127.0.0.1",
                80,
                requestBuilder.toString(),
                1024
        );


        httpSingleRequest.get(httpResponse -> {
            String result = httpResponse.getBodyContent();
            Assertions.assertTrue(result.toLowerCase().contains("</html>"), result.substring(result.length() - 15));
            totalDone.incrementAndGet();
            countDownLatch.countDown();
        }, Throwable::printStackTrace);


        countDownLatch.await();

        if (totalDone.get() % 100 == 0)
            logger.info("Done " + totalDone.get());
    }
    

HTTPS

public void testSSL() throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ChannelingSocket cs = channeling.wrapSSL("TLSv1.2", "mycamel.abcfoo.com", 443, null);
        String host = "mycamel.abcfoo.com";
        HttpRequest httpSingleRequest = new HttpSingleRequest(
                cs,
                host,
                443,
                "GET / HTTP/1.1\n" +
                        "Host: "+host+"\n\n",
//                "Connection: close\n" + // Do not close
                cs.getSSLMinimumInputBufferSize(),false
        );


        httpSingleRequest.get(httpResponse -> {
            String result = httpResponse.getBodyContent();
            Assertions.assertTrue(result.toLowerCase().contains("</html>"), result.substring(result.length() - 15));

            totalDone.incrementAndGet();
            countDownLatch.countDown();
        }, Throwable::printStackTrace);


        countDownLatch.await();
        if(totalDone.get() % 100 == 0)
        System.out.println("Done " + totalDone.get());
    }
    

TCP example

 public void testingSocket() throws IOException, InterruptedException {
        logger.info("Start Testing ");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Channeling channeling = Channeling.startNewChanneling(1, 2 * 1000, 15 * 1000);
        ChannelingSocket socket = channeling.wrap(null);
        socket.withConnect("127.0.0.1", 8080)
                .when(Channeling.whenConnected)
                .then(sc -> {
                    StringBuilder a = new StringBuilder();
                    for(int i = 0 ; i< 5000; i++) {
                        a.append(i);
                    }

                    sc.withWrite(ByteBuffer.wrap(a.toString().getBytes(StandardCharsets.UTF_8))).when(Channeling.whenNoMoreToWrite)
                            .then(sc1 -> {
                                System.out.println("done");
                                sc1.withRead(2048).when(Channeling.whenByteConsumed)
                                        .then(sc2 -> {

                                            System.out.println(sc2.getReadBuffer());

//                                            System.out.println(new String(sc2.getReadBuffer().flip().array()));


                                            sc.withClose().when(Channeling.whenClosed)
                                                    .then(scclose -> {

                                                        logger.info("Done Close");
                                                        Assertions.assertTrue(true);
                                                        countDownLatch.countDown();
                                                    });

                                        });
                            });
                }, (sc, e) -> {
                    e.printStackTrace();
                    countDownLatch.countDown();
                    Assertions.fail(e.getMessage());
                });


        logger.info("SETUP FINISHED");

        countDownLatch.await();
        channeling.shutdown();
        Assertions.assertTrue(true);
    }
    

With http proxy

 public void testProxy() throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        ChannelingSocket cs = channeling.wrapProxy(channelingProxy, null);

        HttpRequest httpSingleRequest = new HttpRequest(
                cs,
                "openapi.abcfoo.com",
                80,
                "GET / HTTP/1.1\n" +
                "Host: openapi.abcfoo.com:80\n\n",
                1024
        );

        httpSingleRequest.get(httpResponse -> {
            String result = httpResponse.getBodyContent();
            Assertions.assertTrue(result.contains("</html>"), result.substring(result.length() - 15));
            totalDone.incrementAndGet();
            countDownLatch.countDown();
        }, e -> {
            countDownLatch.countDown();
            e.printStackTrace();
        });


        countDownLatch.await();

        if(totalDone.get() % 100 == 0)
            System.out.println("Done " + totalDone.get());
    }

channeling's People

Contributors

taymindis avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.