diff options
Diffstat (limited to 'src/org/gnunet/construct/MessageLoader.java')
-rw-r--r-- | src/org/gnunet/construct/MessageLoader.java | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/org/gnunet/construct/MessageLoader.java b/src/org/gnunet/construct/MessageLoader.java index d40dfda..7ba2e60 100644 --- a/src/org/gnunet/construct/MessageLoader.java +++ b/src/org/gnunet/construct/MessageLoader.java | |||
@@ -37,21 +37,26 @@ import java.util.Map; | |||
37 | 37 | ||
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Load message maps | 40 | * Load message maps, which contain the information the parse/write unions. |
41 | */ | 41 | */ |
42 | public class MessageLoader { | 42 | public class MessageLoader { |
43 | private static final Logger logger = LoggerFactory | 43 | private static final Logger logger = LoggerFactory |
44 | .getLogger(MessageLoader.class); | 44 | .getLogger(MessageLoader.class); |
45 | 45 | ||
46 | 46 | ||
47 | 47 | /** | |
48 | 48 | * Thrown when a trying to serialize an object that is not registered as a union type. | |
49 | */ | ||
49 | public static class UnknownUnionException extends RuntimeException { | 50 | public static class UnknownUnionException extends RuntimeException { |
50 | public UnknownUnionException(String msg) { | 51 | public UnknownUnionException(String msg) { |
51 | super(msg); | 52 | super(msg); |
52 | } | 53 | } |
53 | } | 54 | } |
54 | 55 | ||
56 | |||
57 | /** | ||
58 | * Thrown when parsing a union whose ID is not known. | ||
59 | */ | ||
55 | public static class UnknownUnionIdException extends RuntimeException { | 60 | public static class UnknownUnionIdException extends RuntimeException { |
56 | 61 | ||
57 | } | 62 | } |
@@ -86,19 +91,22 @@ public class MessageLoader { | |||
86 | } | 91 | } |
87 | 92 | ||
88 | public static void loadMessageMap(URL loc) { | 93 | public static void loadMessageMap(URL loc) { |
89 | if (loc == null) | 94 | if (loc == null) { |
90 | throw new RuntimeException("could not load message map"); | 95 | throw new RuntimeException("could not load message map"); |
96 | } | ||
91 | BufferedReader in = null; | 97 | BufferedReader in = null; |
92 | try { | 98 | try { |
93 | in = new BufferedReader(new InputStreamReader(loc.openStream())); | 99 | in = new BufferedReader(new InputStreamReader(loc.openStream())); |
94 | String line; | 100 | String line; |
95 | while ((line = in.readLine()) != null) { | 101 | while ((line = in.readLine()) != null) { |
96 | // skip empty lines and comments | 102 | // skip empty lines and comments |
97 | if (line.isEmpty() || line.charAt(0) == '#') | 103 | if (line.isEmpty() || line.charAt(0) == '#') { |
98 | continue; | 104 | continue; |
105 | } | ||
99 | String[] m = line.split("="); | 106 | String[] m = line.split("="); |
100 | if (m.length != 2) | 107 | if (m.length != 2) { |
101 | throw new RuntimeException("invalid message map format (separation by '=')"); | 108 | throw new RuntimeException("invalid message map format (separation by '=')"); |
109 | } | ||
102 | String[] left = m[0].split("[|]"); | 110 | String[] left = m[0].split("[|]"); |
103 | if (left.length != 2) { | 111 | if (left.length != 2) { |
104 | logger.debug(m[0]); | 112 | logger.debug(m[0]); |
@@ -134,8 +142,9 @@ public class MessageLoader { | |||
134 | 142 | ||
135 | private static void maybeClose(Closeable in) { | 143 | private static void maybeClose(Closeable in) { |
136 | try { | 144 | try { |
137 | if (in != null) | 145 | if (in != null) { |
138 | in.close(); | 146 | in.close(); |
147 | } | ||
139 | } catch (IOException e) { | 148 | } catch (IOException e) { |
140 | throw new RuntimeException("error closing stream: " + e.getMessage()); | 149 | throw new RuntimeException("error closing stream: " + e.getMessage()); |
141 | } | 150 | } |
@@ -164,7 +173,7 @@ public class MessageLoader { | |||
164 | 173 | ||
165 | Class<? extends MessageUnion> cls = map.get(tag); | 174 | Class<? extends MessageUnion> cls = map.get(tag); |
166 | if (cls == null) { | 175 | if (cls == null) { |
167 | throw new ProtocolViolation("don't know how to translate message of type " + tag); | 176 | throw new ProtocolViolationException("don't know how to translate message of type " + tag); |
168 | } | 177 | } |
169 | 178 | ||
170 | return cls; | 179 | return cls; |
@@ -173,10 +182,12 @@ public class MessageLoader { | |||
173 | 182 | ||
174 | public static int getUnionTag(Class<? extends MessageUnion> unionInterface, Class<? extends MessageUnion> unionCase) { | 183 | public static int getUnionTag(Class<? extends MessageUnion> unionInterface, Class<? extends MessageUnion> unionCase) { |
175 | Map<Class<? extends MessageUnion>, Integer> map = tagmap.get(unionInterface); | 184 | Map<Class<? extends MessageUnion>, Integer> map = tagmap.get(unionInterface); |
176 | if (map == null) | 185 | if (map == null) { |
177 | throw new AssertionError(String.format("%s is not a known union type", unionInterface)); | 186 | throw new AssertionError(String.format("%s is not a known union type", unionInterface)); |
178 | if (!map.containsKey(unionCase)) | 187 | } |
188 | if (!map.containsKey(unionCase)) { | ||
179 | throw new AssertionError(String.format("%s is not a known instance of %s", unionCase, unionInterface)); | 189 | throw new AssertionError(String.format("%s is not a known instance of %s", unionCase, unionInterface)); |
190 | } | ||
180 | return map.get(unionCase); | 191 | return map.get(unionCase); |
181 | } | 192 | } |
182 | } | 193 | } |