Search in sources :

Example 31 with UnknownHostException

use of java.net.UnknownHostException in project grpc-java by grpc.

the class DnsNameResolverTest method shutdownCancelsScheduledRetry.

@Test
public void shutdownCancelsScheduledRetry() throws Exception {
    String name = "foo.googleapis.com";
    UnknownHostException error = new UnknownHostException(name);
    MockResolver resolver = new MockResolver(name, 81, error);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onError(statusCaptor.capture());
    assertEquals(name, resolver.invocations.poll());
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.UNAVAILABLE, status.getCode());
    assertSame(error, status.getCause());
    // Retry scheduled
    assertEquals(1, fakeClock.numPendingTasks());
    // Shutdown cancelled the retry
    resolver.shutdown();
    assertEquals(0, fakeClock.numPendingTasks());
    verifyNoMoreInteractions(mockListener);
}
Also used : Status(io.grpc.Status) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 32 with UnknownHostException

use of java.net.UnknownHostException in project grpc-java by grpc.

the class DnsNameResolverTest method retry.

@Test
public void retry() throws Exception {
    String name = "foo.googleapis.com";
    UnknownHostException error = new UnknownHostException(name);
    InetAddress[] answer = createAddressList(2);
    MockResolver resolver = new MockResolver(name, 81, error, error, answer);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onError(statusCaptor.capture());
    assertEquals(name, resolver.invocations.poll());
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.UNAVAILABLE, status.getCode());
    assertSame(error, status.getCause());
    // First retry scheduled
    assertEquals(1, fakeClock.numPendingTasks());
    fakeClock.forwardMillis(TimeUnit.MINUTES.toMillis(1) - 1);
    assertEquals(1, fakeClock.numPendingTasks());
    // First retry
    fakeClock.forwardMillis(1);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener, times(2)).onError(statusCaptor.capture());
    assertEquals(name, resolver.invocations.poll());
    status = statusCaptor.getValue();
    assertEquals(Status.Code.UNAVAILABLE, status.getCode());
    assertSame(error, status.getCause());
    // Second retry scheduled
    assertEquals(1, fakeClock.numPendingTasks());
    fakeClock.forwardMillis(TimeUnit.MINUTES.toMillis(1) - 1);
    assertEquals(1, fakeClock.numPendingTasks());
    // Second retry
    fakeClock.forwardMillis(1);
    assertEquals(0, fakeClock.numPendingTasks());
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onUpdate(resultCaptor.capture(), any(Attributes.class));
    assertEquals(name, resolver.invocations.poll());
    assertAnswerMatches(answer, 81, Iterables.getOnlyElement(resultCaptor.getValue()));
    verifyNoMoreInteractions(mockListener);
}
Also used : Status(io.grpc.Status) UnknownHostException(java.net.UnknownHostException) Attributes(io.grpc.Attributes) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 33 with UnknownHostException

use of java.net.UnknownHostException in project grpc-java by grpc.

the class TestUtils method testServerAddress.

/**
   * Creates a new {@link InetSocketAddress} on localhost that overrides the host with
   * {@link #TEST_SERVER_HOST}.
   */
