[go: up one dir, main page]

File: AltosUnitInfo.java

package info (click to toggle)
altos 1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 67,272 kB
  • sloc: ansic: 90,607; java: 39,048; makefile: 6,591; sh: 3,008; xml: 1,972; pascal: 1,597
file content (106 lines) | stat: -rw-r--r-- 2,633 bytes parent folder | download
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
/*
 * Copyright © 2018 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.altusmetrum.altoslib_13;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.concurrent.*;
import java.net.*;
import java.text.*;

public class AltosUnitInfo extends Thread {
	int			sn;
	int			rfcal;
	AltosUnitInfoListener	listener;
	String			json_string;

	public int sn() {
		return sn;
	}

	public int rfcal() {
		return rfcal;
	}

	void add(String line) {
		if (json_string == null) {
			json_string = line;
		} else {
			json_string = json_string + "\n" + line;
		}
	}

	void notify_complete() {
		rfcal = AltosLib.MISSING;

		if (json_string != null) {
			System.out.printf("json_string: %s\n", json_string);
			AltosJson json = AltosJson.fromString(json_string);
			System.out.printf("json: %s\n", json);
			String rfcal_string = null;
			try {
				AltosJson unitinfo = json.get("unitinfo");
				rfcal_string = unitinfo.get_string("rfcal", null);
				if (rfcal_string != null)
					rfcal = Integer.parseInt(rfcal_string);
			} catch (NumberFormatException ne) {
				System.out.printf("mal-formed integer %s\n", rfcal_string);
			} catch (IllegalArgumentException ie) {
				System.out.printf("mal-formed json\n");
			}
		}
		listener.notify_unit_info(this);
	}

	public void run() {
		try {
			String	format;

			format = System.getenv(AltosLib.unit_info_env);
			if (format == null)
				format = AltosLib.unit_info_url;

			String path = String.format(format, sn);

			URL url = new URL(path);

			System.out.printf("URL: %s\n", path);

			URLConnection uc = url.openConnection();

			InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
			BufferedReader in = new BufferedReader(in_stream);

			for (;;) {
				String line = in.readLine();
				if (line == null)
					break;
				add(line);
			}
		} catch (Exception e) {
			System.out.printf("file exception %s\n", e.toString());
		} finally {
			notify_complete();
		}
	}

	public AltosUnitInfo(int sn, AltosUnitInfoListener listener) {
		this.listener = listener;
		this.sn = sn;
		this.rfcal = AltosLib.MISSING;
		start();
	}
}