-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
OneLineAtATimeRemoteEntity.java
164 lines (136 loc) · 3.82 KB
/
OneLineAtATimeRemoteEntity.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.marginallyclever.robotOverlord.entity.remoteEntity;
import java.util.LinkedList;
import com.marginallyclever.communications.NetworkConnection;
import com.marginallyclever.communications.NetworkConnectionListener;
import com.marginallyclever.communications.NetworkConnectionManager;
import com.marginallyclever.convenience.StringHelper;
import com.marginallyclever.robotOverlord.entity.basicDataTypes.StringEntity;
import com.marginallyclever.robotOverlord.log.Log;
import com.marginallyclever.robotOverlord.swingInterface.view.ViewPanel;
/**
* Wraps all network connection stuff into a neat entity package designed to send one \n terminated string at a time.
*
* See also https://en.wikipedia.org/wiki/Messaging_pattern
*
* @author Dan Royer
* @since 1.6.0
*
*/
public class OneLineAtATimeRemoteEntity extends StringEntity implements NetworkConnectionListener {
/*
// pull the last connected port from prefs
private void loadRecentPortFromPreferences() {
recentPort = prefs.get("recent-port", "");
}
// update the prefs with the last port connected and refreshes the menus.
public void setRecentPort(String portName) {
prefs.put("recent-port", portName);
recentPort = portName;
//UpdateMenuBar();
}
*/
/**
*
*/
private static final long serialVersionUID = 1L;
public NetworkConnection connection;
public class NumberedCommand {
public int n;
public String c;
NumberedCommand(int nn,String cc) {
n=nn;
c=cc;
}
NumberedCommand() {
n=0;
c="";
}
}
private LinkedList<NumberedCommand> commands = new LinkedList<NumberedCommand>();
int lastNumberAdded=0;
int nextNumberToSend=0;
public OneLineAtATimeRemoteEntity() {
super();
setName("Connected to");
resetCommands();
}
private void resetCommands() {
commands.clear();
lastNumberAdded=0;
nextNumberToSend=0;
}
private String getCommand(int number) {
for( NumberedCommand nc : commands ) {
if(nc.n==number) return nc.c;
}
return null;
}
public void closeConnection() {
connection.closeConnection();
connection.removeListener(this);
connection=null;
resetCommands();
}
public void openConnection() {
connection = NetworkConnectionManager.requestNewConnection(null);
if(connection!=null) {
connection.addListener(this);
}
}
@Override
public void update(double dt) {
if(connection!=null) {
connection.update();
}
}
@Override
public void lineError(NetworkConnection arg0, int lineNumber) {
Log.error("REM RESEND "+lineNumber);
nextNumberToSend=lineNumber;
}
@Override
public void sendBufferEmpty(NetworkConnection arg0) {
if(!commands.isEmpty()) {
sendQueuedMessage(getCommand(nextNumberToSend++));
}
}
@Override
public void dataAvailable(NetworkConnection arg0, String data) {
setChanged();
notifyObservers(data);
if(data.contains(">")) {
sendQueuedMessage(getCommand(nextNumberToSend++));
}
}
public boolean isConnectionOpen() {
return (connection!=null) && (connection.isOpen());
}
/**
* called by someone else to start the process of sending a message
* @param command
*/
public void sendMessage(String command) {
if(!isConnectionOpen()) return;
//command.trim();
if(command.isEmpty()) return;
// add the number for error checking
command = "N"+lastNumberAdded+" "+command;
// add "there is a checksum" (*) + the checksum + end-of-line character
command += StringHelper.generateChecksum(command) + "\n";
commands.add(new NumberedCommand(lastNumberAdded,command));
lastNumberAdded++;
sendQueuedMessage(command);
}
private void sendQueuedMessage(String command) {
if(command==null) return;
try {
connection.sendMessage(command);
} catch (Exception e) {
Log.error("RemoteEntity.sendQueuedMessage failed: "+e.getLocalizedMessage());
}
}
@Override
public void getView(ViewPanel view) {
super.getView(view);
}
}