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);
}
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);
}
}
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;
}
}
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);
}
};
}
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);
}
};
}
Aggregations