-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientHandler.java
75 lines (71 loc) · 1.77 KB
/
ClientHandler.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class ClientHandler
{
Client c;
public static void main(String[] args)
{
executeInstruction(waitForInstructions());
}
ClientHandler(String ip)
{
c=new Client(ip);
}
public Instruction waitForInstructions() throws IOException
{
ServerSocket ss= new ServerSocket(2001);
//System.out.println(n);
ss.setReuseAddress(true);
ss.setSoTimeout(0);
//n++;
try
{
Socket s = ss.accept();
s.setReuseAddress(true);
s.setSoTimeout(0);
//System.out.println(s.getSoTimeout() );
//System.out.println(s.getReuseAddress() );
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
Instruction I=null;
I=(Instruction)ois.readObject();
is.close();
s.close();
ss.close();
return I;
}
catch(Exception e)
{
System.out.println(e.getLocalizedMessage());
return null;
}
}
public void executeInstruction(Instruction I)
{
if(I.getInst().equals("connectToServer"))
{
c=new Client(I.getReplicaIP()); //new client connected to server
}
if(I.getInst().equals("Write"))
{
if(I.getWrite().getOp().equals("add"))
{
c.add(I.getWrite().getSongEntry());
}
if(I.getWrite().getOp().equals("delete"))
{
c.delete(I.getWrite().getSongEntry());
}
if(I.getWrite().getOp().equals("modify"))
{
c.modify(I.getWrite().getSongEntry());
}
}
}
public String getServerIP()
{
return c.getServerIP();
}
public String getIP()
{
return ip;
}
}