-
Notifications
You must be signed in to change notification settings - Fork 17
/
Logfile.java
258 lines (220 loc) · 6.83 KB
/
Logfile.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright 2011 RoboViz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rv.comm.rcssserver;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import rv.Viewer;
import rv.comm.drawing.commands.Command;
/**
* Abstraction for a log that can be viewed frame by frame. Supports unpacked, single file zipped
* and tar.bz2 files.
*
* @author justin
*/
public class Logfile implements ILogfileReader {
/** used for sequentially playing frames */
private BufferedReader br;
/** the file to read from */
private final File logsrc;
/** index of the frame that is currently buffered */
private int curFramePtr;
/** the number of frames in the logfile, initially estimated */
private int numFrames;
/** stores the server message at the current frame position */
private String curFrameMsg;
/** viewer needed for parsing draw commands */
private final Viewer viewer;
/** if we should execute draw commands */
private final boolean execDrawCmds;
private final List<LogfileListener> listeners = new ArrayList<>();
/**
* Default constructor
*
* @param file
* the logfile to open
* @param viewer
* viewer needed for parsing draw commands
* @param execDrawCmds
* if draw commands should be executed
* @throws Exception
* if the logfile can not be opened
*/
public Logfile(File file, Viewer viewer, boolean execDrawCmds) throws Exception {
this.logsrc = file;
this.viewer = viewer;
this.execDrawCmds = execDrawCmds;
numFrames = 1700;
open();
}
/**
* Opens the file for buffered reading
*
* @throws IOException
*/
private void open() throws IOException {
br = TarBz2ZipUtil.createBufferedReader(logsrc);
if (br != null) {
curFrameMsg = br.readLine();
if (curFrameMsg != null && curFrameMsg.startsWith("[")) {
curFrameMsg = processDrawCmds(curFrameMsg);
}
}
curFramePtr = 0;
}
@Override
public boolean isValid() {
return br != null;
}
@Override
public boolean isAtBeginningOfLog() {
return curFramePtr == 0;
}
@Override
public boolean isAtEndOfLog() {
return curFrameMsg == null;
}
@Override
public void setNumFrames(int numFrames) {
this.numFrames = numFrames;
}
@Override
public int getNumFrames() {
return numFrames;
}
@Override
public int getCurrentFrame() {
return curFramePtr;
}
@Override
public String getCurrentFrameMessage() {
return curFrameMsg;
}
private String setCurrentFrame(int frame) throws IOException {
if (frame < curFramePtr) {
// we have a sequential reader, for stepping backwards we have to start from beginning
close();
open();
}
String line = curFrameMsg;
while (curFramePtr < frame && line != null) {
line = stepForward();
}
return line;
}
@Override
public void rewind() throws IOException {
close();
open();
}
@Override
public void close() {
try {
br.close();
} catch (Exception ex) {
}
}
@Override
public String stepForward() throws IOException {
if (isAtEndOfLog())
return null;
curFrameMsg = br.readLine();
if (curFrameMsg != null && curFrameMsg.startsWith("[")) {
curFrameMsg = processDrawCmds(curFrameMsg);
}
curFramePtr++;
if (curFramePtr >= numFrames) {
// the number of frames was estimated too low
numFrames++;
} else if (curFrameMsg == null) {
// the number of frames was estimated too high
numFrames = curFramePtr + 1;
}
return curFrameMsg;
}
@Override
public void stepBackward() throws IOException {
if (curFramePtr > 0) {
setCurrentFrame(curFramePtr - 1);
}
}
@Override
public void stepAnywhere(int frame) throws IOException {
if (frame < 0) {
frame = 0;
}
setCurrentFrame(frame);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
close();
}
@Override
public File getFile() {
return logsrc;
}
@Override
public void addListener(LogfileListener l) {
listeners.add(l);
}
@Override
public void removeListener(LogfileListener l) {
listeners.remove(l);
}
public String processDrawCmds(String line) {
if (line == null) {
return null;
}
while (line.startsWith("[")) {
for (LogfileListener l : listeners)
l.haveDrawCmds();
int endIndex = line.indexOf("]");
if (endIndex == -1) {
break;
}
if (execDrawCmds) {
String drawCmd = line.substring(0, endIndex);
String[] drawCmdByteValues = drawCmd.substring(1, drawCmd.length()).split(",");
byte[] drawCmdBytes = new byte[drawCmdByteValues.length];
for (int i = 0; i < drawCmdBytes.length; i++) {
try {
drawCmdBytes[i] = Byte.parseByte(drawCmdByteValues[i].trim());
} catch (Exception e) {
System.out.println(e);
}
}
ByteBuffer buf = ByteBuffer.wrap(drawCmdBytes);
while (buf.hasRemaining()) {
Command cmd = null;
try {
cmd = Command.parse(buf, viewer);
if (cmd != null) {
cmd.execute();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
line = line.substring(endIndex + 1);
}
return line;
}
}