Esempio n. 1
0
ChannelFuturePtr ClientBootstrap::connect(const InetAddress& remote,
        const InetAddress& local) {
    if (!remote) {
        LOG_INFO << "the remote address is invalidated, then return a failed future.";
        return NullChannel::instance()->newFailedFuture(
                   ChannelException("Failed to initialize a pipeline."));
    }

    ChannelPtr ch = newChannel();

    if (!ch) {
        LOG_INFO << "failed to create a new channel, then return a failed future.";
        return NullChannel::instance()->newFailedFuture(
                   ChannelException("Failed to create a new channel."));
    }

    if (!initializer()) {
        LOG_INFO << "has not set channel pipeline initializer.";
        return NullChannel::instance()->newFailedFuture(
                   ChannelException("has not set channel pipeline initializer."));
    }

    ch->setInitializer(initializer());
    ch->open();

    ch->closeFuture()->addListener(boost::bind(
        &ClientBootstrap::closeChannelBeforeDestruct,
        this,
        _1,
        ch));

    // Set the options.
    ch->config().setOptions(options());

    insertChannel(ch->id(), ch);

    // Bind.
    if (localAddress()) {
        LOG_INFO << "bind the channel to local address" << local.toString();
        ChannelFuturePtr future = ch->bind(local);
        future->awaitUninterruptibly();
    }

//     ch->closeFuture()->addListener(
//         boost::bind(&ClientBootstrap::onChannelClosed,
//                     this,
//                     _1));

    // Connect.
    return ch->connect(remote);
}
Esempio n. 2
0
/*const std::vector<const std::string&> params;
	params.push_back()*/
int main(int argc, char* argv[]) {
    // Print usage if no argument is specified.

    // Parse options.
    std::string host = "192.168.1.112";
    int port = 4730;
    int ioThreadCount = 1;

    // Configure the client.
    ClientBootstrap bootstrap(new AsioClientSocketChannelFactory(ioThreadCount));

    bootstrap.setPipelineFactory(new GearmanPipelineFactory);

    // Start the connection attempt.
    std::vector<Channel*> clientChannels;

    ChannelFuturePtr future = bootstrap.connect(host, port);
    future->awaitUninterruptibly();

	ChannelPtr c = future->channel();

	//test4Echo(c);
	test4submitJob(c);
	test4submitJobBG(c);
	//Sleep(5000);
 	//test4GetStatus(c);
	//test4submitJobBG(c);

    system("PAUSE");
    c->closeFuture()->awaitUninterruptibly();

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

    return 0;
}
Esempio n. 3
0
/**
 * A simple HTTP client that prints out the content of the HTTP response to
 * {@link System#out} to test {@link HttpServer}.
 *
 * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
 * @author Andy Taylor ([email protected])
 * @author <a href="http://gleamynode.net/">Trustin Lee</a>
 *
 * @version $Rev: 2226 $, $Date: 2010-03-31 11:26:51 +0900 (Wed, 31 Mar 2010) $
 */
int main(int argc, const char* argv[]) {
    if (argc != 2) {
        printf("Usage: SnoopClient <URL>");
        return -1;
    }

    URI uri(argv[1]);
    std::string scheme = uri.getScheme().empty() ? "http" : uri.getScheme();
    std::string host = uri.getHost().empty() ? "localhost" : uri.getHost();
    int port = uri.getPort();

    if (port == -1) {
        if (scheme == "http") {
            port = 80;
        }
        else if (scheme == "https") {
            port = 443;
        }
    }

    if ((scheme != "http") && (scheme != "https")) {
        printf("Only HTTP(S) is supported.");
        return -1;
    }

    bool ssl = scheme == "https";

    // Configure the client.
    ClientBootstrap bootstrap(
        ChannelFactoryPtr(new AsioClientSocketChannelFactory(1)));

    // Set up the event pipeline factory.
    bootstrap.setPipeline(getPipeline(ssl));

    // Start the connection attempt.
    ChannelFuturePtr future = bootstrap.connect(InetAddress(host, port));

    // Wait until the connection attempt succeeds or fails.
    ChannelPtr channel = future->awaitUninterruptibly()->channel();

    if (!future->isSuccess()) {
        bootstrap.shutdown();
        return -1;
    }

    // Prepare the HTTP request.
    HttpRequestPtr request =
        HttpRequestPtr(new HttpRequest(HttpVersion::HTTP_1_1,
                       HttpMethod::GET,
                       uri.toString()));

    request->setHeader(HttpHeaders::Names::HOST, host);
    request->setHeader(HttpHeaders::Names::CONNECTION, HttpHeaders::Values::CLOSE);
    request->setHeader(HttpHeaders::Names::ACCEPT_ENCODING, HttpHeaders::Values::GZIP);

    // Set some example cookies.
    //CookieEncoder httpCookieEncoder = new CookieEncoder(false);
    //httpCookieEncoder.addCookie("my-cookie", "foo");
    //httpCookieEncoder.addCookie("another-cookie", "bar");
    //request->setHeader(HttpHeaders::Names::COOKIE, httpCookieEncoder.encode());

    // Send the HTTP request.
    channel->write(request);

    // Wait for the server to close the connection.
    channel->closeFuture()->awaitUninterruptibly();

    // Shut down executor threads to exit.
    bootstrap.shutdown();

    return 0;
};