-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,616 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.concurrentsortedset.client; | ||
|
||
public class ClientConfig { | ||
public static String SERVER_ADDRESS = "localhost"; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.concurrentsortedset.client; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.concurrentsortedset.client.command.AddCommand; | ||
import com.concurrentsortedset.client.command.ClientCommand; | ||
import com.concurrentsortedset.client.command.GetCommand; | ||
import com.concurrentsortedset.client.command.GetRangeCommand; | ||
import com.concurrentsortedset.client.command.RemCommand; | ||
import com.concurrentsortedset.client.command.SizeCommand; | ||
|
||
public class ClientSimulator { | ||
/** | ||
* <SIZE> <set1> [0] | ||
<ADD> <set1> <k1> <1> | ||
<GET> <set1> <k1> [1] | ||
<SIZE> <set1> [1] | ||
<ADD> <set1> <k1> <2> | ||
<GET> <set1> <k1> [2] | ||
<ADD> <set1> <k2> <3> | ||
<GETRANGE> <set1> <-1> <3> <3> [k2] [3] [-1] | ||
<ADD> <set2> <k3> <1> | ||
<GETRANGE> <set1> <set2> <-1> <0> <INT_MAX> [k3] [1] [k1] [2] [k2] [3] [-1] | ||
<REM> <set1> <k2> | ||
<GETRANGE> <set1> <set2> <-1> <0> <INT_MAX> [k3] [1] [k1] [2] [-1] | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
ClientCommand command; | ||
command = new SizeCommand(1); | ||
command.doWork(); | ||
System.out.println("EXPECTED: 0"); | ||
command = new AddCommand(1, 1, 1); | ||
command.doWork(); | ||
command = new GetCommand(1, 1); | ||
command.doWork(); | ||
System.out.println("EXPECTED: 1"); | ||
command = new SizeCommand(1); | ||
command.doWork(); | ||
System.out.println("EXPECTED: 1"); | ||
command = new AddCommand(1, 1, 2); | ||
command.doWork(); | ||
command = new GetCommand(1, 1); | ||
command.doWork(); | ||
System.out.println("EXPECTED: 2"); | ||
command = new AddCommand(1, 2, 3); | ||
command.doWork(); | ||
List<Integer> sets = new ArrayList<Integer>(); | ||
sets.add(1); | ||
command = new GetRangeCommand(sets, 3, 3); | ||
command.doWork(); | ||
command = new AddCommand(2, 3, 1); | ||
command.doWork(); | ||
sets.add(2); | ||
command = new GetRangeCommand(sets, 0, Integer.MAX_VALUE); | ||
command.doWork(); | ||
command = new RemCommand(1, 2); | ||
command.doWork(); | ||
command = new GetRangeCommand(sets, 0, Integer.MAX_VALUE); | ||
command.doWork(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.IOException; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public class AddCommand extends ClientCommand { | ||
|
||
int set, key, score; | ||
|
||
public AddCommand(int set, int key, int score) { | ||
this.set=set; | ||
this.key=key; | ||
this.score=score; | ||
} | ||
|
||
@Override | ||
public void fullCommand() throws IOException { | ||
dos.writeInt(set); | ||
dos.writeInt(key); | ||
dos.writeInt(score); | ||
} | ||
|
||
@Override | ||
public int commandCode() { | ||
return CommonParam.CODE_ADD; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
|
||
import com.concurrentsortedset.client.ClientConfig; | ||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public abstract class ClientCommand { | ||
|
||
DataInputStream dis; | ||
DataOutputStream dos; | ||
|
||
public abstract void fullCommand() throws IOException; | ||
|
||
public abstract int commandCode(); | ||
|
||
public void doWork() throws Exception | ||
{ | ||
Socket client = new Socket(); | ||
InetSocketAddress socket_address = new InetSocketAddress(ClientConfig.SERVER_ADDRESS, CommonParam.LISTEN_PORT); | ||
client.connect(socket_address, CommonParam.CONNECTION_TIMEOUT); | ||
dis = new DataInputStream(client.getInputStream()); | ||
dos = new DataOutputStream(client.getOutputStream()); | ||
dos.writeInt(commandCode()); | ||
fullCommand(); | ||
dos.flush(); | ||
client.close(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.IOException; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public class GetCommand extends ClientCommand { | ||
|
||
int set, key; | ||
|
||
public GetCommand(int set, int key) { | ||
this.set = set; | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
public void fullCommand() throws IOException { | ||
dos.writeInt(set); | ||
dos.writeInt(key); | ||
dos.flush(); | ||
int score_retrieved = dis.readInt(); | ||
System.out.println("The score value of set " + set + " and key " + key + " is: " + score_retrieved); | ||
} | ||
|
||
@Override | ||
public int commandCode() { | ||
return CommonParam.CODE_GET; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public class GetRangeCommand extends ClientCommand { | ||
|
||
List<Integer> sets; | ||
int lower, upper; | ||
|
||
public GetRangeCommand(List<Integer> sets, int lower, int upper) { | ||
this.sets=sets; | ||
this.lower=lower; | ||
this.upper=upper; | ||
} | ||
|
||
@Override | ||
public void fullCommand() throws IOException { | ||
for (Integer set : sets) | ||
{ | ||
dos.writeInt(set); | ||
} | ||
dos.writeInt(-1); | ||
dos.writeInt(lower); | ||
dos.writeInt(upper); | ||
dos.flush(); | ||
int key,score; | ||
System.out.println("In the range [" + lower + "-" + upper + "]there are the key->score"); | ||
while ((key=dis.readInt()) != -1) | ||
{ | ||
score=dis.readInt(); | ||
System.out.println(key + "->" + score); | ||
} | ||
} | ||
|
||
@Override | ||
public int commandCode() { | ||
return CommonParam.CODE_GETRANGE; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.IOException; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public class RemCommand extends ClientCommand { | ||
|
||
int set, key; | ||
|
||
public RemCommand(int set, int key) { | ||
this.set=set; | ||
this.key=key; | ||
} | ||
|
||
@Override | ||
public void fullCommand() throws IOException { | ||
dos.writeInt(set); | ||
dos.writeInt(key); | ||
} | ||
|
||
@Override | ||
public int commandCode() { | ||
return CommonParam.CODE_REM; | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.concurrentsortedset.client.command; | ||
|
||
import java.io.IOException; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
|
||
public class SizeCommand extends ClientCommand { | ||
|
||
int set; | ||
|
||
public SizeCommand(int set) { | ||
this.set=set; | ||
} | ||
|
||
@Override | ||
public void fullCommand() throws IOException { | ||
dos.writeInt(set); | ||
dos.flush(); | ||
int set_size = dis.readInt(); | ||
System.out.println("The size of set " + set + " is: " + set_size); | ||
} | ||
|
||
@Override | ||
public int commandCode() { | ||
return CommonParam.CODE_SIZE; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.concurrentsortedset.config; | ||
|
||
|
||
public class CommonParam { | ||
public static final int LISTEN_PORT = 7999; | ||
public static final int CONNECTION_TIMEOUT = 60000; | ||
|
||
public static final int CODE_ADD = 1; | ||
public static final int CODE_REM = 2; | ||
public static final int CODE_SIZE = 3; | ||
public static final int CODE_GET = 4; | ||
public static final int CODE_GETRANGE = 5; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.concurrentsortedset.init; | ||
|
||
import com.concurrentsortedset.networking.ConnectionListener; | ||
|
||
public class Launcher { | ||
public static void main(String[] args) throws InterruptedException { | ||
ConnectionListener listener = new ConnectionListener(); | ||
listener.start(); | ||
listener.join(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.concurrentsortedset.networking; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.net.Socket; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import com.concurrentsortedset.config.CommonParam; | ||
import com.concurrentsortedset.sortedset.SortedSetManager; | ||
import com.concurrentsortedset.sortedset.tree.Element; | ||
|
||
public class ConnectionHandler extends Thread { | ||
|
||
public static int threadIdGen = 0; | ||
|
||
public static int threadId; | ||
|
||
private Socket clientSocket; | ||
|
||
public boolean terminated; | ||
|
||
public ConnectionHandler(Socket clientSocket) | ||
{ | ||
this.clientSocket = clientSocket; | ||
terminated = false; | ||
threadId = threadIdGen++; | ||
this.setName("ConnectionHandler #" + threadId); | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
try | ||
{ | ||
DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); | ||
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); | ||
int command_code = dis.readInt(); | ||
int set, key, score; | ||
SortedSetManager sortedSetManager = new SortedSetManager(); | ||
switch (command_code) | ||
{ | ||
case CommonParam.CODE_ADD: | ||
set = dis.readInt(); | ||
key = dis.readInt(); | ||
score = dis.readInt(); | ||
System.out.println("[SERVER] ADD " + set+","+key+","+score); | ||
sortedSetManager.add(set, key, score); | ||
break; | ||
case CommonParam.CODE_REM: | ||
set = dis.readInt(); | ||
key = dis.readInt(); | ||
System.out.println("[SERVER] REM " + set+","+key); | ||
sortedSetManager.remove(set, key); | ||
break; | ||
case CommonParam.CODE_SIZE: | ||
set = dis.readInt(); | ||
System.out.println("[SERVER] SIZE " + set); | ||
int set_size = sortedSetManager.size(set); | ||
dos.writeInt(set_size); | ||
dos.flush(); | ||
System.out.println("[SERVER] SIZE=" + set_size); | ||
break; | ||
case CommonParam.CODE_GET: | ||
set = dis.readInt(); | ||
key = dis.readInt(); | ||
System.out.println("[SERVER] GET " + set+","+key); | ||
int score_retrieved = sortedSetManager.get(set, key); | ||
dos.writeInt(score_retrieved); | ||
dos.flush(); | ||
System.out.println("[SERVER] GET=" + score_retrieved); | ||
break; | ||
case CommonParam.CODE_GETRANGE: | ||
List<Integer> sets = new LinkedList<Integer>(); | ||
while ((set=dis.readInt()) != -1) | ||
{ | ||
sets.add(set); | ||
} | ||
int lower = dis.readInt(); | ||
int upper = dis.readInt(); | ||
List<Element> in_range = sortedSetManager.getRange(sets, lower, upper); | ||
for (Element element : in_range) | ||
{ | ||
dos.writeInt(element.key); | ||
dos.writeInt(element.score); | ||
} | ||
dos.writeInt(-1); | ||
dos.flush(); | ||
break; | ||
} | ||
} | ||
catch (Exception error) | ||
{ | ||
error.printStackTrace(); | ||
} | ||
finally | ||
{ | ||
terminated = true; | ||
try {clientSocket.close();} catch (Exception e) {} | ||
} | ||
} | ||
} |
Oops, something went wrong.