Skip to content

Commit

Permalink
feat(chunk-server): make chunk-server port configurable (closes #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
menuka94 committed Sep 29, 2021
1 parent 18f92ae commit f3fe963
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/main/java/cs555/hw1/node/chunkServer/ChunkServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class ChunkServer implements Node {

private String hostName;

public ChunkServer(Socket controllerSocket) throws IOException {
public ChunkServer(Socket controllerSocket, int port) throws IOException {
log.info("Initializing ChunkServer on {}", System.getenv("HOSTNAME"));
controllerConnection = new TCPConnection(controllerSocket, this);
filesMap = new HashMap<>();
Expand All @@ -75,7 +75,7 @@ public ChunkServer(Socket controllerSocket) throws IOException {
hostName = controllerSocket.getLocalAddress().getHostName();

tcpConnectionsCache = new TCPConnectionsCache();
tcpServerThread = new TCPServerThread(0, this, tcpConnectionsCache);
tcpServerThread = new TCPServerThread(port, this, tcpConnectionsCache);
commandParser = new InteractiveCommandParser(this);
sendRegistrationRequestToController();
}
Expand Down Expand Up @@ -154,18 +154,19 @@ private void ReadFilesFromDisk() {
}

public static void main(String[] args) throws IOException {
if (args.length < 2) {
if (args.length < 3) {
System.out.println("Not enough arguments to start ChunkServer. " +
"Enter <controller-host> and <controller-port>");
"Enter <controller-host> and <controller-port> <chunk-server-port>");
System.exit(1);
} else if (args.length > 2) {
System.out.println("Invalid number of arguments. Provide <controller-host> and <controller-port>");
} else if (args.length > 3) {
System.out.println("Invalid number of arguments. Provide <controller-host> <controller-port> <chunk-server-port>");
}

String controllerHost = args[0];
int controllerPort = Integer.parseInt(args[1]);
int chunkServerPort = Integer.parseInt(args[2]);
Socket controllerSocket = new Socket(controllerHost, controllerPort);
ChunkServer chunkServer = new ChunkServer(controllerSocket);
ChunkServer chunkServer = new ChunkServer(controllerSocket, chunkServerPort);
chunkServer.initialize();
TCPConnection tcpConnection;
if (chunkServer.tcpConnectionsCache.containsConnection(controllerSocket)) {
Expand Down Expand Up @@ -305,11 +306,11 @@ private synchronized void handleRetrieveChunkRequest(Event event) {
//if(request.getSocket().getInetAddress().getHostName().contains("pollock")){
if (corrupted | corruptedChunk) {
//notify Controller
ReportChunkCorruption reportCorChunk = new ReportChunkCorruption();
reportCorChunk.setChunkName(chunkName);
ReportChunkCorruption reportChunkCorruption = new ReportChunkCorruption();
reportChunkCorruption.setChunkName(chunkName);
try {
log.info("ChunkServer {} is notifying controller about chunk corruption", hostName);
controllerConnection.sendData(reportCorChunk.getBytes());
controllerConnection.sendData(reportChunkCorruption.getBytes());
} catch (IOException e) {
log.error(e.getLocalizedMessage());
e.printStackTrace();
Expand All @@ -331,6 +332,7 @@ private synchronized void handleRetrieveChunkRequest(Event event) {
if (tcpConnectionsCache.containsConnection(clientSocket)) {
clientConnection = tcpConnectionsCache.getConnection(clientSocket);
} else {
log.warn("Connection does not exist in cache");
clientConnection = new TCPConnection(clientSocket, this);
}

Expand Down
2 changes: 1 addition & 1 deletion start-chunk-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ JAR_FILE=../../../libs/cs555-hw1-1.0.jar
CLASSES_DIR=build/classes/java/main

# Start Client on boston
cd ${CLASSES_DIR} && java -cp ${JAR_FILE} cs555.hw1.node.chunkServer.ChunkServer arkansas 9000
cd ${CLASSES_DIR} && java -cp ${JAR_FILE} cs555.hw1.node.chunkServer.ChunkServer arkansas 9000 9001
3 changes: 1 addition & 2 deletions start-chunk-servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ declare -a CHUNK_SERVERS=(
"berlin"
"cairo"
"lima"
"london"
)

for CHUNK_SERVER in "${CHUNK_SERVERS[@]}"
do
xterm -hold -title "ChunkServer: ${CHUNK_SERVER}" -e "ssh -t ${CHUNK_SERVER} \"cd ${CLASSES_DIR} && java -cp ${JAR_FILE} cs555.hw1.node.chunkServer.ChunkServer arkansas 9000\"" &
xterm -hold -title "ChunkServer: ${CHUNK_SERVER}" -e "ssh -t ${CHUNK_SERVER} \"cd ${CLASSES_DIR} && java -cp ${JAR_FILE} cs555.hw1.node.chunkServer.ChunkServer arkansas 9000 9001\"" &
done

0 comments on commit f3fe963

Please sign in to comment.