aboutsummaryrefslogtreecommitdiff
path: root/CS2452/assign5/src/WRServer.java
blob: eadddc38b9ddb693195c0d7c0db7c25340feec16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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");
	}
}