From b4dbd2cfa724476162fa6d35941a5d7cdc9c9524 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 18 Feb 2019 08:10:10 -0500 Subject: Adds all assignments not previously in a git repo --- CS2452/assign5/src/Client.java | 32 ++++++++++++ CS2452/assign5/src/ResourceReader.java | 89 ++++++++++++++++++++++++++++++++++ CS2452/assign5/src/ResourceWriter.java | 56 +++++++++++++++++++++ CS2452/assign5/src/Test.java | 23 +++++++++ CS2452/assign5/src/WRServer.java | 59 ++++++++++++++++++++++ 5 files changed, 259 insertions(+) create mode 100755 CS2452/assign5/src/Client.java create mode 100755 CS2452/assign5/src/ResourceReader.java create mode 100755 CS2452/assign5/src/ResourceWriter.java create mode 100755 CS2452/assign5/src/Test.java create mode 100755 CS2452/assign5/src/WRServer.java (limited to 'CS2452/assign5/src') diff --git a/CS2452/assign5/src/Client.java b/CS2452/assign5/src/Client.java new file mode 100755 index 0000000..9d7cb19 --- /dev/null +++ b/CS2452/assign5/src/Client.java @@ -0,0 +1,32 @@ +import java.net.*; +import java.io.*; + +class Client +{ + public static void main(String[] args) + { + try + { + byte b = 0; + int cnt = 0; + Socket s = new Socket(args[0], Integer.parseInt(args[1])); + DataInputStream di = new DataInputStream(s.getInputStream()); + while (true) + { + b = di.readByte(); + System.out.printf("%d - %d\t%x\t%c\n", ++cnt, b, b, b); + if ((cnt % (1024 * 1024)) == 0) + { + System.out.println("------MILLION------\n\n"); + cnt = 0; + Thread.sleep(1000); + di.close(); + return; + } + } + } catch (Exception e) + { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/CS2452/assign5/src/ResourceReader.java b/CS2452/assign5/src/ResourceReader.java new file mode 100755 index 0000000..f9f0f71 --- /dev/null +++ b/CS2452/assign5/src/ResourceReader.java @@ -0,0 +1,89 @@ +import java.io.*; +import java.util.concurrent.Semaphore; + +public class ResourceReader implements Runnable +{ + public static double cor = 0; + public static Semaphore flg1 = new Semaphore(1); + + Thread t; + byte[] res = null, sentinel = null; + DataOutputStream dout = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceReader(DataOutputStream dot, byte[] r, Semaphore s, Semaphore s2) + { + res = r; + dout = dot; + flg = s; + flg2 = s2; + sentinel = new byte[32768]; + for (int i = 0; i < sentinel.length; i++ ) + sentinel[i] = 65; + t = new Thread(this, "ResourceReader"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceReader: starts"); + while (running) + { + try + { + + flg2.acquire(); + flg2.release(); + flg1.acquire(); + if (cor == 0) + { + flg.acquire(); + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + flg.release(); + } + else + { + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + } + flg1.acquire(); + cor-- ; + flg1.release(); + + System.out.println("ResourceReader: sent a bunch"); + Thread.sleep(100); + running = false; + } catch (Exception e) + { + e.printStackTrace(); + try + { + dout.close(); + } catch (Exception e2) + { + } + return; + } + } + System.out.println("ResourceReader: ends"); + try + { + dout.close(); + } catch (Exception e2) + { + } + + } +} diff --git a/CS2452/assign5/src/ResourceWriter.java b/CS2452/assign5/src/ResourceWriter.java new file mode 100755 index 0000000..2fb0967 --- /dev/null +++ b/CS2452/assign5/src/ResourceWriter.java @@ -0,0 +1,56 @@ +import java.util.concurrent.Semaphore; + +public class ResourceWriter implements Runnable +{ + Thread t; + byte[] res = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceWriter(byte[] r, Semaphore s, Semaphore s2) + { + res = r; + flg = s; + flg2 = s2; + t = new Thread(this, "ResourceWriter"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceWriter: starts"); + while (running) + { + System.out.println("RW in while"); + try + { + flg2.acquire(); + flg.acquire(); + + for (i = 0; i < res.length; i++ ) + res[i] = value; + flg.release(); + flg2.release(); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + value++ ; + if (value == 100) value = 33; + System.out.println("ResourceWriter: updated, waiting 15, 14, ..."); + try + { + Thread.sleep(15000); + } catch (Exception e) + { + } + } + System.out.println("ResourceWriter: ends"); + + } +} diff --git a/CS2452/assign5/src/Test.java b/CS2452/assign5/src/Test.java new file mode 100755 index 0000000..ed6234c --- /dev/null +++ b/CS2452/assign5/src/Test.java @@ -0,0 +1,23 @@ +class Test +{ + public static void main(String[] args) + { + try + { + if (args.length != 1) + { + System.out.println("usage: java Test listeningPort"); + System.exit(1); + } + System.out.println("Test progam starts"); + + WRServer server = new WRServer(Integer.parseInt(args[0])); + server.t.join(); + + System.out.println("Test progam ends"); + } catch (Exception e) + { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/CS2452/assign5/src/WRServer.java b/CS2452/assign5/src/WRServer.java new file mode 100755 index 0000000..eadddc3 --- /dev/null +++ b/CS2452/assign5/src/WRServer.java @@ -0,0 +1,59 @@ +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +class WRServer implements Runnable +{ + int port; + Thread t; + byte[] theSharedResource = null; + Semaphore flg = null; + Semaphore flg2 = null; + + WRServer(int p) + { + port = p; + theSharedResource = new byte[1024 * 1024]; + flg = new Semaphore(1); + flg2 = new Semaphore(2); + t = new Thread(this, "WRServer"); + t.start(); + } + + public void run() + { + ServerSocket ss = null; + Socket s = null; + DataOutputStream dout = null; + boolean running = true; + System.out.println("WRServer is alive"); + + ResourceWriter rw = new ResourceWriter(theSharedResource, flg, flg2); + + try + { + ss = new ServerSocket(port); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + while (running) + { + try + { + System.out.println("WRServer: waiting for incoming connection"); + s = ss.accept(); + System.out.println("WRServer: received incoming connection"); + + dout = new DataOutputStream(s.getOutputStream()); + ResourceReader rr = new ResourceReader(dout, theSharedResource, flg, flg2); + } catch (Exception e) + { + e.printStackTrace(); + } + } + + System.out.println("WRServer is finished"); + } +} -- cgit v1.1