@petermd
eclipsecon 2014
for (;;) { fd = accept(listenfd, ...);
if ( (pid = fork()) == 0 ) { close(listenfd);
/* Process request */
exit(0); } close(fd); }
do { rc=poll(fds, nfds, timeout); current_size = nfds; for (i = 0; i < current_size; i++) { if(fds[i].revents == 0) continue; rc = recv(fds[i].fd, buffer, sizeof(buffer), 0); if (rc < 0) {
/* Not ready yet */ } .
/* Do something */ . } while(TRUE);
fs.readdir(source, function(err, files) {
if(err){
console.log('Error finding files: '+err)
}else{
files.forEach(function(filename,fileIndex){
console.log(filename)
gm(source+filename).size(function(err,values){
if(err){
console.log('Error identifying file size: '+err)
}else{
console.log(filename+' : '+values)
aspect=(values.width/values.height)
widths.forEach(function(width,widthIndex){
height=Math.round(width/aspect)
console.log('resizing '+filename+'to '+height+'x'+height)
this
.resize(width,height)
.write(destination+'w'+width+'_'+filename,function(err) {
if(err)
console.log('Error writing file: '+err)
})
}.bind(this))
}
})
})
}
})
(not handling errors is worse)
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail;
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
goto fail;
"a library for composing asynchronous and event-based programs by using observable sequences"
interface Observer<T> {
void onNext(T value);
void onCompleted();
void onError(Throwable t);
}
myStringService
.fetch()
.subscribe { v -> println("Got: "+v) }
myStringService
.fetch()
.map { v -> v.toLowerCase() }
.subscribe(...)
TRANSFORM ONLINE
myStringService
.fetch()
.flatMap { v -> myTranslationService.translate(v) }
.subscribe(...)
MERGE
Observable<String> s=myStringService.fetch().publish()
Observable<String> lc=s.map { v -> v.toLowerCase() }
Observable<String> tr=s.flatMap { v -> myTranslateService.translate(v) }
Observable
.zip(lc,tr,{ word, translated -> { return word+"="+translated } })
.subscribe(
{ v -> println(v) },
{ e -> println("ERROR:"+e) }
)
EVENT BUS
RxEventBus eb=new RxEventBus(vertx.eventBus());
eb.<String>registerHandler("ping")
.subscribe( (RxMessage<String>) (m) -> {
m.reply("pong")
})
eb
.send("ping","yolo")
.subscribe((RxMessage<String>)(m) -> {
out.println("got:"+m.body());
});
HTTP SERVER
RxHttpServer server=new RxHttpServer(...);
server
.http()
.subscribe((RxHttpServerRequest)(req) -> {
// Handle request and request.toObservable() inside
// a new Observable
});
HTTPCLIENT
RxHttpClient client=new RxHttpClient(...);
client
.getNow("/someurl)
.flatMap(parseResponse)
.map(handleResponse)
// Subscription used to map output to response
.subscribe(...)
eb.send("guest","you'll have tea")
.flatMap((RxMessage)(resp) -> {
return resp.reply("ah you will");
})
.flatMap((RxMessage)(resp) -> {
return resp.reply("pour-tea");
})
.subscribe(...)
rx.<String>observeStream("pint",1)
.map((Func1<RxStream,Integer>)(s) -> {
// request next block
s.next(...);
// forward current block
return s.value();
})
.subscribe(...)
Observable<HttpServerRequest> req;
req=Observable
.just(req)
.filter(checkValid) // remove request if handled eg redirect
.map(checkAuth) // map it if just adding data
.flatMap(lookupAuth) // flatMap if you need a remote call
.map(..process request..)
.subscribe(
renderResponse,
renderError
)
BASIC TYPES
/** Render string */ public static void render(HttpServerResponse resp, String value) { resp .headers().add("Content-Type","text/plain;charset=UTF8"); resp .setChunked(true) .end(value); }
void render(HttpServerResponse resp, JsonObject value) {..}
void renderErr(HttpServerResponse resp, int code, String msg) {..}
OBSERVABLE
void render(HttpServerResponse resp, Observable ob) {
// map value + error to renderer
// completed without value -> response already sent
}
HttpSupport.render(resp,process);
HTTP UPLOAD
Observable<Buffer> in=request.toObservable();
HttpCommand<> cmd=new HttpCommand(request,in.map(..));
Observable<HttpCommand> cmd=HttpSupport.waitFor(cmd);
/** Return Observable<HttpCommand<JsonObject>> */
public static Observable jsonRequest(RxHttpServerRequest request) {
HttpCommand req=
new HttpCommand(
request,
request
.asObservable()
.reduce(mergeBuffers)
.map(toJson));
return waitFor(req);
}
USAGE
Obserable .just(request) .flatMap(HttpSupport.jsonRequest) .map((HttpCommand)(cmd) -> {
// cmd.body is the JsonObject })
eventBus
.observeSend("db",request)
.retry(2)
.subscribe(...)
Observable<String> firehose=rx.<String>observeStream("twitter","*")
.map((RxStream)(s) -> { s.next(..); return s.value(); })
.map((String)(s) -> { ..render.. })
RxSupport.stream(firehose,output);
Regulator reg=new Regulator();
Observable<String> firehose=rx.<String>observeStream("twitter","*")
.lift(reg)
.map((RxStream)(s) -> { s.next(..); return s.value(); })
.map((String)(s) -> { ..render.. })
reg.stream(firehose,output);