java: Bump java library versions for next release
[fw/altos] / altoslib / AltosLog.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.io.*;
21 import java.text.*;
22 import java.util.concurrent.*;
23
24 /*
25  * This creates a thread to capture telemetry data and write it to
26  * a log file
27  */
28 public class AltosLog implements Runnable {
29
30         LinkedBlockingQueue<AltosLine>  input_queue;
31         LinkedBlockingQueue<String>     pending_queue;
32         int                             serial;
33         int                             flight;
34         int                             receiver_serial;
35         FileWriter                      log_file;
36         Thread                          log_thread;
37         AltosFile                       file;
38         AltosLink                       link;
39
40         private void close_log_file() {
41                 if (log_file != null) {
42                         try {
43                                 log_file.close();
44                         } catch (IOException io) {
45                         }
46                         log_file = null;
47                 }
48         }
49
50         public void close() {
51                 link.remove_monitor(input_queue);
52                 close_log_file();
53                 if (log_thread != null) {
54                         log_thread.interrupt();
55                         log_thread = null;
56                 }
57         }
58
59         public File file() {
60                 return file;
61         }
62
63         boolean open (AltosState state) throws IOException, InterruptedException {
64                 AltosFile       a = new AltosFile(state);
65
66                 log_file = new FileWriter(a, true);
67                 if (log_file != null) {
68                         while (!pending_queue.isEmpty()) {
69                                 String s = pending_queue.take();
70                                 log_file.write(s);
71                                 log_file.write('\n');
72                         }
73                         log_file.flush();
74                         file = a;
75                 }
76                 return log_file != null;
77         }
78
79         public void run () {
80                 try {
81                         AltosState      state = new AltosState();
82                         AltosConfigData receiver_config = link.config_data();
83                         state.set_receiver_serial(receiver_config.serial);
84                         for (;;) {
85                                 AltosLine       line = input_queue.take();
86                                 if (line.line == null)
87                                         continue;
88                                 try {
89                                         AltosTelemetry  telem = AltosTelemetry.parse(line.line);
90                                         state = state.clone();
91                                         telem.update_state(state);
92                                         if (state.serial != serial || state.flight != flight || log_file == null)
93                                         {
94                                                 close_log_file();
95                                                 serial = state.serial;
96                                                 flight = state.flight;
97                                                 if (state.serial != AltosLib.MISSING && state.flight != AltosLib.MISSING)
98                                                         open(state);
99                                         }
100                                 } catch (ParseException pe) {
101                                 } catch (AltosCRCException ce) {
102                                 }
103                                 if (log_file != null) {
104                                         log_file.write(line.line);
105                                         log_file.write('\n');
106                                         log_file.flush();
107                                 } else
108                                         pending_queue.put(line.line);
109                         }
110                 } catch (InterruptedException ie) {
111                 } catch (TimeoutException te) {
112                 } catch (IOException ie) {
113                 }
114                 close();
115         }
116
117         public AltosLog (AltosLink link) {
118                 pending_queue = new LinkedBlockingQueue<String> ();
119                 input_queue = new LinkedBlockingQueue<AltosLine> ();
120                 link.add_monitor(input_queue);
121                 serial = -1;
122                 flight = -1;
123                 this.link = link;
124                 log_file = null;
125                 log_thread = new Thread(this);
126                 log_thread.start();
127         }
128 }