Subversion Repositories Projects

Rev

Rev 1539 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1539 - 1
package dongfang.mkt.serial;
2
 
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.OutputStream;
6
 
7
import dongfang.mkt.frames.RequestFrame;
8
import dongfang.mkt.frames.ResponseFrame;
9
import dongfang.mkt.io.MKCommPort;
10
 
11
/**
12
 * Thread safe!
13
 *
14
 * @author dongfang
15
public class FrameQueue {
16
        private final MKInputStream input;
17
        private final MKOutputStream output;
18
        // private List responseQueue;
19
        private ResponseFrame lastResponseFrame;
20
        private boolean doQueue = true;
21
 
22
        class Receiver extends Thread {
23
                public void run() {
24
                        while (doQueue) {
25
                                try {
26
                                        ResponseFrame f = input.getNextFrame();
27
                                        synchronized (FrameQueue.this.input) {
28
                                                lastResponseFrame = f;
29
                                                FrameQueue.this.input.notifyAll();
30
                                        }
31
                                } catch (IOException ex) {
32
                                        System.err.println(ex);
33
                                }
34
                        }
35
                        System.out.println("Receiver terminated.");
36
                }
37
        }
38
 
39
        public FrameQueue(MKCommPort port) throws IOException {
40
                super();
41
                this.input = new MKInputStream (port.getInputStream());
42
                this.output = new MKOutputStream(port.getOutputStream());
43
                new Receiver().start();
44
        }
45
 
46
        public FrameQueue(InputStream in, OutputStream out) throws IOException {
47
                super();
48
                this.input = new MKInputStream (in);
49
                this.output = new MKOutputStream(out);
50
                new Receiver().start();
51
        }
52
 
53
        public void sendRequest(RequestFrame f) throws IOException {
54
                synchronized (this.output) {
55
                        output.write(f);
56
                }
57
        }
58
 
59
        public ResponseFrame getResponseFor(RequestFrame f, int maxwait) throws IOException {
60
                ResponseFrame response;
61
                long timeout = System.currentTimeMillis() + maxwait;
62
                synchronized (input) {
63
                        while ((response = responseTo(f)) == null && System.currentTimeMillis() < timeout) {
64
                                try {
65
                                        input.wait(100);
66
                                } catch (InterruptedException ex) {
67
                                }
68
                        }
69
                }
70
                return response;
71
        }
72
 
73
        public void kill() {
74
                doQueue = false;
75
        }
76
 
77
        private ResponseFrame responseTo(RequestFrame f) {
78
                synchronized (this.input) {
79
                        if (lastResponseFrame != null && lastResponseFrame.isResponseTo(f)) {
80
                                ResponseFrame result = lastResponseFrame;
81
                                lastResponseFrame = null;
82
                                return result;
83
                        }
84
                        return null;
85
                }
86
        }
87
}
1564 - 88
 */