Monday, January 14, 2013

Java Distributed computing


package dcj.examples;

import java.lang.*;

abstract class SimpleCmd
{
protected String arg;

public SimpleCmd(String inArg) {
arg = inArg;
}

public abstract String Do();
}

class GetCmd extends SimpleCmd
{
public GetCmd(String s) { super(s); }

public String Do() {
String result = arg + " Gotten\n";
return result;
}
}

public class HeadCmd extends SimpleCmd
{
public HeadCmd(String s) { super(s); }
public String Do() {
String result = "Head \"" + arg + "\" processed.\n";
return result;
}
}

class PostCmd extends SimpleCmd
{
public PostCmd(String s) { super(s); }

public String Do() {
String result = arg + " Posted\n";
return result;
}
}

class DoneCmd extends SimpleCmd
{
public DoneCmd() { super(""); };
public String Do() {
String result = "All done.\n";
return result;
}
}




package dcj.examples;

import java.lang.*;
import java.io.*;
import java.net.*;

public class SimpleCmdInputStream extends DataInputStream
{
public SimpleCmdInputStream(InputStream in) {
super(in);
}

public String readString() throws IOException {
StringBuffer strBuf = new StringBuffer();
boolean hitSpace = false;
while (!hitSpace) {
char c = readChar();
hitSpace = Character.isSpace(c);
if (!hitSpace)
strBuf.append(c);
}

String str = new String(strBuf);
return str;
}

public SimpleCmd readCommand() throws IOException {
SimpleCmd cmd;
String commStr = readString();
if (commStr.compareTo("HEAD") == 0)
cmd = new HeadCmd(readString());
else if (commStr.compareTo("GET") == 0)
cmd = new GetCmd(readString());
else if (commStr.compareTo("POST") == 0)
cmd = new PostCmd(readString());
else if (commStr.compareTo("DONE") == 0)
cmd = new DoneCmd();
else
throw new IOException("Unknown command.");

return cmd;
}
}



package dcj.examples;

import java.lang.*;
import java.net.*;
import java.io.*;

public class SimpleClient
{
// Our socket connection to the server
protected Socket serverConn;

// The input command stream from the server
protected SimpleCmdInputStream inStream;

public SimpleClient(String host, int port)
throws IllegalArgumentException {
try {
System.out.println("Trying to connect to " + host + " " + port);
serverConn = new Socket(host, port);
}
catch (UnknownHostException e) {
throw new IllegalArgumentException("Bad host name given.");
}
catch (IOException e) {
System.out.println("SimpleClient: " + e);
System.exit(1);
}

System.out.println("Made server connection.");
}

public static void main(String argv[]) {
if (argv.length < 2) {
System.out.println("Usage: java SimpleClient [host] [port]");
System.exit(1);
}

String host = argv[0];
int port = 3000;
try {
port = Integer.parseInt(argv[1]);
}
catch (NumberFormatException e) {}

SimpleClient client = new SimpleClient(host, port);
client.sendCommands();
}

public void sendCommands() {
try {
OutputStreamWriter wout =
new OutputStreamWriter(serverConn.getOutputStream());
BufferedReader rin = new BufferedReader(
new InputStreamReader(serverConn.getInputStream()));

// Send a GET command...
wout.write("GET goodies ");
// ...and receive the results
String result = rin.readLine();
System.out.println("Server says: \"" + result + "\"");

// Now try a POST command
wout.write("POST goodies ");
// ...and receive the results
result = rin.readLine();
System.out.println("Server says: \"" + result + "\"");

// All done, tell the server so
wout.writeChars("DONE ");
result = rin.readLine();
System.out.println("Server says: \"" + result + "\"");
}
catch (IOException e) {
System.out.println("SimpleClient: " + e);
System.exit(1);
}
}

public synchronized void finalize() {
System.out.println("Closing down SimpleClient...");
try { serverConn.close(); }
catch (IOException e) {
System.out.println("SimpleClient: " + e);
System.exit(1);
}
}
}











package dcj.examples;

import java.net.*;
import java.io.*;
import java.lang.*;

// A generic server that listens on a port and connects to any clients it
// finds. Made to extend Thread, so that an application can have multiple
// server threads servicing several ports, if necessary.

public class SimpleServer
{
protected int portNo = 3000; // Port to listen to for clients
protected ServerSocket clientConnect;

public SimpleServer(int port) throws IllegalArgumentException {
if (port <= 0)
throw new IllegalArgumentException(
"Bad port number given to SimpleServer constructor.");

// Try making a ServerSocket to the given port
System.out.println("Connecting server socket to port...");
try { clientConnect = new ServerSocket(port); }
catch (IOException e) {
System.out.println("Failed to connect to port " + port);
System.exit(1);
}

// Made the connection, so set the local port number
this.portNo = port;
}

public static void main(String argv[]) {
int port = 3000;
if (argv.length > 0) {
int tmp = port;
try {
tmp = Integer.parseInt(argv[0]);
}
catch (NumberFormatException e) {}

port = tmp;
}

SimpleServer server = new SimpleServer(port);
System.out.println("SimpleServer running on port " + port + "...");
server.listen();
}

public void listen() {
// Listen to port for client connection requests.
try {
System.out.println("Waiting for clients...");
while (true) {
Socket clientReq = clientConnect.accept();
System.out.println("Got a client...");
serviceClient(clientReq);
}
}
catch (IOException e) {
System.out.println("IO exception while listening for clients.");
System.exit(1);
}
}

public void serviceClient(Socket clientConn) {
SimpleCmdInputStream inStream = null;
DataOutputStream outStream = null;
try {
inStream = new SimpleCmdInputStream(clientConn.getInputStream());
outStream = new DataOutputStream(clientConn.getOutputStream());
}
catch (IOException e) {
System.out.println("SimpleServer: Error getting I/O streams.");
}

SimpleCmd cmd = null;
System.out.println("Attempting to read commands...");
while (cmd == null ||
!(cmd instanceOf DomeCmd)) {
try { cmd = inStream.readCommand(); }
catch (IOException e) {
System.out.println("SimpleServer: " + e);
System.exit(1);
}

if (cmd != null) {
String result = cmd.Do();
try { outStream.writeBytes(result); }
catch (IOException e) {
System.out.println("SimpleServer: " + e);
System.exit(1);
}
}
}
}

public synchronized void finalize() {
System.out.println("Shutting down SimpleServer running on port "
+ portNo);
}
}

No comments:

Post a Comment