summaryrefslogtreecommitdiff
path: root/NET/worlds/console/BlackBoxCommand.java
blob: bfafc437c10f636335840792e4cd12e6ba282fa4 (plain) (blame)
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
package NET.worlds.console;

import NET.worlds.core.Std;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public abstract class BlackBoxCommand {
   int commandType;
   long startTime;
   BlackBoxCallback callback;
   boolean waiting = false;

   void timestamp(long basetime) {
      this.startTime = Std.getFastTime() - basetime;
   }

   public boolean execute(BlackBoxCallback c) {
      if (this.waiting) {
         return false;
      } else {
         this.callback = c;
         if (c != null) {
            this.waiting = true;
         }

         return this.execute();
      }
   }

   abstract boolean execute();

   void save(DataOutputStream dos) throws IOException {
      dos.writeInt(this.commandType);
      dos.writeLong(this.startTime);
   }

   void load(DataInputStream dis) throws IOException {
      this.startTime = dis.readLong();
   }

   void doCallback(boolean ok) {
      if (this.callback != null) {
         this.waiting = false;
         this.callback.commandCompleted(this, ok);
      }
   }
}