2009-04-22 15:52:23 +02:00
|
|
|
include("mjsunit");
|
2009-05-02 16:34:24 +02:00
|
|
|
|
|
|
|
var port = 12123;
|
2009-05-03 01:11:39 +02:00
|
|
|
var N = 1000;
|
2009-05-02 16:34:24 +02:00
|
|
|
var count = 0;
|
|
|
|
|
2009-05-04 12:08:13 +02:00
|
|
|
function Ponger (socket) {
|
2009-05-14 23:47:21 +02:00
|
|
|
socket.encoding = "UTF8";
|
|
|
|
socket.timeout = 0;
|
2009-05-02 16:34:24 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
puts("got socket.");
|
2009-05-02 16:34:24 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
socket.onReceive = function (data) {
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("open", socket.readyState);
|
2009-05-14 23:47:21 +02:00
|
|
|
//puts("server recved data: " + JSON.stringify(data));
|
2009-05-02 16:34:24 +02:00
|
|
|
assertTrue(count <= N);
|
2009-05-03 14:09:16 +02:00
|
|
|
stdout.print("-");
|
2009-05-03 01:01:42 +02:00
|
|
|
if (/PING/.exec(data)) {
|
2009-05-02 16:34:24 +02:00
|
|
|
socket.send("PONG");
|
2009-04-22 15:52:23 +02:00
|
|
|
}
|
2009-05-02 16:34:24 +02:00
|
|
|
};
|
2009-05-03 01:01:42 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
socket.onEOF = function () {
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("writeOnly", socket.readyState);
|
2009-05-03 01:01:42 +02:00
|
|
|
puts("ponger: onEOF");
|
2009-05-03 01:11:39 +02:00
|
|
|
socket.close();
|
2009-05-03 01:01:42 +02:00
|
|
|
};
|
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
socket.onDisconnect = function () {
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("closed", socket.readyState);
|
2009-05-03 01:01:42 +02:00
|
|
|
puts("ponger: onDisconnect");
|
2009-05-04 12:08:13 +02:00
|
|
|
socket.server.close();
|
2009-05-03 01:01:42 +02:00
|
|
|
};
|
2009-05-02 16:34:24 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
function onLoad() {
|
|
|
|
var server = new node.tcp.Server(Ponger);
|
|
|
|
server.listen(port);
|
|
|
|
|
|
|
|
var client = new node.tcp.Connection();
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("closed", client.readyState);
|
2009-05-14 23:47:21 +02:00
|
|
|
|
|
|
|
client.encoding = "UTF8";
|
2009-04-22 15:52:23 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
client.onConnect = function () {
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("open", client.readyState);
|
2009-05-14 23:47:21 +02:00
|
|
|
puts("client is connected.");
|
|
|
|
client.send("PING");
|
2009-05-02 16:34:24 +02:00
|
|
|
};
|
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
client.onReceive = function (data) {
|
2009-05-18 13:48:58 +02:00
|
|
|
assertEquals("open", client.readyState);
|
2009-05-14 23:47:21 +02:00
|
|
|
//puts("client recved data: " + JSON.stringify(data));
|
2009-05-02 16:34:24 +02:00
|
|
|
stdout.print(".");
|
2009-04-22 15:52:23 +02:00
|
|
|
assertEquals("PONG", data);
|
2009-05-03 01:01:42 +02:00
|
|
|
count += 1;
|
|
|
|
if (count < N) {
|
2009-05-14 23:47:21 +02:00
|
|
|
client.send("PING");
|
2009-05-03 01:01:42 +02:00
|
|
|
} else {
|
|
|
|
puts("sending FIN");
|
2009-05-14 23:47:21 +02:00
|
|
|
client.close();
|
2009-05-03 01:01:42 +02:00
|
|
|
}
|
2009-04-22 15:52:23 +02:00
|
|
|
};
|
2009-05-18 13:48:58 +02:00
|
|
|
|
2009-05-14 23:47:21 +02:00
|
|
|
client.onEOF = function () {
|
2009-05-03 01:01:42 +02:00
|
|
|
puts("pinger: onEOF");
|
2009-04-23 01:14:11 +02:00
|
|
|
assertEquals(N, count);
|
2009-04-22 16:04:05 +02:00
|
|
|
};
|
2009-04-22 15:52:23 +02:00
|
|
|
|
2009-05-02 16:34:24 +02:00
|
|
|
client.connect(port);
|
2009-04-22 15:52:23 +02:00
|
|
|
}
|