Skip to content

Commit

Permalink
Asynch Client now supported in client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Jul 23, 2013
1 parent 38c7889 commit e1e097b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 41 deletions.
89 changes: 51 additions & 38 deletions Most-Pixels-Ever-Processing/src/mpe/client/TCPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,31 @@ public TCPClient(String _fileString, PApplet _p, boolean _autoMode) {
loadSettings(_fileString);
connect(hostName, serverPort, id);

// look for a method called "frameEvent" in the parent PApplet, with one
// argument of type Client
try {
frameEventMethod = p5parent.getClass().getMethod("frameEvent",
new Class[] { TCPClient.class });
} catch (Exception e) {
System.out.println("You are missing the frameEvent() method. " + e);
}

try {
resetEventMethod = p5parent.getClass().getMethod("resetEvent",
new Class[] { TCPClient.class });
} catch (Exception e) {
System.out.println("You are missing the resetEvent() method. " + e);
}

if (autoMode) {
p5parent.registerMethod("draw", this);
if (!asynchronous) {

// look for a method called "frameEvent" in the parent PApplet, with one
// argument of type Client
try {
frameEventMethod = p5parent.getClass().getMethod("frameEvent",
new Class[] { TCPClient.class });
} catch (Exception e) {
System.out.println("You are missing the frameEvent() method. " + e);
}

try {
resetEventMethod = p5parent.getClass().getMethod("resetEvent",
new Class[] { TCPClient.class });
} catch (Exception e) {
System.out.println("You are missing the resetEvent() method. " + e);
}

if (autoMode) {
p5parent.registerMethod("draw", this);
}
} else {
// TODO implement dataEvent() method for asynch client?

}

}
Expand All @@ -151,26 +158,28 @@ public void draw() {
if (running && rendering) {
placeScreen();

if (reset) {
try {
resetEventMethod.invoke(p5parent, new Object[] { this });
} catch (Exception e) {
err("Could not invoke the \"resetEvent()\" method for some reason.");
e.printStackTrace();
resetEventMethod = null;
}
} else if (frameEventMethod != null) {
try {
// call the method with this object as the argument!
frameEventMethod.invoke(p5parent, new Object[] { this });
} catch (Exception e) {
err("Could not invoke the \"frameEvent()\" method for some reason.");
e.printStackTrace();
frameEventMethod = null;
}
}
if (!asynchronous) {
if (reset) {
try {
resetEventMethod.invoke(p5parent, new Object[] { this });
} catch (Exception e) {
err("Could not invoke the \"resetEvent()\" method for some reason.");
e.printStackTrace();
resetEventMethod = null;
}
} else if (frameEventMethod != null) {
try {
// call the method with this object as the argument!
frameEventMethod.invoke(p5parent, new Object[] { this });
} catch (Exception e) {
err("Could not invoke the \"frameEvent()\" method for some reason.");
e.printStackTrace();
frameEventMethod = null;
}
}
done();
} else {
// TODO: deal with asynch connection receiving data
}
}
}
Expand Down Expand Up @@ -202,7 +211,7 @@ private void loadSettings(String filename) {

String v = xml.getChild("verbose").getContent();
VERBOSE = Boolean.parseBoolean(v);

// Implement name
if (!asynchronous) {
int w = xml.getChild("local_dimensions/width").getIntContent();
Expand Down Expand Up @@ -522,11 +531,15 @@ public void start() {
* This method should only be called internally by Thread.start().
*/
public void run() {

if (VERBOSE) out("Running!");

// let the server know that this client is ready to start.
send("S|" + id);
if (asynchronous) {
send("A|" + id);
} else {
send("S|" + id);
}

try {
while (running) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mpe.examples;

import mpe.client.*;
import processing.core.*;

public class ASyncClientExample extends PApplet {
//--------------------------------------

TCPClient client;

//--------------------------------------
static public void main(String args[]) {
PApplet.main(new String[] { "mpe.examples.ASyncClientExample" });
}

//--------------------------------------
public void setup() {
size(320,240);
// make a new Client using an INI file
client = new TCPClient("asynch.xml", this);

// IMPORTANT, YOU MUST START THE CLIENT!
client.start();
}

public void draw() {

}



//--------------------------------------
// Adds a Ball to the stage at the position of the mouse click.
public void mousePressed() {
// never include a ":" when broadcasting your message
int x = mouseX*2;
int y = mouseY*2;
client.broadcast(x + "," + y);
}
}
16 changes: 13 additions & 3 deletions Most-Pixels-Ever-Server/src/mpe/server/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,25 @@ void read(String msg) {

switch(startsWith){
// For Starting Up

case 'A':
if (parent.verbose) {
System.out.println("Raw receive: " + msg);
}
clientID = Integer.parseInt(tokens[1]);
// TODO: Track synch connections in list
System.out.println("Connecting asynch client " + clientID);

break;
case 'S':
if (parent.verbose) {
System.out.println("Raw message: " + msg);
System.out.println("Raw receive: " + msg);
}
clientID = Integer.parseInt(tokens[1]);

parent.addConnection(this);

System.out.println("Connecting Client " + clientID);
System.out.println("Connecting synch client " + clientID);
int total = parent.totalConnections();

// We should only wait the *first* time if we are told to wait for everyone
Expand Down Expand Up @@ -93,7 +103,7 @@ void read(String msg) {
break;
case 'T':
if (parent.verbose) {
System.out.println("Adding message to next frameEvent: " + msg);
System.out.println("Adding message to next frameEvent: " + clientID + "," + tokens[1]);
}
parent.newMessage = true;
parent.message += clientID + "," + tokens[1] + "|";
Expand Down

0 comments on commit e1e097b

Please sign in to comment.