Created
December 7, 2014 18:48
-
-
Save danielvaughan/466cb4222e6a50581d6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package camdug; | |
import com.google.gson.Gson; | |
import org.zeromq.ZMQ; | |
public class MQExtensions { | |
public static void sendT(ZMQ.Socket socket, Object src) { | |
Gson gson = new Gson(); | |
String json = gson.toJson(src); | |
socket.send(json.getBytes()); | |
} | |
public static Object receiveT(ZMQ.Socket socket, Class clazz) { | |
Gson gson = new Gson(); | |
String json = socket.recvStr(); | |
Object obj = gson.fromJson(json, clazz); | |
return obj; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package camdug; | |
public class TestScore { | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
TestScore testScore = (TestScore) o; | |
if (name != null ? !name.equals(testScore.name) : testScore.name != null) return false; | |
if (score != null ? !score.equals(testScore.score) : testScore.score != null) return false; | |
return true; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getScore() { | |
return score; | |
} | |
@Override | |
public String toString() { | |
return "TestScore{" + | |
"name='" + name + '\'' + | |
", score='" + score + '\'' + | |
'}'; | |
} | |
@Override | |
public int hashCode() { | |
int result = name != null ? name.hashCode() : 0; | |
result = 31 * result + (score != null ? score.hashCode() : 0); | |
return result; | |
} | |
public TestScore(String name, String score) { | |
this.name = name; | |
this.score = score; | |
} | |
private final String name; | |
private final String score; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package camdug; | |
import java.util.Arrays; | |
public class TestSubmission { | |
private final String name; | |
@Override | |
public String toString() { | |
return "TestSubmission{" + | |
"name='" + name + '\'' + | |
", age=" + age + | |
", answers=" + Arrays.toString(answers) + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
TestSubmission that = (TestSubmission) o; | |
if (age != that.age) return false; | |
if (!Arrays.equals(answers, that.answers)) return false; | |
if (!name.equals(that.name)) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = name.hashCode(); | |
result = 31 * result + age; | |
result = 31 * result + Arrays.hashCode(answers); | |
return result; | |
} | |
private final int age; | |
private final String[] answers; | |
public TestSubmission(String name, int age, String[] answers) { | |
this.name = name; | |
this.age = age; | |
this.answers = answers; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public String[] getAnswers() { | |
return answers; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package camdug; | |
import org.junit.Test; | |
import org.zeromq.ZMQ; | |
import org.zeromq.ZMQ.Context; | |
import org.zeromq.ZMQ.Socket; | |
import static org.junit.Assert.assertEquals; | |
public class MQExtensionTests { | |
@Test | |
public void ShouldBeAbleToSendAndRecieveGenericMessagesUsingJsonStrings() { | |
Context context = ZMQ.context(1); | |
Socket server = context.socket(ZMQ.REP); | |
server.bind("tcp://127.0.0.1:5556"); | |
Socket client = context.socket(ZMQ.REQ); | |
TestSubmission testSubmission = new TestSubmission("Fred", 5, new String[]{"Washington", | |
"Snowcode is the world's highest free developer unconference", | |
"2010 What year did snowcode start?"}); | |
client.connect("tcp://127.0.0.1:5556"); | |
System.out.println(String.format("submitting test for student:%s", testSubmission.getName())); | |
MQExtensions.sendT(client, testSubmission); | |
TestSubmission test = (TestSubmission) MQExtensions.receiveT(server, TestSubmission.class); | |
assertEquals(testSubmission, test); | |
System.out.println(String.format("received test for student:%s", test.getName())); | |
TestScore testScore = new TestScore(test.getName(), "A+"); | |
System.out.println("sending test results back"); | |
MQExtensions.sendT(server, testScore); | |
TestScore mytestScore = (TestScore) MQExtensions.receiveT(client, TestScore.class); | |
assertEquals(testScore, testScore); | |
System.out.println(String.format("recieved test score, I got an '%s'!", mytestScore.getScore())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment