Search in sources :

Example 76 with SocketAddress

use of java.net.SocketAddress in project flink by apache.

the class ZooKeeperLeaderRetrievalTest method testConnectingAddressRetrievalWithDelayedLeaderElection.

/**
	 * Tests that LeaderRetrievalUtils.findConnectingAdress finds the correct connecting address
	 * in case of an old leader address in ZooKeeper and a subsequent election of a new leader.
	 * The findConnectingAddress should block until the new leader has been elected and his
	 * address has been written to ZooKeeper.
	 */
@Test
public void testConnectingAddressRetrievalWithDelayedLeaderElection() throws Exception {
    FiniteDuration timeout = new FiniteDuration(1, TimeUnit.MINUTES);
    Configuration config = new Configuration();
    long sleepingTime = 1000;
    config.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    config.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    LeaderElectionService leaderElectionService = null;
    LeaderElectionService faultyLeaderElectionService;
    ServerSocket serverSocket;
    InetAddress localHost;
    Thread thread;
    CuratorFramework[] client = new CuratorFramework[2];
    try {
        client[0] = ZooKeeperUtils.startCuratorFramework(config);
        client[1] = ZooKeeperUtils.startCuratorFramework(config);
        String wrongHostPort = NetUtils.unresolvedHostAndPortToNormalizedString("1.1.1.1", 1234);
        String wrongAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), wrongHostPort, Option.<String>empty());
        try {
            localHost = InetAddress.getLocalHost();
            serverSocket = new ServerSocket(0, 50, localHost);
        } catch (UnknownHostException e) {
            // may happen if disconnected. skip test.
            System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
            return;
        } catch (IOException e) {
            // may happen in certain test setups, skip test.
            System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
            return;
        }
        InetSocketAddress correctInetSocketAddress = new InetSocketAddress(localHost, serverSocket.getLocalPort());
        String hostPort = NetUtils.unresolvedHostAndPortToNormalizedString(localHost.getHostName(), correctInetSocketAddress.getPort());
        String correctAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), hostPort, Option.<String>empty());
        faultyLeaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[0], config);
        TestingContender wrongLeaderAddressContender = new TestingContender(wrongAddress, faultyLeaderElectionService);
        faultyLeaderElectionService.start(wrongLeaderAddressContender);
        FindConnectingAddress findConnectingAddress = new FindConnectingAddress(config, timeout);
        thread = new Thread(findConnectingAddress);
        thread.start();
        leaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[1], config);
        TestingContender correctLeaderAddressContender = new TestingContender(correctAddress, leaderElectionService);
        Thread.sleep(sleepingTime);
        faultyLeaderElectionService.stop();
        leaderElectionService.start(correctLeaderAddressContender);
        thread.join();
        InetAddress result = findConnectingAddress.getInetAddress();
        // check that we can connect to the localHost
        Socket socket = new Socket();
        try {
            // port 0 = let the OS choose the port
            SocketAddress bindP = new InetSocketAddress(result, 0);
            // machine
            socket.bind(bindP);
            socket.connect(correctInetSocketAddress, 1000);
        } finally {
            socket.close();
        }
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (client[0] != null) {
            client[0].close();
        }
        if (client[1] != null) {
            client[1].close();
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) FiniteDuration(scala.concurrent.duration.FiniteDuration) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 77 with SocketAddress

use of java.net.SocketAddress in project hadoop by apache.

the class RpcProgramNfs3 method commit.

@Override
public COMMIT3Response commit(XDR xdr, RpcInfo info) {
    SecurityHandler securityHandler = getSecurityHandler(info);
    RpcCall rpcCall = (RpcCall) info.header();
    int xid = rpcCall.getXid();
    SocketAddress remoteAddress = info.remoteAddress();
    return commit(xdr, info.channel(), xid, securityHandler, remoteAddress);
}
Also used : SecurityHandler(org.apache.hadoop.oncrpc.security.SecurityHandler) SysSecurityHandler(org.apache.hadoop.oncrpc.security.SysSecurityHandler) RpcCall(org.apache.hadoop.oncrpc.RpcCall) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 78 with SocketAddress

use of java.net.SocketAddress in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadWithTor.

