Search in sources :

Example 1 with Thread_

use of suite.util.Thread_ in project suite by stupidsing.

the class NioDispatcherTest method testRequestResponse.

@Test
public void testRequestResponse() throws IOException, InterruptedException {
    RequestResponseMatcher matcher = new RequestResponseMatcher();
    ThreadPoolExecutor executor = Thread_.newExecutor();
    Iterate<Bytes> handler = request -> request;
    NioDispatcher<RequestResponseNioChannel> dispatcher = new NioDispatcherImpl<>(() -> NioChannelFactory.requestResponse(new RequestResponseNioChannel(), matcher, executor, handler));
    dispatcher.start();
    try (Closeable closeServer = dispatcher.listen(5151)) {
        InetAddress localHost = InetAddress.getLocalHost();
        InetSocketAddress address = new InetSocketAddress(localHost, 5151);
        RequestResponseNioChannel client = dispatcher.connect(address);
        for (String s : new String[] { "ABC", "WXYZ", "" }) {
            byte[] bs = s.getBytes(Constants.charset);
            Bytes request = Bytes.of(bs);
            Bytes response = matcher.requestForResponse(client, request);
            assertEquals(request, response);
            System.out.println("Request '" + s + "' is okay");
        }
    } finally {
        dispatcher.stop();
    }
    executor.awaitTermination(0, TimeUnit.SECONDS);
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Socket(java.net.Socket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Constants(suite.Constants) Source(suite.util.FunUtil.Source) Bytes(suite.primitive.Bytes) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) Iterate(suite.util.FunUtil.Iterate) Charset(java.nio.charset.Charset) Closeable(java.io.Closeable) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) InetSocketAddress(java.net.InetSocketAddress) Closeable(java.io.Closeable) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) Bytes(suite.primitive.Bytes) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with Thread_

use of suite.util.Thread_ in project suite by stupidsing.

the class CollectDataTest method test.

@Test
public void test() throws IOException {
    Streamlet<String> equities = // 
    Streamlet.concat(// 
    hkex.queryCompanies().map(company -> company.symbol), forex.invertedCurrencies.keys());
    for (String code : equities) {
        String urlString = yahoo.tableUrl(code, TimeRange.ages());
        URL url = To.url(urlString);
        try (FileOutputStream fos = new FileOutputStream("/data/storey/markets/" + code + ".csv")) {
            Copy.stream(To.inputStream(HttpUtil.get(url).out), fos);
        }
        Thread_.sleepQuietly(2000);
    }
}
Also used : HttpUtil(suite.http.HttpUtil) Streamlet(suite.streamlet.Streamlet) Copy(suite.util.Copy) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) Forex(suite.trade.Forex) TimeRange(suite.trade.TimeRange) FileOutputStream(java.io.FileOutputStream) URL(java.net.URL) Test(org.junit.Test)

Example 3 with Thread_

use of suite.util.Thread_ in project suite by stupidsing.

the class Scheduler method run.

public void run() {
    while (!schedules.isEmpty()) {
        List<Schedule> schedules1 = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nextWakeUpDateTime = // 
        Read.from(// 
        schedules).map(// 
        schedule -> schedule.nextRunDateTime).min(Object_::compare);
        Thread_.sleepQuietly(Duration.between(now, nextWakeUpDateTime).toMillis());
        for (Schedule schedule : schedules) if (now.isBefore(schedule.nextRunDateTime))
            schedules1.add(schedule);
        else
            try {
                schedules1.addAll(schedule.run.source());
            } catch (Exception ex) {
                LogUtil.error(ex);
            }
        schedules = schedules1;
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) List(java.util.List) Read(suite.streamlet.Read) Object_(suite.util.Object_) Duration(java.time.Duration) LocalDateTime(java.time.LocalDateTime) Thread_(suite.util.Thread_) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Object_(suite.util.Object_)

Example 4 with Thread_

use of suite.util.Thread_ in project suite by stupidsing.

the class DblFunUtil method suck.

/**
 * Sucks data from a sink and produce into a source.
 */
public static DblSource suck(Sink<DblSink> fun) {
    NullableSyncQueue<Double> queue = new NullableSyncQueue<>();
    DblSink enqueue = c -> enqueue(queue, c);
    Thread thread = Thread_.startThread(() -> {
        try {
            fun.sink(enqueue);
        } finally {
            enqueue(queue, EMPTYVALUE);
        }
    });
    return () -> {
        try {
            return queue.take();
        } catch (InterruptedException ex) {
            thread.interrupt();
            return Fail.t(ex);
        }
    };
}
Also used : DblSink(suite.primitive.DblPrimitives.DblSink) DblSource(suite.primitive.DblPrimitives.DblSource) Dbl_Obj(suite.primitive.DblPrimitives.Dbl_Obj) Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) DblObjSource(suite.primitive.DblPrimitives.DblObjSource) Sink(suite.util.FunUtil.Sink) Collections(java.util.Collections) Fail(suite.util.Fail) DblTest(suite.primitive.DblPrimitives.DblTest) DblObjPair(suite.primitive.adt.pair.DblObjPair) NullableSyncQueue(suite.util.NullableSyncQueue) DblSink(suite.primitive.DblPrimitives.DblSink)

Example 5 with Thread_

use of suite.util.Thread_ in project suite by stupidsing.

the class FltFunUtil method suck.

/**
 * Sucks data from a sink and produce into a source.
 */
public static FltSource suck(Sink<FltSink> fun) {
    NullableSyncQueue<Float> queue = new NullableSyncQueue<>();
    FltSink enqueue = c -> enqueue(queue, c);
    Thread thread = Thread_.startThread(() -> {
        try {
            fun.sink(enqueue);
        } finally {
            enqueue(queue, EMPTYVALUE);
        }
    });
    return () -> {
        try {
            return queue.take();
        } catch (InterruptedException ex) {
            thread.interrupt();
            return Fail.t(ex);
        }
    };
}
Also used : Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) FltSink(suite.primitive.FltPrimitives.FltSink) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) Flt_Obj(suite.primitive.FltPrimitives.Flt_Obj) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltObjSource(suite.primitive.FltPrimitives.FltObjSource) Sink(suite.util.FunUtil.Sink) FltTest(suite.primitive.FltPrimitives.FltTest) Collections(java.util.Collections) FltSource(suite.primitive.FltPrimitives.FltSource) Fail(suite.util.Fail) NullableSyncQueue(suite.util.NullableSyncQueue) FltSink(suite.primitive.FltPrimitives.FltSink)

Aggregations

Thread_ (suite.util.Thread_)10 LogUtil (suite.os.LogUtil)6 Source (suite.util.FunUtil.Source)6 Collections (java.util.Collections)5 Iterator (java.util.Iterator)5 Fail (suite.util.Fail)5 Fun (suite.util.FunUtil.Fun)5 Sink (suite.util.FunUtil.Sink)5 Source2 (suite.util.FunUtil2.Source2)5 NullableSyncQueue (suite.util.NullableSyncQueue)5 List (java.util.List)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Constants (suite.Constants)2 To (suite.util.To)2 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1