This repository was archived by the owner on Sep 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunChatServer.java
More file actions
164 lines (150 loc) · 3.88 KB
/
Copy pathRunChatServer.java
File metadata and controls
164 lines (150 loc) · 3.88 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
import java.io.IOException;
import java.net.SocketException;
import chat.Failure;
import chat.server.ChatServer;
/**
* Classe/programme qui lance un serveur de chat
* @author davidroussel
*/
public class RunChatServer extends AbstractRunChat
{
/**
* Time out de la server socket avant qu'elle ne recommence à attendre
* des connections des éventuels clients
*/
private int timeout;
/**
* Flag permettant (ou pas) de quitter le serveur lorsque le dernier
* client se délogue
*/
private boolean quitOnLastclient;
/**
* Default time out to wait for client connection : 5 seconds
*/
public static final int DEFAULTTIMEOUT = 5000;
/**
* Constructeur d'un lanceur de serveur d'après les arguments du programme
* principal
* @param args les arguments du programme principal
*/
protected RunChatServer(String[] args)
{
super(args);
}
/**
* Mise en place des attributs du serveur de chat en fonction des arguments
* utilisés dans la ligne de commande
* @param args les arguments fournis au programme principal.
*/
@Override
protected void setAttributes(String[] args)
{
/*
* On met d'abord les attributs locaux à leur valeur par défaut
*/
timeout = DEFAULTTIMEOUT;
quitOnLastclient = true;
/*
* parsing des arguments communs aux clients et serveur
* -v | --verbose
* -p | --port : port à utiliser pour la serverSocket
*/
super.setAttributes(args);
/*
* parsing des arguments spécifique au serveur
* -t | --timeout : timeout d'attente de la server socket
*/
for (int i=0; i < args.length; i++)
{
if (args[i].equals("--timeout") || args[i].equals("-t"))
{
if (i < (args.length - 1))
{
// parse next arg for in port value
Integer timeInteger = readInt(args[++i]);
if (timeInteger != null)
{
timeout = timeInteger.intValue();
}
logger.info("Setting timeout to " + timeout);
}
else
{
logger.warning("invalid timeout value");
}
}
if (args[i].equals("--quit") || args[i].equals("-q"))
{
quitOnLastclient = true;
logger.info("Setting quit on last client to true");
}
if (args[i].equals("--noquit") || args[i].equals("-n"))
{
quitOnLastclient = false;
logger.info("Setting quit on last client to false");
}
}
}
/**
* Lancement du serveur de chat
*/
@Override
protected void launch()
{
/*
* Create and Launch server on local ip adress with port number and verbose
* status
*/
logger.info("Creating server on port " + port + " with timeout "
+ timeout + " ms and verbose " + (verbose ? "on" : "off"));
ChatServer server = null;
try
{
server = new ChatServer(port, timeout, quitOnLastclient, logger);
}
catch (SocketException se)
{
logger.severe(Failure.SET_SERVER_SOCKET_TIMEOUT + ", abort ...");
logger.severe(se.getLocalizedMessage());
System.exit(Failure.SET_SERVER_SOCKET_TIMEOUT.toInteger());
}
catch (IOException e)
{
logger.severe(Failure.CREATE_SERVER_SOCKET + ", abort ...");
e.printStackTrace();
System.exit(Failure.CREATE_SERVER_SOCKET.toInteger());
}
// Wait for serverThread to stop
Thread serverThread = null;
if (server != null)
{
serverThread = new Thread(server);
serverThread.start();
logger.info("Waiting for server to terminate ... ");
try
{
serverThread.join();
logger.fine("Server terminated, program end.");
}
catch (InterruptedException e)
{
logger.severe("Server Thread Join interrupted");
logger.severe(e.getLocalizedMessage());
}
}
}
/**
* Programme principal
* @param args les arguments
* <ul>
* <li>--port <port number> : set host connection port</li>
* <li>--verbose : set verbose on</li>
* <li>--timeout <timeout in ms> : server socket waiting time out</li>
* </ul>
*/
public static void main(String[] args)
{
RunChatServer server = new RunChatServer(args);
server.launch();
}
}