comparison src/fdl/test/api/update/UpdateMetaEngine.java @ 111:29ed7feebf2a

add API test for in()out() and update().
author kazz
date Fri, 20 Aug 2010 21:22:51 +0900
parents
children 27874f473d48
comparison
equal deleted inserted replaced
110:8ae522e1a4bf 111:29ed7feebf2a
1 package fdl.test.api.update;
2
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.util.Date;
6
7 import fdl.MetaEngine;
8 import fdl.MetaLinda;
9 import fdl.PSXLinda;
10 import fdl.PSXReply;
11
12 public class UpdateMetaEngine implements MetaEngine {
13 private final static int targetId = 1001;
14
15 private String host;
16 private int port;
17 private int maxCount, count = 0;
18 private MetaLinda ml;
19 private PSXLinda linda;
20 private PSXReply reply;
21 private Date startTime, endTime;
22
23 private UpdateMetaEngine(MetaLinda metaLinda, String host, int port, int count) {
24 this.ml = metaLinda;
25 this.host = host;
26 this.port = port;
27 this.maxCount = count;
28 }
29
30 private void connect() {
31 try {
32 linda = ml.open(host, port);
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36 }
37
38 public void mainLoop(MetaLinda ml) {
39 if (count >= maxCount) {
40 endTime = new Date();
41 Long resultTime = new Long(endTime.getTime() - startTime.getTime());
42 System.out.println(resultTime);
43 System.exit(0);
44 }
45 if (reply == null) {
46 reply = linda.update(targetId, ByteBuffer.wrap("test".getBytes()));
47 return;
48 }
49 if (reply.ready()) {
50 reply = null;
51 count++;
52 }
53 }
54
55 public static UpdateMetaEngine create(MetaLinda metaLinda, String host, int port, int count) {
56 UpdateMetaEngine iome = new UpdateMetaEngine(metaLinda, host, port, count);
57 iome.connect();
58 iome.linda.out(targetId, ByteBuffer.wrap("test".getBytes()));
59 try {
60 iome.linda.sync();
61 } catch (IOException e) {
62 e.printStackTrace();
63 }
64 iome.startTime = new Date();
65 return iome;
66 }
67 }