Skip to content

Commit

Permalink
Added pseudo code for future methods. Added Node and NodeServer docum…
Browse files Browse the repository at this point in the history
…entation.
  • Loading branch information
joemirizio committed May 6, 2012
1 parent 36ed325 commit 2bbff9d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 42 deletions.
108 changes: 66 additions & 42 deletions src/Node.java
@@ -1,5 +1,7 @@
// MusicObject class
// 20120418
/**
* CSC 445
* Project 3
*/

import java.io.ObjectInputStream;
import java.io.FileInputStream;
Expand All @@ -10,45 +12,67 @@
import java.util.HashMap;
import java.util.concurrent.*;

/**
* A representation of a persistent data node.
*/
public class Node implements Serializable {

protected static final String FILENAME = "data.bin";

protected ConcurrentHashMap<String, MusicObject> data;


public Node() {
this.data = new ConcurrentHashMap<String, MusicObject>();
}

public void addMusicObject(MusicObject song) {
this.data.put(song.getId(), song);
}

protected void writeToFile() {
try {
FileOutputStream fos = new FileOutputStream(FILENAME);
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(this.data);

oos.close();
} catch (IOException e) { e.printStackTrace(); }
}

@SuppressWarnings("unchecked")
protected void readFromFile() {
try {
FileInputStream fis = new FileInputStream(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);

this.data = (ConcurrentHashMap<String, MusicObject>)ois.readObject();

ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* The filename for the data persistence.
*/
protected static final String FILENAME = "data.bin";

/**
* The data stored in the node.
*/
protected ConcurrentHashMap<String, MusicObject> data;


/**
* Constructor.
*/
public Node() {
this.data = new ConcurrentHashMap<String, MusicObject>();
}

/**
* Add a MusicObject to the node.
* @param song The song to be added.
*/
public void addMusicObject(MusicObject song) {
this.data.put(song.getId(), song);
}

/**
* Persists the data to the file.
*/
protected void writeToFile() {
try {
FileOutputStream fos = new FileOutputStream(FILENAME);
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(this.data);

oos.close();
} catch (IOException e) { e.printStackTrace(); }
}

/**
* Reinitializes the node from the file.
*/
@SuppressWarnings("unchecked")
protected void readFromFile() {
try {
FileInputStream fis = new FileInputStream(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);

this.data = (ConcurrentHashMap<String, MusicObject>)ois.readObject();

ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
60 changes: 60 additions & 0 deletions src/NodeServer.java
@@ -1,3 +1,8 @@
/**
* CSC 445
* Project 3
*/

import java.io.*;
import java.net.*;
import java.util.Scanner;
Expand All @@ -10,10 +15,25 @@ public class NodeServer extends Thread {
public static final String DEFAULT_SERVER_LIST = "servers.txt";
public static final int SERVER_CONNECT_TIMEOUT = 1000;

/**
* The socket connection for listening.
*/
private ServerSocket socket;
/**
* A list of all connected nodes.
*/
private List<Socket> servers;
/**
* The persistent data node.
*/
private Node node;


/**
* Constructor specifying the port and the initial server list.
* @param port The port.
* @param server_list_file The initial list of all known nodes.
*/
public NodeServer(int port, String server_list_file) {
/* Create TCP socket */
try {
Expand All @@ -39,13 +59,53 @@ public NodeServer(int port, String server_list_file) {
} catch (IOException e) { System.out.format("[!] %s: %s:%d\n", e.getMessage(), svr_host, svr_port); }
}

/* Initialize node */
this.node = new Node();
this.node.readFromFile();

this.start();
}

/**
* Processes the requests.
*/
public void run() {
while (true) {
// @TODO Receive requests and process actions
// @TODO Check status of all other nodes
}
}

/**
* Adds a MusicObject to the node and updates all connected nodes.
* @param song The song to be added.
*/
private void addMusicObject(MusicObject song) {
this.node.addMusicObject(song);
// @TODO Send updates to all nodes (possibly do a consensus check)
}

/**
* Queries all nodes and returns the appropriate dataset.
* @param query The query.
* @return The list of all appropriate MusicOjects.
*/
private List<MusicObject> queryAll(String query) {
List<MusicObject> results = new ArrayList<MusicObject>();//this.node.query(query);
for (Socket sock : this.servers) {
// @TODO Send GET to all other nodes, receive List<MusicObject> through OOS
}
// @TODO Perform consensus

// @TODO Limit and return results
return results;
}

/**
* Starts the server.
* @param args[0] The port to listen on.
* @param args[1] The initial list of all other known nodes.
*/
public static void main(String[] args) {
int port = (args.length > 0) ? Integer.parseInt(args[0]) : DEFAULT_PORT;
String server_list_file = (args.length > 1) ? args[1] : DEFAULT_SERVER_LIST;
Expand Down

0 comments on commit 2bbff9d

Please sign in to comment.