1 | package com.framsticks.net.client3D; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.FileReader; |
---|
5 | import java.io.IOException; |
---|
6 | import java.util.ArrayList; |
---|
7 | |
---|
8 | /** |
---|
9 | * Mock server connection. Instead of connecting through the network it gets |
---|
10 | * data from text files. |
---|
11 | * |
---|
12 | * @author vorg |
---|
13 | */ |
---|
14 | public class CommunicationMock extends Communication { |
---|
15 | private String message; |
---|
16 | |
---|
17 | public void connect(String ip, int port) throws IOException { |
---|
18 | connected = true; |
---|
19 | } |
---|
20 | |
---|
21 | public void disconnect() throws IOException { |
---|
22 | connected = false; |
---|
23 | } |
---|
24 | |
---|
25 | public ArrayList<String> readMessage() throws IOException, |
---|
26 | InterruptedException, CommunicationErrorException { |
---|
27 | ArrayList<String> buffer = new ArrayList<String>(); |
---|
28 | if (message.indexOf("name,genotype,parts,joints") > -1) { |
---|
29 | buffer = readFile("res/mock/creature_Oafuz_info.txt"); |
---|
30 | } else if (message.indexOf("/joints p1,p2") > -1) { |
---|
31 | buffer = readFile("res/mock/creature_Oafuz_joints.txt"); |
---|
32 | } else if (message.indexOf("/parts x,y,z") > -1) { |
---|
33 | buffer = readFile("res/mock/creature_Oafuz_parts.txt"); |
---|
34 | } else if (message.indexOf("/neurodefs") > -1) { |
---|
35 | buffer = readFile("res/mock/creature_Oafuz_neurons.txt"); |
---|
36 | } else if (message.indexOf("/groups/+/creatures/+/") > -1) { |
---|
37 | buffer = readFile("res/mock/creatures.txt"); |
---|
38 | } else if (message.indexOf("/world") > -1) { |
---|
39 | buffer = readFile("res/mock/world.txt"); |
---|
40 | } |
---|
41 | return buffer; |
---|
42 | } |
---|
43 | |
---|
44 | public void sendMessage(String message) { |
---|
45 | Log.getInstance().log(">>>", message); |
---|
46 | this.message = message; |
---|
47 | } |
---|
48 | |
---|
49 | public ArrayList<String> getLog() { |
---|
50 | return new ArrayList<String>(); |
---|
51 | } |
---|
52 | |
---|
53 | private ArrayList<String> readFile(String path) { |
---|
54 | ArrayList<String> result = new ArrayList<String>(); |
---|
55 | try { |
---|
56 | BufferedReader reader = new BufferedReader(new FileReader(path)); |
---|
57 | String line = null; |
---|
58 | while ((line = reader.readLine()) != null) { |
---|
59 | result.add(line); |
---|
60 | } |
---|
61 | } catch (Exception e) { |
---|
62 | Log.getInstance().log("err", e.toString()); |
---|
63 | } |
---|
64 | |
---|
65 | return result; |
---|
66 | } |
---|
67 | |
---|
68 | } |
---|