-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDepartamento.java
More file actions
215 lines (188 loc) · 5.99 KB
/
Copy pathDepartamento.java
File metadata and controls
215 lines (188 loc) · 5.99 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
public class Departamento {
private String name;
private InetAddress ip;
private InetAddress central;
private Socket con;
private Socket peer;
private ObjectInputStream in;
private ObjectOutputStream out;
private ServerSocket server;
private HashMap<Integer, Student> students = new HashMap<Integer, Student>();
public Departamento(InetAddress central, String name) {
try {
this.ip = InetAddress.getLocalHost();
this.name = name;
this.central = central;
} catch (UnknownHostException e) {
e.printStackTrace();
}
connect();
}
public void connect(){
try {
this.con = new Socket(this.central, 8080);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Conectado a central");
}
public void listen(){
Processor p = new Processor(this);
try {
this.server = new ServerSocket(8081);
System.out.println("Departamento " + this.name + " listening");
//Passa conexão (socket) para o processador
p.add(this.server.accept());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addStudent(Student student){
this.students.put(student.getId(), student);
}
public void addStudent(String name, InetAddress dep){
Student tmp = new Student(name, dep);
this.students.put(tmp.getId(), tmp);
System.out.println(name + " adicionado!");
}
public Student getStudent(Student student){
return this.students.get(student.getId());
}
public void update(Student student){
this.students.put(student.getId(), student);
System.out.println(student.getName() + " atualizado!");
}
public void giveBack(String book, Student student){
if( this.students.containsKey(student.getId()) ){
this.students.get(student.getId()).removeBook(book);
} else {
try {
this.in = new ObjectInputStream( new DataInputStream(this.con.getInputStream()));
this.out = new ObjectOutputStream( new DataOutputStream( this.con.getOutputStream()));
//Pede localização para central
this.out.writeObject( student );
Student stdt = (Student) this.in.readObject();
//Cria conexão com dep
this.peer = new Socket(stdt.getDep(), 8081);
this.in = new ObjectInputStream( new DataInputStream(this.peer.getInputStream()));
this.out = new ObjectOutputStream( new DataOutputStream( this.peer.getOutputStream()));
stdt.request = true;
//Informa o objecto requisitado
this.out.writeObject(stdt);
stdt = (Student) this.in.readObject();
//Atualiza o objeto
stdt.removeBook(book);
stdt.request = false;
//Devolve atualizado
this.out.writeObject(stdt);
this.peer.close();
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void borrow(String book, Student student){
int index = student.getId();
if(this.students.get(student.getId()) != null){
if(this.students.get(index).canBorrow()){
this.students.get(index).addBook(book);
} else {
System.out.println("Limite de impréstimos atingido");
return;
}
} else {
try {
this.in = new ObjectInputStream( new DataInputStream(this.con.getInputStream()));
this.out = new ObjectOutputStream( new DataOutputStream( this.con.getOutputStream()));
//Envia objeto para central
this.out.writeObject(new Student(student.getId()));
//Recebe da central
Student stdt = (Student) this.in.readObject();
//Conecta ao outro departamento
this.peer = new Socket(stdt.getDep(), 8081);
this.in = new ObjectInputStream( new DataInputStream(this.peer.getInputStream()));
this.out = new ObjectOutputStream( new DataOutputStream(this.peer.getOutputStream()));
this.out.writeObject(student);
//Recebe cópia do objeto estudante do outro departamento
stdt = (Student) this.in.readObject();
if(stdt.canBorrow()){
stdt.addBook(book);
} else {
System.out.println("Limite de impréstimos atingido");
this.peer.close();
return;
}
//Devolve objeto modificado;
this.out.writeObject(stdt);
this.peer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Processor extends Thread {
private Stack<Socket> stack;
private ObjectInputStream in;
private ObjectOutputStream out;
private Socket dep;
private Departamento departament;
public Processor(Departamento d){
this.departament = d;
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(Socket student){
this.stack.add(student);
if(this.isInterrupted()){
this.notify();
}
}
@Override
public void run(){
while(true){
this.dep = this.stack.pop();
try {
this.in = new ObjectInputStream( new DataInputStream( this.dep.getInputStream()));
this.out = new ObjectOutputStream( new DataOutputStream( this.dep.getOutputStream()));
Student stdt = (Student) this.in.readObject();
//Diferenciar atualização de requisição
if(stdt.request){
//Request
stdt = this.departament.getStudent(stdt);
this.out.writeObject(stdt);
this.dep.close();
this.wait();
} else {
//Update
this.departament.update(stdt);
}
} catch (IOException | ClassNotFoundException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}