RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
ProtocolStateMachine.java
1/*
2 * Copyright (c) 2013 Creative Sphere Limited.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing,
9 * software distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 *
14 * Contributors:
15 *
16 * Creative Sphere - initial API and implementation
17 *
18 */
19package org.ah.java.remotevmlauncher.protocol;
20
21import java.io.DataInputStream;
22import java.io.IOException;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.logging.Level;
26import java.util.logging.Logger;
27
33public class ProtocolStateMachine {
34
35 private Map<String, ProtocolProcessor> processorMap = new HashMap<String, ProtocolProcessor>();
36 private Logger logger = null;
37
38 public ProtocolStateMachine() {
39 }
40
41 public ProtocolStateMachine(Logger logger) {
42 this.logger = logger;
43 }
44
45 public Map<String, ProtocolProcessor> getProcessorMap() {
46 return processorMap;
47 }
48
49 public void register(ProtocolProcessor protolProcessor) {
50 this.processorMap.put(protolProcessor.getId(), protolProcessor);
51 }
52
53 public void processInput(DataInputStream dis) throws IOException {
54 String id = dis.readUTF();
55 if (logger != null && logger.isLoggable(Level.FINEST)) {
56 logger.finest("Received packet with id: " + id);
57 }
58 ProtocolProcessor processor = processorMap.get(id);
59
60 if (processor == null) {
61 throw new RuntimeException("Unknown playload with id '" + id + "'");
62 }
63 processor.receive(dis);
64 }
65}