-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservidor.java
213 lines (181 loc) · 9.32 KB
/
servidor.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
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
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class servidor extends Application {
private ServerSocket server;
private TextArea textArea;
private final librosQuerie libroQuerie = new librosQuerie();
@Override
public void start(Stage primaryStage) {
textArea = new TextArea();
textArea.setEditable(false);
ScrollPane scrollPane = new ScrollPane(textArea);
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
Scene scene = new Scene(scrollPane, 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Servidor");
primaryStage.show();
new Thread(() -> {
try {
server = new ServerSocket(8000); // Establecer el ServerSocket
textArea.appendText("Server Started \n");
while (true) {
// Esperar y aceptar una conexión de cliente
textArea.appendText("Esperando una conexión de cliente...\n");
Socket clientSocket = server.accept();
textArea.appendText("Cliente conectado desde " + clientSocket.getInetAddress() + "\n");
Handler handler = new Handler(clientSocket);
new Thread(handler).start();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}).start();
}
private class Handler implements Runnable {
private Socket socket; // conexión con el cliente
private DataInputStream input; // entrada desde el cliente
private DataOutputStream output; // salida hacia el cliente
// configurar la comunicación
public Handler(Socket socket) {
this.socket = socket; // establecer la conexión
try{
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(socket.getInputStream());
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
// enviar mensajes al cliente
public void run() {
try{
while (true) {
String command = input.readUTF();
String[] parts = command.split(" ", 2);
if (parts[0].equals("GET_ALL")) {
textArea.appendText("Busco todos los libros...\n");
// Simula una lista de libros (cámbialo según tus datos)
List<libros> resultados = getAll();
ObjectOutputStream objectOutputToClient = new ObjectOutputStream(output);
objectOutputToClient.writeObject(resultados);
objectOutputToClient.flush();
}
else if (parts[0].equals("GET_ISBN")) {
textArea.appendText("Busco por isbn...\n");
// La solicitud es para buscar libros por ISBN
int isbn = Integer.parseInt(parts[1]); // 8 es la longitud de "GET_ISBN "
// Lógica para buscar libros por ISBN y obtener la lista de resultados
List<libros> resultados = getByIsbn(isbn);
ObjectOutputStream objectOutputToClient = new ObjectOutputStream(output);
objectOutputToClient.writeObject(resultados);
objectOutputToClient.flush();
}
else if(parts[0].equals("GET_TITULO")) {
textArea.appendText("Busco por titulo...\n");
// La solicitud es para buscar libros por ISBN
String titulo = parts[1]; // 11 es la longitud
// Lógica para buscar libros por ISBN y obtener la lista de resultados
List<libros> resultados = getByTitulo(titulo);
ObjectOutputStream objectOutputToClient = new ObjectOutputStream(output);
objectOutputToClient.writeObject(resultados);
objectOutputToClient.flush();
}
else if(parts[0].equals("GET_AUTOR")) {
textArea.appendText("Busco por autor...\n");
// La solicitud es para buscar libros por autor
String autor = parts[1]; // 11 es la longitud
// Lógica para buscar libros por ISBN y obtener la lista de resultados
List<libros> resultados = getByAutor(autor);
ObjectOutputStream objectOutputToClient = new ObjectOutputStream(output);
objectOutputToClient.writeObject(resultados);
objectOutputToClient.flush();
}
else if (parts[0].equals("GET_ANIO")) {
textArea.appendText("Busco por anio...\n");
// La solicitud es para buscar libros por anio
int anio = Integer.parseInt(parts[1]); // 8 es la longitud de "GET_ISBN "
// Lógica para buscar libros por ISBN y obtener la lista de resultados
List<libros> resultados = getByAnio(anio);
ObjectOutputStream objectOutputToClient = new ObjectOutputStream(output);
objectOutputToClient.writeObject(resultados);
objectOutputToClient.flush();
}
else if (parts[0].equals("ADD_LIBRO")){
textArea.appendText("Agrego un libro...\n");
// La solicitud es para agregar un libro
String libro = parts[1]; // 10 es la longitud de "ADD_LIBRO "
// Lógica para agregar un libro
String[] libroArray = libro.split(",");
int isbn = Integer.parseInt(libroArray[0]);
String titulo = libroArray[1];
String autor = libroArray[2];
int anio = Integer.parseInt(libroArray[3]);
libroQuerie.addLibros(isbn, titulo, autor, anio);
}
else if (parts[0].equals("DELETE_LIBRO")){
textArea.appendText("Elimino un libro...\n");
// La solicitud es para agregar un libro
int isbn = Integer.parseInt(parts[1]); // 10 es la longitud de "DEL_LIBRO "
// Lógica para borrar un libro
libroQuerie.deleteLibro(isbn);
}
else if (parts[0].equals("UPDATE_LIBRO")){
textArea.appendText("Actualizo un libro...\n");
// La solicitud es para agregar un libro
String libro = parts[1]; // 10 es la longitud de "DEL_LIBRO "
// Lógica para borrar un libro
String[] libroArray = libro.split(",");
int isbn = Integer.parseInt(libroArray[0]);
String titulo = libroArray[1];
String autor = libroArray[2];
int anio = Integer.parseInt(libroArray[3]);
int a = libroQuerie.updateLibro(isbn, titulo, autor, anio);
}
else if (parts[0].equals("CLOSE")) {
textArea.appendText("Cerrando la conexión con el cliente...\n");
break; // Salir del bucle while
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
private List<libros> getAll() {
List<libros> listaLibros = libroQuerie.getAllLibros();
return listaLibros;
}
private List<libros> getByIsbn(int isbn) {
List<libros> listaLibros = libroQuerie.getLibrosByIsbn(isbn);
return listaLibros;
}
private List<libros> getByAutor(String autor) {
List<libros> listaLibros = libroQuerie.getLibrosByAutor(autor);
return listaLibros;
}
private List<libros> getByTitulo(String titulo) {
List<libros> listaLibros = libroQuerie.getLibrosByTitulo(titulo);
return listaLibros;
}
private List<libros> getByAnio(int anio) {
List<libros> listaLibros = libroQuerie.getLibrosByAnio(anio);
return listaLibros;
}
public static void main(String[] args) {
launch(args);
}
}