Skip to content
Browse files

End of episode 1

  • Loading branch information...
0 parents commit 3d7a2c0543cb6d848a006c0de951d5c60ea3177f @KentBeck committed Feb 23, 2010
7 .classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>MyTyrant</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
12 .settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Thu Feb 18 14:15:55 PST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
8 To Do
@@ -0,0 +1,8 @@
+* automatically start/stop server
+
+* put
+* get
+* remove
+* vanish
+* iterator
+* size
BIN bin/TyrantMap.class
Binary file not shown.
BIN bin/TyrantTest.class
Binary file not shown.
52 src/TyrantMap.java
@@ -0,0 +1,52 @@
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+class TyrantMap {
+ private static final int GET_OPERATION = 0x30;
+ private static final int PUT_OPERATION = 0x10;
+ private Socket socket;
+ private DataOutputStream writer;
+ private DataInputStream reader;
+
+ void put(byte[] key, byte[] value) throws UnknownHostException, IOException {
+ writeOperation(PUT_OPERATION);
+ writer.writeInt(key.length);
+ writer.writeInt(value.length);
+ writer.write(key);
+ writer.write(value);
+ int status = reader.read();
+ if (status != 0)
+ throw new RuntimeException();
+ }
+
+ private void writeOperation(int operation) throws IOException {
+ writer.write(0xC8);
+ writer.write(operation);
+ }
+
+ public byte[] get(byte[] key) throws IOException {
+ writeOperation(GET_OPERATION);
+ writer.writeInt(key.length);
+ writer.write(key);
+ int status = reader.read();
+ if (status != 0)
+ throw new RuntimeException();
+ int length = reader.readInt();
+ byte[] results = new byte[length];
+ reader.read(results); // TODO check return value
+ return results;
+ }
+
+ void open() throws UnknownHostException, IOException {
+ socket = new Socket("localhost", 1978);
+ writer = new DataOutputStream(socket.getOutputStream());
+ reader = new DataInputStream(socket.getInputStream());
+ }
+
+ public void close() throws IOException {
+ socket.close();
+ }
+}
29 src/TyrantTest.java
@@ -0,0 +1,29 @@
+import static org.junit.Assert.assertArrayEquals;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TyrantTest {
+ private TyrantMap tyrant;
+
+ @Test public void putThenGet() throws UnknownHostException, IOException {
+ byte[] key = new byte[] {'k', 'e', 'y'};
+ byte[] value = new byte[] {'v', 'a', 'l', 'u', 'e'};
+ tyrant.put(key, value);
+ byte[] actual = tyrant.get(key);
+ assertArrayEquals(value, actual);
+ }
+
+ @Before public void openTyrant() throws UnknownHostException, IOException {
+ tyrant = new TyrantMap();
+ tyrant.open();
+ }
+
+ @After public void closeTyrant() throws IOException {
+ tyrant.close();
+ }
+}

0 comments on commit 3d7a2c0

Please sign in to comment.
Something went wrong with that request. Please try again.