-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
126 lines (115 loc) · 5.96 KB
/
server.js
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
const net = require('net');
const readline = require('readline')
const {authorsController} = require('./controllers/authorsController');
const {booksController} = require('./controllers/booksController');
const {publishersController} = require('./controllers/publishersController')
const PORT = 8080;
const server = net.createServer((socket) => {
console.log('✅ Client connected');
let response = "";
socket.on('data', (data) => {
const message = data.toString().trim();
//command y args se utiliza para poder separar la acción que quiere realizar el cliente y los argumentos que envía como parámetro
const [command, ...args] = message.split(' ');
switch (command) {
//Se manejan todos los comandos GET que enviará el cliente, se utiliza el controlador y la función específica para cada comando
//Una vez obtenida la respuesta, se le envía al cliente
case 'GET':
if (args[0] === 'AUTHORS') {
const authors = JSON.parse(authorsController.getAuthors());
socket.write(`Autores: ${JSON.stringify(authors, null, 2)}`);
}
else if (args[0] === 'PUBLISHERS') {
const publishers = JSON.parse(publishersController.getPublishers());
socket.write(`Editoriales: ${JSON.stringify(publishers, null, 2)}\n`);
} else if (args[0] === 'BOOKS') {
const book = JSON.parse(booksController.getBooks());
socket.write(`Libros: ${JSON.stringify(book, null ,2)}\n`);
} else {
socket.write('Command not recognized\n');
}
break;
//Se manejan todos los comandos ADD que enviará el cliente, se utiliza el controlador y la función específica para cada comando
//Se utiliza slice y join para poder obtener fragmentos específicos que introduce el cliente
//Se valida en caso de que no se tengan los parámetros necesarios por ejemplo se revisa que se tenga el nombre, nacionalidad, ubicacion completos
//Una vez obtenida la respuesta, se le envía al cliente
case 'ADD':
if (args[0] === 'AUTHOR') {
const origin = args.slice(args.length - 1).join(' ');
const name = args.slice(1, args.length - 1).join(' ');
if (!name || !origin) {
socket.write('Error: Name and nationality cannot be empty.\n');
return;
}
const newAuthor = authorsController.addAuthor({name: name, nationality: origin});
socket.write(`Added Author: ${newAuthor}`);
} else if (args[0] === 'PUBLISHER') {
const name = args.slice(1, args.length -1).join(' ');
const located = args.slice(args.length - 1).join(' ');
if (!name || !located) {
socket.write('Error: Publisher and location cannot be empty.\n');
return;
}
const newPublisher = publishersController.addPublisher({publisherName: name, location: located});
socket.write(`Publisher added: ${newPublisher}`);
} else if (args[0] === 'BOOK') {
const data = message.split("+");
const name = data.slice(1, data.length -1).join(' ');
const author = data.slice(2).join(' ');
if (!name || !author) {
socket.write('Error: Title and nationality cannot be empty.\n');
return;
}
const newBook = booksController.addBook({newTitle: name, newBookAuthor: author});
socket.write(`Book added: ${newBook}`);
}else {
socket.write('Command not recognized\n');
}
break;
case 'SEARCH':
if(args[0] === 'BOOK'){
if(args[2] === 'TITLE'){
const titleBook = args.slice(3, data.length -1).join(' ');
response = booksController.searchBookByTitle(titleBook);
socket.write(`Book found: ${response}`)
}else if(args[2] === 'AUTHOR'){
const author = args.slice(3).join(' ');
response = booksController.searchBooksByAuthor(author);
socket.write(`Book(s) found: ${response}`)
}
}
if(args[0] === 'AUTHOR'){
const nameOrNationality = args.slice(1, data.length -1).join(' ');
response = authorsController.searchAuthor(nameOrNationality);
socket.write(`Author found: ${response}`)
}
if(args[0] === 'PUBLISHER'){
const nameOrLocation = args.slice(1, data.length -1).join(' ');
response = publishersController.searchPublisher(nameOrLocation);
socket.write(`Publisher found: ${response}`)
}
break;
//Es el comando para salir del uso de la api
case 'EXIT':
command.toUpperCase();
socket.write('Connection finished!');
socket.end();
break;
default:
socket.write('Command not recognized!\n');
break;
}
});
socket.on('error', (err) => {
console.error(`⚠️ Client connection error ${err.message}`);
})
socket.on('end', () => {
console.log('⚠️ Client is disconnected')
})
socket.on('close', () => {
console.log('❌ Client connection is closed');
});
});
server.listen(PORT, () => {
console.log(`🔊 TCP Server is listening on PORT ${PORT}`);
});