Search in sources :

Example 56 with SocketTimeoutException

use of java.net.SocketTimeoutException in project SmartCity-Market by TechnionYP5777.

the class Customer method viewCatalogProduct.

@Override
public CatalogProduct viewCatalogProduct(SmartCode c) throws CriticalError, CustomerNotConnected, ProductCatalogDoesNotExist {
    String serverResponse;
    log.info("Creating viewProductFromCatalog (in order to addPtoductToCart) command wrapper to customer with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    /* first: getting the product from the server */
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.VIEW_PRODUCT_FROM_CATALOG, Serialization.serialize(c)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    CommandWrapper $ = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler($.getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | CriticalError | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    log.info("viewProductFromCatalog command succeed.");
    return Serialization.deserialize($.getData(), CatalogProduct.class);
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists)

Example 57 with SocketTimeoutException

use of java.net.SocketTimeoutException in project SmartCity-Market by TechnionYP5777.

the class Customer method logout.

@Override
public void logout() throws CustomerNotConnected, CriticalError {
    String serverResponse;
    log.info("Creating customer logout command wrapper with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.LOGOUT)).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    try {
        resultDescriptorHandler(getCommandWrapper(serverResponse).getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    customerProfile = null;
    log.info("logout from server succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 58 with SocketTimeoutException

use of java.net.SocketTimeoutException in project spatial-portal by AtlasOfLivingAustralia.

the class ProgressController method get.

JSONObject get() {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/job?pid=").append(pid);
        LOGGER.debug("checking status every '" + timer.getDelay() + "' sec: " + sbProcessUrl.toString());
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
        client.getHttpConnectionManager().getParams().setSoTimeout(timer.getDelay());
        int result = client.executeMethod(get);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(get.getResponseBodyAsString());
        }
    } catch (SocketTimeoutException e) {
        LOGGER.debug("progress timeout exception, will be trying again.");
    } catch (Exception e) {
        LOGGER.error("error getting updated job info pid=" + pid, e);
    }
    return null;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) SocketTimeoutException(java.net.SocketTimeoutException)

Example 59 with SocketTimeoutException

use of java.net.SocketTimeoutException in project GNS by MobilityFirst.

the class Pinger method checkConnection.

/**
   *
   * @param host
   * @param port
   * @param timeoutMs
   * @return the time to connect or NOCONNECTION
   */
public static long checkConnection(String host, int port, int timeoutMs) {
    //default check value
    long start = NOCONNECTION;
    //default check value
    long end = NOCONNECTION;
    // default for bad connection
    long total = NOCONNECTION;
    //make an unbound socket
    Socket theSock = new Socket();
    try {
        InetAddress address = InetAddress.getByName(host);
        SocketAddress sockaddr = new InetSocketAddress(address, port);
        // Create the socket with a timeout
        // when a timeout occurs, we will get timout exp.
        // also time our connection this gets very close to the real time
        start = System.currentTimeMillis();
        theSock.connect(sockaddr, timeoutMs);
        end = System.currentTimeMillis();
    } catch (UnknownHostException e) {
        start = NOCONNECTION;
        end = NOCONNECTION;
    } catch (SocketTimeoutException e) {
        start = NOCONNECTION;
        end = NOCONNECTION;
    } catch (IOException e) {
        start = NOCONNECTION;
        end = NOCONNECTION;
    } finally {
        if (theSock != null) {
            try {
                theSock.close();
            } catch (IOException e) {
            }
        }
        if ((start != NOCONNECTION) && (end != NOCONNECTION)) {
            total = end - start;
        }
    }
    //returns NOCONNECTION if timeout
    return total;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 60 with SocketTimeoutException

use of java.net.SocketTimeoutException in project jdk8u_jdk by JetBrains.

the class JdpTestCase method run.

public void run() throws Exception {
    log.fine("Test started.");
    log.fine("Listening for multicast packets at " + connection.address.getHostAddress() + ":" + String.valueOf(connection.port));
    log.fine(initialLogMessage());
    log.fine("Pause in between packets is: " + connection.pauseInSeconds + " seconds.");
    startTime = System.currentTimeMillis();
    timeOut = connection.pauseInSeconds * TIME_OUT_FACTOR;
    log.fine("Timeout set to " + String.valueOf(timeOut) + " seconds.");
    MulticastSocket socket = connection.connectWithTimeout(timeOut * 1000);
    byte[] buffer = new byte[BUFFER_LENGTH];
    DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
    do {
        try {
            socket.receive(datagram);
            onReceived(extractUDPpayload(datagram));
        } catch (SocketTimeoutException e) {
            onSocketTimeOut(e);
        }
        if (hasTestLivedLongEnough()) {
            shutdown();
        }
    } while (shouldContinue());
    log.fine("Test ended successfully.");
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketTimeoutException(java.net.SocketTimeoutException) DatagramPacket(java.net.DatagramPacket)

Aggregations

SocketTimeoutException (java.net.SocketTimeoutException)721 IOException (java.io.IOException)420 Test (org.junit.Test)139 Socket (java.net.Socket)103 SocketException (java.net.SocketException)99 ServerSocket (java.net.ServerSocket)79 InputStream (java.io.InputStream)77 ConnectException (java.net.ConnectException)70 UnknownHostException (java.net.UnknownHostException)64 InetSocketAddress (java.net.InetSocketAddress)60 URL (java.net.URL)51 EOFException (java.io.EOFException)48 HttpURLConnection (java.net.HttpURLConnection)47 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)42 InterruptedIOException (java.io.InterruptedIOException)40 ArrayList (java.util.ArrayList)40 MalformedURLException (java.net.MalformedURLException)38 InputStreamReader (java.io.InputStreamReader)37 HashMap (java.util.HashMap)36 OutputStream (java.io.OutputStream)35