Search in sources :

Example 51 with SocketTimeoutException

use of java.net.SocketTimeoutException in project opennms by OpenNMS.

the class PerfDataReceiver method createListenerThread.

public static Thread createListenerThread(final int port) {
    m_listenerThread = new Thread() {

        public void run() {
            this.setName("fail");
            try {
                ServerSocket ssocket = new ServerSocket(port);
                ssocket.setSoTimeout(0);
                while (true) {
                    try {
                        Socket socket = ssocket.accept();
                        InputStream is = socket.getInputStream();
                        PerformanceDataProtos.PerformanceDataReadings messages = PerformanceDataProtos.PerformanceDataReadings.parseFrom(is);
                        for (PerformanceDataProtos.PerformanceDataReading message : messages.getMessageList()) {
                            StringBuffer values = new StringBuffer();
                            values.append("{ ");
                            for (int i = 0; i < message.getValueCount(); i++) {
                                if (i != 0) {
                                    values.append(", ");
                                }
                                values.append(message.getValue(i));
                            }
                            values.append(" }");
                            System.out.println("Message received: { " + "path: \"" + message.getPath() + "\", " + "owner: \"" + message.getOwner() + "\", " + "timestamp: \"" + message.getTimestamp() + "\", " + "values: " + values.toString() + " }");
                        }
                    } catch (SocketTimeoutException e) {
                        System.err.println(e.getLocalizedMessage());
                        if (this.isInterrupted()) {
                            System.err.println("Interrupted.");
                            this.setName("notfailed");
                            return;
                        }
                    } catch (IOException e) {
                        System.err.println(e.getLocalizedMessage());
                    }
                }
            } catch (IOException e) {
                System.err.println(e.getLocalizedMessage());
            } catch (Throwable e) {
                System.err.println(e.getLocalizedMessage());
            }
        }
    };
    return m_listenerThread;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 52 with SocketTimeoutException

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

the class GetAllIngredientsTest method getAllIngredientsConnectionFailureTest.

@Test
public void getAllIngredientsConnectionFailureTest() {
    try {
        Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.GET_ALL_INGREDIENTS).serialize())).thenThrow(new SocketTimeoutException());
    } catch (IOException ¢) {
        fail();
    }
    try {
        customer.getAllIngredients();
        fail();
    } catch (CriticalError e) {
    /* success */
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CriticalError(SMExceptions.CommonExceptions.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) IOException(java.io.IOException) Test(org.junit.Test)

Example 53 with SocketTimeoutException

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

the class Customer method addProductToCart.

@Override
public void addProductToCart(SmartCode c, int amount) throws CriticalError, CustomerNotConnected, AmountBiggerThanAvailable, ProductPackageDoesNotExist, InvalidParameter {
    String serverResponse;
    log.info("Creating viewProductFromCatalog (in order to addPtoductToCart) command wrapper to customer with id: " + id);
    CatalogProduct catalogProduct;
    try {
        catalogProduct = viewCatalogProduct(c);
    } catch (ProductCatalogDoesNotExist e1) {
        log.fatal("Critical bug: failed to get catalog product from server");
        throw new CriticalError();
    }
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer(new CommandWrapper(id, CommandDescriptor.ADD_PRODUCT_TO_GROCERY_LIST, Serialization.serialize(new ProductPackage(c, amount, null))).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 | ProductCatalogDoesNotExist | GroceryListIsEmpty | AuthenticationError | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    ProductPackage productPackage1 = new ProductPackage(c, amount, null), productPackage2 = new ProductPackage(c, amount, null);
    groceryList.addProduct(productPackage1);
    addProductToCacheAndUpdateCartData(productPackage2, catalogProduct);
    log.info("addProductToGroceryList command succeed.");
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) ProductPackage(BasicCommonClasses.ProductPackage) CatalogProduct(BasicCommonClasses.CatalogProduct) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) SocketTimeoutException(java.net.SocketTimeoutException) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 54 with SocketTimeoutException

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

the class Customer method checkOutGroceryList.

@Override
public Double checkOutGroceryList() throws CriticalError, CustomerNotConnected, GroceryListIsEmpty {
    String serverResponse;
    Double $ = totalSum;
    log.info("Creating CHECKOUT_GROCERY_LIST command wrapper to customer with id: " + id);
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(id, CommandDescriptor.CHECKOUT_GROCERY_LIST)).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 | AmountBiggerThanAvailable | ProductPackageDoesNotExist | AuthenticationError | ProductCatalogDoesNotExist | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    /* update customer data: groceryList, cartProductCache, totalSum */
    groceryList = new GroceryList();
    cartProductCache = new HashMap<Long, CartProduct>();
    totalSum = Double.valueOf(0);
    totalProductsAmount = 0;
    log.info("CHECKOUT_GROCERY_LIST command succeed.");
    return $;
}
Also used : AuthenticationError(CustomerContracts.ACustomerExceptions.AuthenticationError) CriticalError(SMExceptions.CommonExceptions.CriticalError) GroceryList(BasicCommonClasses.GroceryList) CommandWrapper(ClientServerApi.CommandWrapper) InvalidParameter(CustomerContracts.ACustomerExceptions.InvalidParameter) SocketTimeoutException(java.net.SocketTimeoutException) AmountBiggerThanAvailable(CustomerContracts.ACustomerExceptions.AmountBiggerThanAvailable) InvalidCommandDescriptor(CustomerContracts.ACustomerExceptions.InvalidCommandDescriptor) ForgotPasswordWrongAnswer(CustomerContracts.ACustomerExceptions.ForgotPasswordWrongAnswer) CartProduct(BasicCommonClasses.CartProduct) ProductPackageDoesNotExist(CustomerContracts.ACustomerExceptions.ProductPackageDoesNotExist) UsernameAlreadyExists(CustomerContracts.ACustomerExceptions.UsernameAlreadyExists) ProductCatalogDoesNotExist(CustomerContracts.ACustomerExceptions.ProductCatalogDoesNotExist)

