All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aaronbedra.tcp.TcpRequester Maven / Gradle / Ivy

package com.aaronbedra.tcp;

import com.aaronbedra.Requester;
import com.aaronbedra.tcp.types.Connectable;
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
import com.jnape.palatable.lambda.monad.MonadRec;
import com.jnape.palatable.lambda.monad.transformer.builtin.ReaderT;
import lombok.AllArgsConstructor;
import lombok.Value;

import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
import static com.jnape.palatable.lambda.monad.transformer.builtin.ReaderT.readerT;
import static lombok.AccessLevel.PRIVATE;

@Value
@AllArgsConstructor(access = PRIVATE)
public class TcpRequester> implements Requester> {
    Connectable connectable;
    int timeout;

    public static > TcpRequester tcpRequester(Connectable connectable, int timeout) {
        return new TcpRequester<>(connectable, timeout);
    }

    @Override
    public ReaderT> request() {
        return readerT(port -> connectable.connect(port, timeout)
                .fmap(either -> either
                        .fmap(constantly(true))
                        .recover(constantly(false)))
                .fmap(success -> tuple(connectable.getHost(), port, success)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy