-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDateDemo.java
69 lines (61 loc) · 2.43 KB
/
DateDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.mastfrog.scamper.demo.dates;
import com.mastfrog.scamper.SctpServer;
import com.mastfrog.scamper.Control;
import com.mastfrog.scamper.DataEncoding;
import com.mastfrog.scamper.Message;
import com.mastfrog.scamper.MessageHandler;
import com.mastfrog.scamper.MessageType;
import com.mastfrog.scamper.SctpServerAndClientBuilder;
import com.mastfrog.scamper.compression.CompressionModule;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
/**
*
* @author Tim Boudreau
*/
public class DateDemo {
public static final MessageType WHAT_TIME_IS_IT = new MessageType("dateQuery", 1, 1);
public static final MessageType THE_TIME_IS = new MessageType("dateResponse", 1, 2);
public static void main(String[] args) throws IOException, InterruptedException {
Control<SctpServer> control = new SctpServerAndClientBuilder("datedemo")
.onPort(8007)
.withWorkerThreads(3)
.withModule(new CompressionModule())
.useLoggingHandler()
.withDataEncoding(DataEncoding.JSON) // BSON or JSON or JAVA_SERIALIZATION
.bind(WHAT_TIME_IS_IT, DateQueryHandler.class)
.bind(THE_TIME_IS, DateResponseHandler.class)
.buildServer(args);
SctpServer server = control.get();
ChannelFuture future = server.start();
future.sync();
}
static class DateQueryHandler extends MessageHandler<DateRecord, Map> {
DateQueryHandler() {
super(Map.class);
}
@Override
public Message<DateRecord> onMessage(Message<Map> data, ChannelHandlerContext ctx) {
DateRecord response = new DateRecord();
System.out.println("Send response for query " + data);
return THE_TIME_IS.newMessage(response);
}
}
static class DateResponseHandler extends MessageHandler<Map, DateRecord> {
DateResponseHandler() {
super(DateRecord.class);
}
@Override
public Message<Map> onMessage(Message<DateRecord> data, ChannelHandlerContext ctx) {
System.out.println("RECEIVE " + new Date(data.body.when) + " from " + ctx.channel().remoteAddress() + " at " + new Date());
return null;
}
}
public static class DateRecord implements Serializable {
public long when = System.currentTimeMillis();
}
}