Nzen avatar Nzen committed ab0ea57

Code adapted, untested

Untested because I've nothing is implemented. I guess I'll test the
template by itself against the client and have it echo later. Otherwise
this is ready for hacking. However, one coordination remains: the data
structure's location.

Will the threads initialize with a structure housed in the server or use a
static data structure? I don't recall the consequences of each at the
moment.

Comments (0)

Files changed (3)

 import java.net.*;
 import java.io.*;
-import java.util.Scanner;
-	/*	PhysicalLayer API
-	
-	String specialOut( Comm exe )
-	String encode( String message )
-	Comm whichSpecialInput( String bitString )
-	String link.decode(Bits);
-	*/
 
-// command line > java Client 127.0.0.1 (port#, but unnecessary)
+// command line > java Client 127.0.0.1 (port#, but unnecessary) ; consider letting client supply an address?
 
 public class Client
 {
-    private static boolean connected;
-	private static final int BUFSIZE = 32;	//Size of the ACK buffer
-	//private static PhysicalLayer ethernet;
 	private static DataInputStream in;
 	private static DataOutputStream out;
 	
 	public static void main( String[] args ) throws IOException
 	{
-		ethernet = new PhysicalLayer( );
 		testArgs( args.length );
 		String server = args[ 0 ];       // Server name/IP	
 		int servPort = ( args.length == 2 ) ? Integer.parseInt(args[1]) : 7;
 	
 	static void handleSession( Socket serverConn ) throws IOException
 	{
-		boolean finished = false;
-		connected = false;	// return satisfied?
-		int count = 0;
-		while(count < 10 && !finished ){
-			try {
-				Thread.sleep(1000);
-			} catch (Exception ex) {}
-			if( connected ){
-				System.out.println("Connected to server");
-				finished = communicate( serverConn );
-			}
-			else
-				handShake ( serverConn );
-			count++;
-		}
-		System.out.println("\nConnection closed, either you finished or time out.");
+		// send a data link address, generated or via stdin
+		// perhaps offer chance to release IP early
+		;
+	}
+	
+	/*
+	static void handleSession( Socket serverConn, String macAddr ) throws IOException
+	{
+		// send an address provided in arguments?
 	}
+	*/
 }
 import java.net.*;
 import java.security.*;
 
+	// -- confer with server, will you use a static table or use server's instance?
+
 public class Dhcp implements Runnable
 {
+	private Socket connection;
+	private DataInputStream in;
+	private DataOutputStream out;
+	// private appropriateConcurrentDataStrucure table; // am I static or server instance?
+	//private static int lowest; // or your own implementation for keeping track of the lowest across threads
+	//private int myEntry; // or some other way to know which timer to refer to
+
+	public Dhcp( Socket client, InputStream inPath, OutputStream outPath )
+	{
+		connection = client;
+		in = new DataInputStream( inPath );
+		out = new DataOutputStream( outPath );
+	}
 
-	/*
-	     private Socket server;
-    private String line,input;
-
-    doComms(Socket server) {
-      this.server=server;
-    }
-
-    public void run () {
-
-      input="";
-
-      try {
-        // Get input from the client
-        DataInputStream in = new DataInputStream (server.getInputStream());
-        PrintStream out = new PrintStream(server.getOutputStream());
-
-        while((line = in.readLine()) != null && !line.equals(".")) {
-          input=input + line;
-          out.println("I got:" + line);
-        }
-
-        // Now write to the client
-
-        System.out.println("Overall message is:" + input);
-        out.println("Overall message is:" + input);
-
-        server.close();
-      } catch (IOException ioe) {
-        System.out.println("IOException on socket listen: " + ioe);
-        ioe.printStackTrace();
-      }
-    } 
-	 */
 	@Override
 	public void run()
 	{
-		// TODO Auto-generated method stub
-
+		// handle session
+		try
+		{
+			// handleSession();
+			
+			// receive mac addr (check for collisions?)
+			// choose lowest, fill table ?? syncronize ??
+			// inform client of IP to use
+			// set timer
+			
+				// sleep 1 second intervals
+				// check if client released addr? release it myself "at random to simulate client request" (paraphrased)?
+			// inform client
+			// ! syncronize ! find lowest available, update lowest (another option lets thread search DS for lowest with timer 0)
+			
+			connection.close();
+		}
+		catch (IOException ioe)
+		{
+			System.out.println("IOException on socket listen: " + ioe);
+			ioe.printStackTrace();
+		}
 	}
 
 }
+
+class tableEntry
+{
+	// dumb struct for addresses & start times
+	public tableEntry( )
+	{
+		;
+	}
+}
 import java.net.*;
 import java.io.*;
-import java.util.Random;
-	/*	PhysicalLayer API
-	
-	String specialOut( Comm exe )
-	String encode( String message )
-	Comm whichSpecialInput( String bitString )
-	String link.decode(Bits);
-	*/
 
-// command line > java Server 50000 (or OS appropriate ephemeral port)
+// adapted from http://www.kieser.net/linux/java_server.html
 
+// command line > java Server 50000 (or OS appropriate ephemeral port)
 public class Server
 {
-	/*
-	 *   private static int port=4444, maxConnections=0;
-  // Listen for incoming connections and handle them
-  public static void main(String[] args) {
-    int i=0;
-
-    try{
-      ServerSocket listener = new ServerSocket(port);
-      Socket server;
-
-      while((i++ < maxConnections) || (maxConnections == 0)){
-        doComms connection;
-
-        server = listener.accept();
-        doComms conn_c= new doComms(server);
-        Thread t = new Thread(conn_c);
-        t.start();
-      }
-    } catch (IOException ioe) {
-      System.out.println("IOException on socket listen: " + ioe);
-      ioe.printStackTrace();
-    }
-  }
-	 */
-	
-	private static boolean connected;
-	//private static PhysicalLayer ethernet;
-	private static DataInputStream in;
-	private static DataOutputStream out;
+	private static final int maxClients = 6; // template suggested. do something else? unlimited?
 	
 	public static void main( String[] args ) throws IOException
 	{
-		testArgs( args.length );	
-		int servPort = Integer.parseInt(args[0]);
-		ServerSocket servSock = new ServerSocket(servPort);
-		ethernet = new PhysicalLayer( );
-		System.out.println("Server is now ready to receive clients at port " + servPort);
+		testArgs( args.length );
+		int clients = 0;
+		InputStream inPath;
+		OutputStream outPath;
 		
-		Socket clntSock = servSock.accept();
-		SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
-		System.out.println("Sensed client at " + clientAddress);
-
-		InputStream inPath = clntSock.getInputStream();
-		in = new DataInputStream( inPath );
-		OutputStream outPath = clntSock.getOutputStream();
-		out = new DataOutputStream( outPath );
-		handleSession( clntSock );
-		servSock.close();
+		int servPort = Integer.parseInt(args[0]);
+		try
+		{
+			ServerSocket servSock = new ServerSocket(servPort);
+			Socket clntSock;
+			while ( clients <= maxClients )
+			{
+				Dhcp orgin; // not sure what this is for, was in the template
+				clntSock = servSock.accept();
+				inPath = clntSock.getInputStream();
+				outPath = clntSock.getOutputStream();
+				clients++;
+				
+				Dhcp newSession = new Dhcp( clntSock, inPath, outPath );
+				Thread core = new Thread( newSession );
+				core.start();
+				
+				if ( core.getState() == Thread.State.TERMINATED ) // session over
+					clients--;
+			}
+			servSock.close();
+		}
+		catch (IOException ioe)
+		{
+			System.out.println("IOException on socket listen: " + ioe);
+			ioe.printStackTrace();
+		}
      }
 	
 	static void testArgs( int numArgs ) throws IOException
 		if ( numArgs != 1 )
 			throw new IllegalArgumentException( "Only parameter: Port#" );
 	}
-	
-	static void handleSession( Socket clntSock) throws IOException
-	{
-		int tries = 20;
-		connected = false;
-		boolean success = false;
-		while( tries >= 0 ){
-			if( connected )
-			{
-				communicate( clntSock );
-				return;
-			}
-			else
-			{
-				success = handShake( clntSock );
-				if ( !success )
-				{
-					try
-					{	Thread.sleep(1000); }
-					catch (Exception ex) {}
-				}
-			}
-			tries--;
-        }
-	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.