-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
129 lines (111 loc) · 3.18 KB
/
main.cpp
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
/*
* main.cpp - Chat multiusuario basado en memoria compartida
*
* Este programa de Jesús Torres <[email protected]> está bajo una Licencia
* Creative Commons Public Domain Dedication 1.0.
*
* http://creativecommons.org/publicdomain/zero/1.0/deed.es
*/
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cstring>
#include "chatroom.h"
//
// Funciones para mostrar mensajes de ayuda y de error
//
const char* programShortName(const char* appName = NULL)
{
static const char* name = appName;
return name;
}
void showUsage (std::ostream& out = std::cout)
{
out << "Modo de empleo: "
<< programShortName()
<< " [[-u | --user username] CHATROOMID] | [-h | --help]"
<< std::endl;
}
void showOptionError (const std::string& message, const std::string& option = std::string())
{
std::cerr << programShortName() << ": " << message;
if ( ! option.empty() ) {
std::cerr << " -- " << option << std::endl;
}
showUsage(std::cerr);
}
//
// Función principal o punto de entrada del programa
//
int main(int argc, char** argv)
{
ChatRoom chatRoom;
std::string chatRoomId;
std::string username = getenv("USER");
std::vector<std::string> arguments(argv, argv + argc);
programShortName(basename(argv[0]));
if (arguments.size() < 2) {
showUsage();
exit(2);
}
// Análisis de las opciones de línea de comandos
for (unsigned i = 1; i < arguments.size(); ++i) {
const std::string& option = arguments[i];
if (option == "-h" || option == "--help") {
showUsage();
exit(0);
}
/* @2@NOTA
* Incluir la posibilidad de indicar un nombre de usuario se podría
* considerar como opcional.
*/
else if (option == "-u" || option == "--user") {
if ( ++i >= arguments.size()) {
showOptionError("la opción requiere un argumento", option);
exit(2);
}
username = arguments[i];
}
else if (option[0] != '-') {
chatRoomId = option;
}
else {
showOptionError("opción inválida", option);
exit(2);
}
}
if ( chatRoomId.empty() ) {
showOptionError("es necesario indicar un nombre para la sala de chat");
exit(2);
}
// Conectar a la sala de chat usando el identificador y nombre de usuario
// indicado
int result = chatRoom.connectTo(chatRoomId, username);
if ( result < 0 ) {
std::cerr << "error: ";
switch (result) {
case -1:
std::cerr << "instancia ya conectada a una sala";
break;
case -2:
std::cerr << "shm_open(): "
<< strerror(errno);
break;
case -3:
std::cerr << "ftruncate(): "
<< strerror(errno);
break;
case -4:
std::cerr << "mmap(): "
<< strerror(errno);
break;
default:
std::cerr << "error desconocido";
}
std::cerr << std::endl;
exit(3);
}
// Ejecutar el chat
chatRoom.run();
return 0;
}