Skip to content

Commit d5bed9d

Browse files
committed
More cloud enhancements.
1 parent fb26c60 commit d5bed9d

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.encog.cloud;
22

3+
import org.encog.cloud.basic.CloudPacket;
4+
import org.encog.cloud.basic.CommunicationLink;
5+
36
public interface CloudListener {
4-
void notifyPacket();
5-
void notifyConnections();
7+
void notifyPacket(CloudPacket packet);
8+
void notifyConnections(CommunicationLink link, boolean hasOpened);
69
}

src/main/java/org/encog/cloud/basic/CommunicationLink.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,43 +77,20 @@ public CommunicationLink(CloudNode node, Socket s) {
7777
}
7878

7979

80-
public void writePacket(int command, String[] args) {
80+
public void writePacket(String command, Object[] args) {
8181
try {
82-
// first determine length
83-
int length = 0;
84-
for(int i=0;i<args.length;i++) {
85-
length+=args[i].length();
86-
length++;
82+
StringBuilder line = new StringBuilder();
83+
line.append("\"");
84+
line.append(command);
85+
line.append("\"");
86+
for (int i = 0; i < args.length; i++) {
87+
line.append(",\"");
88+
line.append(args[i].toString());
89+
line.append("\"");
8790
}
88-
89-
// write the packet
90-
this.outputToRemote.writeByte('E');
91-
this.outputToRemote.writeByte('N');
92-
this.outputToRemote.writeByte('C');
93-
this.outputToRemote.writeByte('O');
94-
this.outputToRemote.writeByte('G');
95-
this.outputToRemote.writeByte(0);// string terminator
96-
this.outputToRemote.writeByte(0);//version
97-
this.outputToRemote.writeByte(command);
98-
this.outputToRemote.writeByte(0);//count
99-
this.outputToRemote.writeLong(0);//time
100-
this.outputToRemote.writeInt(length);//size
101-
102-
// write the arguments
103-
byte[] b = new byte[length];
104-
int index = 0;
105-
106-
for(int i=0;i<args.length;i++) {
107-
String str = args[i];
108-
for(int j=0;j<str.length();j++) {
109-
b[index++] = (byte)str.charAt(j);
110-
}
111-
b[index++] = 0;
112-
}
113-
114-
this.outputToRemote.write(b);
91+
line.append("\n");
92+
this.outputToRemote.writeChars(line.toString());
11593
this.flushOutput();
116-
11794
} catch (IOException ex) {
11895
throw new CloudError(ex);
11996
}
@@ -125,7 +102,6 @@ public CloudPacket readPacket() {
125102
String str = this.inputFromRemote.readLine();
126103
List<String> list = parseLine.parse(str);
127104
this.packets++;
128-
this.parentNode.notifyListenersPacket();
129105

130106
EncogLogging.log(EncogLogging.LEVEL_DEBUG, "Received Packet: " + str);
131107
return new CloudPacket(list);
@@ -161,4 +137,9 @@ public void close() {
161137
}
162138

163139
}
140+
141+
public void requestSignal(List<String> dataSource) {
142+
writePacket("signals",dataSource.toArray());
143+
144+
}
164145
}

src/main/java/org/encog/cloud/node/CloudNode.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.encog.cloud.CloudListener;
3535
import org.encog.cloud.basic.CloudError;
36+
import org.encog.cloud.basic.CloudPacket;
3637
import org.encog.cloud.basic.CommunicationLink;
3738
import org.encog.util.logging.EncogLogging;
3839

@@ -72,7 +73,7 @@ public void run() {
7273
Socket connectionSocket = listenSocket.accept();
7374
EncogLogging.log(EncogLogging.LEVEL_DEBUG, "Connection from: " + connectionSocket.getRemoteSocketAddress().toString());
7475
CommunicationLink link = new CommunicationLink(this,connectionSocket);
75-
notifyListenersConnections();
76+
notifyListenersConnections(link, true);
7677
HandleClient hc = new HandleClient(this, link);
7778
this.connections.add(hc);
7879
Thread t = new Thread(hc);
@@ -144,16 +145,21 @@ public void clearListeners() {
144145
this.listeners.clear();
145146
}
146147

147-
public void notifyListenersConnections() {
148-
for( CloudListener listener : this.listeners ) {
149-
listener.notifyConnections();
148+
public void notifyListenersConnections(CommunicationLink link, boolean hasOpened) {
149+
Object[] list = this.listeners.toArray();
150+
151+
for(int i=0;i<list.length;i++) {
152+
CloudListener listener = (CloudListener)list[i];
153+
listener.notifyConnections(link, hasOpened);
150154
}
151155
}
152156

153-
public void notifyListenersPacket() {
154-
for( CloudListener listener : this.listeners ) {
155-
listener.notifyPacket();
157+
public void notifyListenersPacket(CloudPacket packet) {
158+
Object[] list = this.listeners.toArray();
159+
160+
for(int i=0;i<list.length;i++) {
161+
CloudListener listener = (CloudListener)list[i];
162+
listener.notifyPacket(packet);
156163
}
157164
}
158-
159165
}

src/main/java/org/encog/cloud/node/HandleClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public void run() {
6868
else if( packet.getCommand().equalsIgnoreCase("goodbye") ) {
6969
this.done = true;
7070
}
71+
else {
72+
this.server.notifyListenersPacket(packet);
73+
}
7174
}
7275
} catch (CloudError ex) {
7376
EncogLogging.log(EncogLogging.LEVEL_DEBUG,"Client ended connection.");
@@ -76,7 +79,7 @@ else if( packet.getCommand().equalsIgnoreCase("goodbye") ) {
7679
}
7780
this.link.close();
7881
this.server.getConnections().remove(this);
79-
this.server.notifyListenersConnections();
82+
this.server.notifyListenersConnections(this.link,false);
8083
EncogLogging.log(EncogLogging.LEVEL_DEBUG,"Shutting down client handler");
8184
}
8285

0 commit comments

Comments
 (0)