private void downloadWithTor(boolean useTor, Uri uri, String title, String desc, File targetFile) {
    initNotificationManager();
    // generate id/tag for notification
    String nTag = indexItem.getExpansionId();
    int nId = 0;
    if (fileName.contains(scal.io.liger.Constants.MAIN)) {
        nId = Integer.parseInt(indexItem.getExpansionFileVersion());
    } else if (fileName.contains(scal.io.liger.Constants.PATCH)) {
        nId = Integer.parseInt(indexItem.getPatchFileVersion());
    }
    // incompatible with lungcast certificate
    // StrongHttpsClient httpClient = getHttpClientInstance();
    OkHttpClient httpClient = new OkHttpClient();
    // we're now using this method to support non-tor downloads as well, so settings must be checked
    if (useTor) {
        if (checkTor(context)) {
            Timber.d("DOWNLOAD WITH TOR PROXY: " + scal.io.liger.Constants.TOR_PROXY_HOST + "/" + scal.io.liger.Constants.TOR_PROXY_PORT);
            SocketAddress torSocket = new InetSocketAddress(scal.io.liger.Constants.TOR_PROXY_HOST, scal.io.liger.Constants.TOR_PROXY_PORT);
            Proxy torProxy = new Proxy(Proxy.Type.HTTP, torSocket);
            httpClient.setProxy(torProxy);
        } else {
            Timber.e("CANNOT DOWNLOAD WITH TOR, TOR IS NOT ACTIVE");
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Check settings, can't use tor if orbot isn't running", true);
            }
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
            return;
        }
    }
    // disable attempts to retry (more retries ties up connection and prevents failure handling)
    httpClient.setRetryOnConnectionFailure(false);
    // set modest timeout (longer timeout ties up connection and prevents failure handling)
    httpClient.setConnectTimeout(3000, TimeUnit.MILLISECONDS);
    String actualFileName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf("."));
    Timber.d("CHECKING URI: " + uri.toString());
    try {
        // FIXME - adding the "Connection: close" header to resolve an issue that seemed to be caused
        // FIXME - by a lingering connection.  when possible, the better solution would be to track
        // FIXME - down the possible end states and add appropriate cleanup steps.
        Request request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").build();
        // check for partially downloaded file
        File partFile = new File(targetFile.getPath().replace(".tmp", ".part"));
        if (partFile.exists()) {
            long partBytes = partFile.length();
            Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
            // request.setHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
            request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").addHeader("Range", "bytes=" + Long.toString(partBytes) + "-").build();
        } else {
            Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
        }
        Response response = httpClient.newCall(request).execute();
        int statusCode = response.code();
        if ((statusCode == 200) || (statusCode == 206)) {
            Timber.d("DOWNLOAD SUCCEEDED, STATUS CODE: " + statusCode);
            // queue item here, "download" doesn't start until after we get a status code
            // queue item, use date to get a unique long, subtract to get a negative number (to distinguish from download manager items)
            Date startTime = new Date();
            long queueId = 0 - startTime.getTime();
            StorymakerQueueManager.addToQueue(context, queueId, targetFile.getName(), queueDao);
            targetFile.getParentFile().mkdirs();
            Timber.d("DOWNLOAD SUCCEEDED, GETTING ENTITY...");
            BufferedInputStream responseInput = new BufferedInputStream(response.body().byteStream());
            try {
                FileOutputStream targetOutput = new FileOutputStream(targetFile);
                byte[] buf = new byte[1024];
                int i;
                int oldPercent = 0;
                long thisTime;
                long lastTime = -1;
                int lastPercent = 0;
                while ((i = responseInput.read(buf)) > 0) {
                    // create status bar notification
                    int nPercent = StorymakerDownloadHelper.getDownloadPercent(context, fileName, installedDao);
                    thisTime = System.currentTimeMillis();
                    if (oldPercent == nPercent) {
                    // need to cut back on notification traffic
                    } else {
                        if (nPercent % 10 == 0 && nPercent != lastPercent) {
                            if (lastTime == -1 || (thisTime - lastTime) > 1000) {
                                lastPercent = nPercent;
                                oldPercent = nPercent;
                                lastTime = thisTime;
                                Notification nProgress = new Notification.Builder(context).setContentTitle(mAppTitle + " content download").setContentText(// assignment file names are meaningless uuids
                                indexItem.getTitle() + " - " + (nPercent / 10.0) + "%").setSmallIcon(android.R.drawable.arrow_down_float).setProgress(100, (nPercent / 10), false).setWhen(startTime.getTime()).build();
                                nManager.notify(nTag, nId, nProgress);
                            //Log.d("Storymaker Download Manager", "** NOTIFICATION ** " + nPercent );
                            }
                        }
                    }
                    targetOutput.write(buf, 0, i);
                }
                targetOutput.close();
                responseInput.close();
                Timber.d("SAVED DOWNLOAD TO " + targetFile);
            } catch (ConnectTimeoutException cte) {
                Timber.e(cte, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (CONNECTION EXCEPTION)");
            } catch (SocketTimeoutException ste) {
                Timber.e(ste, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (SOCKET EXCEPTION)");
            } catch (IOException ioe) {
                Timber.e(ioe, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (IO EXCEPTION)");
            }
            // remove from queue here, regardless of success
            StorymakerQueueManager.removeFromQueue(context, queueId, queueDao);
            // remove notification, regardless of success
            nManager.cancel(nTag, nId);
            // (assumes .tmp file will exist if download is interrupted)
            if (!handleFile(targetFile)) {
                Timber.e("ERROR DURING FILE PROCESSING FOR " + actualFileName);
            }
        } else {
            Timber.e("DOWNLOAD FAILED FOR " + actualFileName + ", STATUS CODE: " + statusCode);
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
        }
    // clean up connection
    // EntityUtils.consume(entity);
    // request.abort();
    // request.releaseConnection();
    } catch (IOException ioe) {
        Timber.e(ioe, "DOWNLOAD FAILED FOR " + actualFileName + ", EXCEPTION THROWN");
        StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) Request(com.squareup.okhttp.Request) Activity(android.app.Activity) IOException(java.io.IOException) Date(java.util.Date) Notification(android.app.Notification) Response(com.squareup.okhttp.Response) HttpResponse(ch.boye.httpclientandroidlib.HttpResponse) Proxy(java.net.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) ConnectTimeoutException(ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)

Example 79 with SocketAddress

use of java.net.SocketAddress in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method startServer.

@BeforeClass
public static void startServer() throws IOException {
    socketConnection = new SocketConnection(new Container() {

        public void handle(Request req, Response resp) {
            try {
                if (req.getPath().getPath().contains("/redirect/")) {
                    resp.setCode(303);
                    resp.add("Location", "/");
                } else {
                    if (req.getPath().getPath().contains("/timeout/")) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                    if (req.getPath().getPath().contains("/gzip/")) {
                        if (!"gzip".equals(req.getValue("Accept-Encoding"))) {
                            throw new IllegalStateException("Should accept gzip");
                        }
                        resp.set("Content-Encoding", "gzip");
                        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
                        gzipOutputStream.write("GZIP response".getBytes());
                        gzipOutputStream.close();
                    } else {
                        resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
                    }
                }
            } catch (IOException e) {
                throw new IllegalStateException(e);
            } finally {
                try {
                    resp.close();
                } catch (IOException ignored) {
                }
            }
        }
    });
    SocketAddress address = socketConnection.connect(new InetSocketAddress("localhost", 0));
    baseUrl = String.format("http://%s:%d", ((InetSocketAddress) address).getAddress().getHostAddress(), ((InetSocketAddress) address).getPort());
}
Also used : Response(org.simpleframework.http.Response) Container(org.simpleframework.http.core.Container) SocketConnection(org.simpleframework.transport.connect.SocketConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) InetSocketAddress(java.net.InetSocketAddress) Request(org.simpleframework.http.Request) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) BeforeClass(org.junit.BeforeClass)

Example 80 with SocketAddress

use of java.net.SocketAddress in project pinpoint by naver.

the class DefaultPinpointClientFactory method reconnect.

public PinpointClient reconnect(String host, int port) throws PinpointSocketException {
    SocketAddress address = new InetSocketAddress(host, port);
    ChannelFuture connectFuture = bootstrap.connect(address);
    PinpointClientHandler pinpointClientHandler = getSocketHandler(connectFuture, address);
    PinpointClient pinpointClient = new DefaultPinpointClient(pinpointClientHandler);
    traceSocket(pinpointClient);
    return pinpointClient;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

SocketAddress (java.net.SocketAddress)355 InetSocketAddress (java.net.InetSocketAddress)279 IOException (java.io.IOException)79 Test (org.junit.Test)73 Socket (java.net.Socket)48 InetAddress (java.net.InetAddress)35 Channel (org.jboss.netty.channel.Channel)33 SocketException (java.net.SocketException)27 UnknownHostException (java.net.UnknownHostException)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)23 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)23 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)20 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)19 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Proxy (java.net.Proxy)18 HashMap (java.util.HashMap)17 DatagramPacket (java.net.DatagramPacket)16 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.apache.log4j.Logger)16 Test (org.testng.annotations.Test)16