File tree Expand file tree Collapse file tree 4 files changed +9
-3
lines changed
Programming on Java Lab №6/src Expand file tree Collapse file tree 4 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ public void parser() {
5656 sender .sendRequest (new Request (userCommand [0 ], userInputParser .inputId ()));
5757 break ;
5858 case "exit" :
59+ sender .sendRequest (new Request (userCommand [0 ]));
5960 exit = true ;
6061 break ;
6162 case "remove_any_by_start_date" :
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ public class RequestHandler implements Runnable{
2828 public RequestHandler (SocketChannel socket , WorkerManager workerManager ) {
2929 this .socket = socket ;
3030 this .workerManager = workerManager ;
31+
3132 }
3233
3334 public void run () {
@@ -68,7 +69,9 @@ public void run() {
6869 oos .writeObject (workerManager .clear ());
6970 break ;
7071 case "save_server" :
72+ case "exit" :
7173 workerManager .save ();
74+ oos .writeObject ("Коллекция сохранена в файл на сервере" );
7275 break ;
7376 case "remove_first" :
7477 oos .writeObject (workerManager .removeFirst ());
Original file line number Diff line number Diff line change 77import java .net .Socket ;
88import java .nio .channels .ServerSocketChannel ;
99import java .nio .channels .SocketChannel ;
10+ import java .util .Scanner ;
1011
1112/**
1213 * Created by IntelliJ IDEA.
@@ -29,7 +30,6 @@ public static void main(String[] args) {
2930 System .out .println ("Что пошло не так" );
3031 System .out .println (e .getMessage ());
3132 }
32-
3333 try (ServerSocketChannel serverSocket = ServerSocketChannel .open ()) {
3434 serverSocket .bind (new InetSocketAddress (PORT ));
3535 System .out .println ("Сервер запущен" );
@@ -39,11 +39,13 @@ public static void main(String[] args) {
3939 System .out .println ("Подключён клиент:" + "" +
4040 "\n \t addr = " + client .getLocalAddress () + "." );
4141 new Thread (new RequestHandler (client , workerManager )).start ();
42-
4342 }
4443 } catch (IOException e ) {
4544 System .out .println ("Не смогли подключится данному порту" );
4645 e .printStackTrace ();
4746 }
47+
48+ Runtime .getRuntime ().addShutdownHook (new Thread (workerManager != null ? workerManager ::save : null ));
4849 }
50+
4951}
Original file line number Diff line number Diff line change @@ -135,7 +135,7 @@ public String clear() {
135135
136136 public void save () {
137137 String stringBuilder = gson .toJson (workerList );
138- try (BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("Common.Common. Data.json" ))) {
138+ try (BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("Data.json" ))) {
139139 byte [] buffer = stringBuilder .getBytes ();
140140 bos .write (buffer , 0 , buffer .length );
141141 } catch (IOException e ) {
You can’t perform that action at this time.
0 commit comments