public static InetSocketAddress testServerAddress(int port) {
    try {
        InetAddress inetAddress = InetAddress.getByName("localhost");
        inetAddress = InetAddress.getByAddress(TEST_SERVER_HOST, inetAddress.getAddress());
        return new InetSocketAddress(inetAddress, port);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 34 with UnknownHostException

use of java.net.UnknownHostException in project j2objc by google.

the class GaiException method rethrowAsUnknownHostException.

public UnknownHostException rethrowAsUnknownHostException(String detailMessage) throws UnknownHostException {
    UnknownHostException newException = new UnknownHostException(detailMessage);
    newException.initCause(this);
    throw newException;
}
Also used : UnknownHostException(java.net.UnknownHostException)

Example 35 with UnknownHostException

use of java.net.UnknownHostException in project newsrob by marianokamp.

the class SyncChangedArticlesStatusJob method doSync.

protected void doSync(final boolean uploadOnly, final boolean manualSync) {
    final EntryManager entryManager = getEntryManager();
    final EntriesRetriever grf = entryManager.getEntriesRetriever();
    final IStorageAdapter fileContextAdapter = entryManager.getStorageAdapter();
    if (entryManager.isModelCurrentlyUpdated())
        return;
    entryManager.lockModel("SSer.doSync");
    PL.log("SynchronizationService. Used settings: " + SettingsRenderer.renderSettings(entryManager, new StringBuilder("\n")), SynchronizationService.this);
    // entryManager.runningThread = new Thread(new Runnable() {
    Throwable caughtThrowable = null;
    // public void run() {
    final Context ctx = getApplicationContext();
    PL.log("SynchronizationService - start", SynchronizationService.this);
    PL.log("Last successful login: " + entryManager.getLastSuccessfulLogin(), SynchronizationService.this);
    // Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
    Timing t = new Timing("Synchronization Runnable", this);
    long started = System.currentTimeMillis();
    Log.i(TAG, "Synchronization started at " + new Date().toString() + ". started=" + started);
    final SyncJobStatus syncJobStatus = new SyncJobStatus();
    // last used
    long lastUsed = entryManager.getLastUsed();
    ModelUpdateResult result = null;
    try {
        PL.log("Run Mark - in Try", SynchronizationService.this);
        if (!uploadOnly) {
            try {
                if (!Feed.restoreFeedsIfNeccesary(this))
                    Feed.saveFeedSettings(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        entryManager.fireModelUpdateStarted("Synchronization", uploadOnly, manualSync);
        List<Job> jobList = new LinkedList<Job>();
        Job deleteReadArticlesJob = new DeleteArticlesJob(SynchronizationService.this, entryManager, syncJobStatus);
        Job reduceToCapacityJob = new ReduceToCapacityJob(SynchronizationService.this, entryManager, syncJobStatus);
        if (!uploadOnly) {
            if (entryManager.shouldReadItemsBeDeleted())
                jobList.add(deleteReadArticlesJob);
            jobList.add(reduceToCapacityJob);
        }
        jobList.add(new Job("Submitting annotated articles", entryManager) {

            @Override
            public void run() throws Exception {
                if (entryManager.syncCurrentlyEnabled(manualSync))
                    entryManager.getEntriesRetriever().submitNotes(this);
            }
        });
        jobList.add(new SyncChangedArticlesStatusJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
        if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
            jobList.add(deleteReadArticlesJob);
        Job removeDeletedNotes = new Job("Removing submitted notes", entryManager) {

            @Override
            public void run() throws Exception {
                if (entryManager.syncCurrentlyEnabled(manualSync))
                    entryManager.getEntriesRetriever().removeDeletedNotes();
            }
        };
        if (!uploadOnly) {
            jobList.add(new Job("Unsubscribing from feeds", entryManager) {

                @Override
                public void run() throws Exception {
                    if (!entryManager.syncCurrentlyEnabled(manualSync))
                        return;
                    Cursor c = entryManager.getFeeds2UnsubscribeCursor();
                    try {
                        while (c.moveToNext()) {
                            String feedAtomId = c.getString(1);
                            PL.log("Unsubscribing: " + feedAtomId, SynchronizationService.this);
                            entryManager.getEntriesRetriever().unsubscribeFeed(feedAtomId);
                        }
                    } finally {
                        c.close();
                    }
                }
            });
        }
        if (!uploadOnly)
            jobList.add(removeDeletedNotes);
        if (!uploadOnly)
            jobList.add(new FetchUnreadArticlesJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
        if (!uploadOnly)
            jobList.add(new Job("Daily update of subscriptions (feed titles)", entryManager) {

                @Override
                public void run() throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
                    if (entryManager.syncCurrentlyEnabled(manualSync)) {
                        grf.updateSubscriptionList(entryManager, this);
                        entryManager.fireModelUpdated();
                    }
                }
            });
        if (!uploadOnly)
            jobList.add(reduceToCapacityJob);
        if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
            jobList.add(deleteReadArticlesJob);
        if (!uploadOnly) {
            // make sure that a manual sync moves the automatic sync
            // forward,
            // i.e. when pushing "Refresh" in the middle of a 24h sync,
            // reset timer to 0, so that it will be another 24h from this
            // point on
            entryManager.getScheduler().resetBackgroundSchedule();
            final float screenSizeFactor = getScreenSizeFactor(ctx);
            final float previewScaleFactor = ctx.getResources().getDisplayMetrics().density;
            jobList.add(new Job("Downloading articles", entryManager) {

                private int currentArticle = 0;

                private Collection<Long> entries2Download;

                @Override
                public void run() {
                    if (entryManager.getSharedPreferences().getString(EntryManager.SETTINGS_STORAGE_ASSET_DOWNLOAD, EntryManager.DOWNLOAD_YES).equals(EntryManager.DOWNLOAD_NO)) {
                        Log.d(TAG, "Downloading of assets is disabled in the settings. Therefore skipping downloading webpages.");
                        return;
                    }
                    Timing tSql = new Timing("SQL Query findAllToDownload", SynchronizationService.this);
                    entries2Download = entryManager.findAllArticleIds2Download();
                    tSql.stop();
                    Timing tOutter = new Timing("Downloading all " + entries2Download.size() + " pages or well, the ones that were downloaded", SynchronizationService.this);
                    for (Long articleId : entries2Download) {
                        // get the current data
                        // LATER use a real cursor and somehow find out when
                        // data became stale
                        Entry entry = entryManager.findArticleById(articleId);
                        if (entry == null)
                            continue;
                        if (!entryManager.downloadContentCurrentlyEnabled(manualSync))
                            return;
                        if (!fileContextAdapter.canWrite()) {
                            Log.d(TAG, "File context adapter (" + fileContextAdapter.getClass().getName() + ") cannot be written to at the moment. Mounted? Read Only? Not downloading web pages.");
                            return;
                        }
                        if (isCancelled())
                            break;
                        // System.out.println("----------------- " +
                        // entry.getTitle());
                        currentArticle++;
                        entryManager.fireStatusUpdated();
                        // ones
                        if (entry.getReadState() == ReadState.READ && !entry.isStarred() && !entry.isNote())
                            continue;
                        int resolvedDownloadPref = entry.getResolvedDownloadPref(entryManager);
                        if (resolvedDownloadPref == Feed.DOWNLOAD_HEADERS_ONLY) {
                            continue;
                        }
                        // check against the db, because in the
                        // meantime
                        // the
                        // read status might have changed
                        Timing tInner = new Timing("Downloading page " + entry.getAlternateHRef(), SynchronizationService.this);
                        try {
                            // check free space
                            float freeSpaceLeft = fileContextAdapter.megaBytesFree();
                            Log.d(TAG, String.format("Free space remaining for downloads: %.2f MB.", freeSpaceLeft));
                            if (freeSpaceLeft < 0) {
                                PL.log(TAG + ": Oh no, free space left is a negative value ;-( Ignoring it.", ctx);
                            } else if (freeSpaceLeft < fileContextAdapter.megaBytesThreshold()) {
                                PL.log(TAG + ": Not enough space left to download page.", ctx);
                                entryManager.getNewsRobNotificationManager().createSyncSpaceExceededProblemNotification(fileContextAdapter.megaBytesThreshold());
                                break;
                            }
                            boolean downloadTheWholePage = (resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_MOBILE_WEBPAGE || resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_WEBPAGE);
                            String summary = entry.getContent() != null ? entry.getContent() : UIHelper.linkize(entry.getAlternateHRef(), entry.getTitle());
                            WebPageDownloadDirector.downloadWebPage(entry.getShortAtomId(), new URL(entry.getBaseUrl(entryManager)), fileContextAdapter, this, summary, downloadTheWholePage, entryManager, manualSync);
                            if (true) {
                                File assetsDir = entry.getAssetsDir(entryManager);
                                System.out.println("generatePreview=" + new PreviewGenerator(ctx, assetsDir, (int) (100 * previewScaleFactor * screenSizeFactor), (int) (100 * previewScaleFactor * screenSizeFactor), (int) (6 * previewScaleFactor)).generatePreview());
                            }
                            // TODO
                            // only
                            // one
                            // instance
                            // TODO use display metrics?
                            // TODO use orientation? Larger thumbs for
                            // larger screens?
                            entry.setDownloaded(downloadTheWholePage ? Entry.STATE_DOWNLOADED_FULL_PAGE : Entry.STATE_DOWNLOADED_FEED_CONTENT);
                            entry.setError(null);
                            entryManager.fireModelUpdated();
                        } catch (Exception e) {
                            Log.e(TAG, "Problem dowloading page " + entry.getAlternateHRef() + ".", e);
                            Throwable cause = null;
                            if (e instanceof DownloadException) {
                                cause = ((DownloadException) e).getCause();
                                Log.d(TAG, "DownloadException cause=" + cause);
                            } else
                                Log.d(TAG, "Exception=" + e);
                            boolean downloadError = false;
                            if (e instanceof DownloadCancelledException || cause != null && (cause instanceof FileNotFoundException || cause instanceof SocketTimeoutException || cause instanceof SocketException || cause instanceof NoHttpResponseException || cause instanceof UnknownHostException || cause instanceof DownloadCancelledException || cause instanceof DownloadTimedOutException)) {
                                Log.d(TAG, "Caught a FNFE");
                            } else {
                                Log.d(TAG, "Marked download as error.");
                                downloadError = true;
                            }
                            StringBuilder renderedStackTrace = new StringBuilder();
                            U.renderStackTrace(e, renderedStackTrace);
                            entry.setError(cause != null ? "Cause: " + cause.getClass().getSimpleName() + ": " + cause.getMessage() : e.getClass().getSimpleName() + ": " + e.getMessage() + "\nStacktrace: " + renderedStackTrace);
                            entry.setDownloaded(downloadError ? Entry.STATE_DOWNLOAD_ERROR : Entry.STATE_NOT_DOWNLOADED);
                        }
                        entryManager.updatedDownloaded(entry);
                        tInner.stop();
                    }
                    tOutter.stop();
                }

                @Override
                public boolean isProgressMeassurable() {
                    return true;
                }

                @Override
                public int[] getProgress() {
                    return new int[] { currentArticle, entries2Download != null ? entries2Download.size() : 0 };
                }
            });
        }
        PL.log("Run Mark - Jobs added", this);
        entryManager.runJobs(jobList);
        Log.d(TAG, "NoOfEntriesUpdated=" + syncJobStatus.noOfEntriesUpdated);
        Log.d(TAG, "NoOfEntriesFetched=" + syncJobStatus.noOfEntriesFetched);
        PL.log("Run Mark - Mission accomplished. -> complete ", this);
        result = new SynchronizeModelSucceeded(syncJobStatus.noOfEntriesUpdated);
    } catch (Throwable throwable) {
        result = new SynchronizeModelFailed(throwable);
        Log.d(TAG, "Problem during synchronization.", throwable);
    } finally {
        PL.log("Run Mark - In Finally", this);
        entryManager.unlockModel("SSer.doSync");
        entryManager.clearCancelState();
        entryManager.fireModelUpdateFinished(result);
        entryManager.fireStatusUpdated();
        Log.i(TAG, "Synchronization finished at " + new Date().toString() + ". started=" + started);
        t.stop();
        if (!uploadOnly)
            entryManager.setLastSync(caughtThrowable == null);
        int noOfNewArticles = entryManager.getNoOfNewArticlesSinceLastUsed(lastUsed);
        entryManager.getNewsRobNotificationManager().notifyNewArticles(entryManager, lastUsed, noOfNewArticles);
        PL.log("Run Mark - End of Finally", this);
        resetLastStarted();
        releaseWakeLock();
    }
}
Also used : SocketException(java.net.SocketException) FileNotFoundException(java.io.FileNotFoundException) SyncJobStatus(com.newsrob.EntryManager.SyncJobStatus) Cursor(android.database.Cursor) DownloadCancelledException(com.newsrob.download.DownloadCancelledException) URL(java.net.URL) DownloadException(com.newsrob.download.DownloadException) Job(com.newsrob.jobs.Job) Context(android.content.Context) NoHttpResponseException(org.apache.http.NoHttpResponseException) DownloadTimedOutException(com.newsrob.download.DownloadTimedOutException) SynchronizeModelFailed(com.newsrob.jobs.SynchronizeModelFailed) ModelUpdateResult(com.newsrob.jobs.ModelUpdateResult) UnknownHostException(java.net.UnknownHostException) SynchronizeModelSucceeded(com.newsrob.jobs.SynchronizeModelSucceeded) Date(java.util.Date) ClientProtocolException(org.apache.http.client.ClientProtocolException) DownloadTimedOutException(com.newsrob.download.DownloadTimedOutException) NoHttpResponseException(org.apache.http.NoHttpResponseException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DownloadException(com.newsrob.download.DownloadException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DownloadCancelledException(com.newsrob.download.DownloadCancelledException) LinkedList(java.util.LinkedList) IStorageAdapter(com.newsrob.storage.IStorageAdapter) SocketTimeoutException(java.net.SocketTimeoutException) Timing(com.newsrob.util.Timing) PreviewGenerator(com.newsrob.util.PreviewGenerator) File(java.io.File)

Aggregations

UnknownHostException (java.net.UnknownHostException)884 InetAddress (java.net.InetAddress)365 IOException (java.io.IOException)246 InetSocketAddress (java.net.InetSocketAddress)91 Test (org.junit.Test)79 SocketException (java.net.SocketException)70 ArrayList (java.util.ArrayList)66 Socket (java.net.Socket)49 SocketTimeoutException (java.net.SocketTimeoutException)44 URL (java.net.URL)40 File (java.io.File)39 MalformedURLException (java.net.MalformedURLException)35 HashMap (java.util.HashMap)35 ConnectException (java.net.ConnectException)28 HttpURLConnection (java.net.HttpURLConnection)28 NetworkInterface (java.net.NetworkInterface)26 Properties (java.util.Properties)26 Inet4Address (java.net.Inet4Address)24 URI (java.net.URI)24 InputStream (java.io.InputStream)23