Example 55 with SocketTimeoutException

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

the class Customer method login.

@Override
public void login(String username, String password, boolean updateProductPictures) throws CriticalError, AuthenticationError {
    CommandWrapper cmdwrppr = null;
    String serverResponse = null;
    log.info("Creating login command wrapper for customer");
    establishCommunication(CustomerDefs.port, CustomerDefs.host, CustomerDefs.timeout);
    try {
        serverResponse = sendRequestWithRespondToServer((new CommandWrapper(CustomerDefs.loginCommandSenderId, CommandDescriptor.LOGIN_CUSTOMER, Serialization.serialize(new Login(username, password)))).serialize());
    } catch (SocketTimeoutException e) {
        log.fatal("Critical bug: failed to get respond from server");
        throw new CriticalError();
    }
    terminateCommunication();
    cmdwrppr = getCommandWrapper(serverResponse);
    try {
        resultDescriptorHandler(cmdwrppr.getResultDescriptor());
    } catch (InvalidCommandDescriptor | InvalidParameter | CustomerNotConnected | ProductCatalogDoesNotExist | AmountBiggerThanAvailable | ProductPackageDoesNotExist | GroceryListIsEmpty | UsernameAlreadyExists | ForgotPasswordWrongAnswer ¢) {
        log.fatal("Critical bug: this command result isn't supposed to return here");
        throw new CriticalError();
    }
    id = cmdwrppr.getSenderID();
    if (this instanceof RegisteredCustomer)
        customerProfile = Serialization.deserialize(cmdwrppr.getData(), CustomerProfile.class);
    //check for new product pictures asynchronous
    if (updateProductPictures)
        new UpdateProductPictures().start();
    log.info("Customer Login to server as succeed. Client id is: " + id);
}
Also used : CriticalError(SMExceptions.CommonExceptions.CriticalError) CustomerNotConnected(CustomerContracts.ACustomerExceptions.CustomerNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) GroceryListIsEmpty(CustomerContracts.ACustomerExceptions.GroceryListIsEmpty) Login(BasicCommonClasses.Login) 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)

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