44import java .io .*;
55import java .net .Socket ;
66import java .util .concurrent .ConcurrentHashMap ;
7+ import java .util .concurrent .TimeUnit ;
78
8- public class DynamicClient implements Closeable ,ConnectionConstants {
9+ public class DynamicClient implements Closeable , ConnectionConstants {
910
1011 private final Object writeLock = new Object ();
1112 private final Socket socket ;
@@ -14,7 +15,7 @@ public class DynamicClient implements Closeable,ConnectionConstants {
1415 private final ConcurrentHashMap <String , Runnable > clRunnable ;
1516 private long lastWrite ;
1617
17- public DynamicClient (String host , int port , final String password ) throws IOException {
18+ public DynamicClient (final String host , final int port , final String password ) throws IOException {
1819 this (new Socket (host , port ), password );
1920 }
2021
@@ -31,122 +32,121 @@ public DynamicClient (final Socket socket, final String password) throws IOExcep
3132 this .startKeepAlive ();
3233 }
3334
34- private void startKeepAlive () {
35+ private void startKeepAlive () {
3536 this .lastWrite = System .currentTimeMillis ();
3637 new Thread () {
3738 @ Override
38- public void run () {
39+ public void run () {
3940 try {
4041 while (!this .isInterrupted () && !socket .isClosed ()) {
41- long current = System .currentTimeMillis ();
42+ final long current = System .currentTimeMillis ();
4243 if (lastWrite + 5000 < current ) {
4344 sendKeepAlive ();
4445 }
45- Thread .sleep (5000 );
46+ TimeUnit . MILLISECONDS .sleep (5000 );
4647 }
47- } catch (InterruptedException | IOException e ) {
48+ } catch (final InterruptedException | IOException e ) {
4849 e .printStackTrace ();
4950 }
5051 }
5152 }.start ();
5253 }
5354
54- public void execRunnable (final Class <? extends Runnable > clazz ) throws IOException {
55+ public void execRunnable (final Class <? extends Runnable > clazz ) {
5556 this .sendRunnable (clazz ).run ();
5657 }
5758
58- public Runnable sendRunnable (final Class <? extends Runnable > clazz ) throws IOException {
59+ public Runnable sendRunnable (final Class <? extends Runnable > clazz ) {
5960 if (clazz .getClassLoader () == null ) {
6061 return () -> {
6162 try {
6263 execClass (clazz .getName ());
63- } catch (IOException e ) {
64+ } catch (final IOException e ) {
6465 throw new RuntimeException (e );
6566 }
6667 };
6768 }
68- return clRunnable .computeIfAbsent (clazz .getName (), name -> {
69- InputStream is = clazz .getClassLoader ().getResourceAsStream (clazz .getName ().replace ('.' , '/' ) + ".class" );
69+ return clRunnable .computeIfAbsent (clazz .getName (), name -> {
70+ final InputStream is = clazz .getClassLoader ().getResourceAsStream (clazz .getName ().replace ('.' , '/' ) + ".class" );
7071 try {
7172 return sendStream (is , clazz .getName (), false );
72- } catch (IOException e ) {
73+ } catch (final IOException e ) {
7374 IOHelper .sneakyThrow (e );
7475 return null ;
7576 }
7677 });
7778 }
7879
7980 public Runnable sendFile (final File file ,final String className ) throws IOException {
80- try (final FileInputStream fis = new FileInputStream (file )) {
81+ try (final FileInputStream fis = new FileInputStream (file )) {
8182 return this .sendStream (fis , className , false );
8283 }
8384 }
8485
85- public Runnable sendStream (final InputStream is ,String name ,boolean execute ) throws IOException {
86+ public Runnable sendStream (final InputStream is , final String name , final boolean execute ) throws IOException {
8687 return this .sendData (IOHelper .readAllBytes (is ), name , execute );
8788 }
8889
89- public Runnable sendData ( byte [] buffer ,String name ,boolean execute ) throws IOException {
90- synchronized (writeLock ) {
91- dos .writeInt (execute ? ID_SEND_EXEC_DATA : ID_SEND_DATA );
92- dos .writeInt (buffer .length );
93- IOHelper .writeString (dos , name );
94- dos .write (buffer );
95- dos .flush ();
90+ public Runnable sendData ( final byte [] buffer , final String name , final boolean execute ) throws IOException {
91+ synchronized (this . writeLock ) {
92+ this . dos .writeInt (execute ? ID_SEND_EXEC_DATA : ID_SEND_DATA );
93+ this . dos .writeInt (buffer .length );
94+ IOHelper .writeString (this . dos , name );
95+ this . dos .write (buffer );
96+ this . dos .flush ();
9697 }
9798 return () -> {
9899 try {
99100 execData (name );
100- } catch (IOException e ) {
101+ } catch (final IOException e ) {
101102 throw new RuntimeException (e );
102103 }
103104 };
104105 }
105106
106- public void execData (String name ) throws IOException {
107- synchronized (writeLock ) {
108- dos .writeInt (ID_EXEC_DATA );
109- IOHelper .writeString (dos , name );
110- dos .flush ();
107+ public void execData (final String name ) throws IOException {
108+ synchronized (this .writeLock ) {
109+ this .dos .writeInt (ID_EXEC_DATA );
110+ IOHelper .writeString (this .dos , name );
111+ this .dos .flush ();
112+ }
113+ if (dis .readByte () != 0 ) {
114+ throw new IOException ("Data " + name + " was not sent yet" );
111115 }
112- if (dis .readByte () != 0 ) {
113- throw new IOException ("Data " + name + " was not sent yet" );
114- }
115-
116116 }
117117
118- public void execClass ( String name ) throws IOException {
119- synchronized (writeLock ) {
120- dos .writeInt (ID_EXEC_CLASS );
121- IOHelper .writeString (dos , name );
122- dos .flush ();
118+ public void execClass ( final String name ) throws IOException {
119+ synchronized (this . writeLock ) {
120+ this . dos .writeInt (ID_EXEC_CLASS );
121+ IOHelper .writeString (this . dos , name );
122+ this . dos .flush ();
123123 }
124124 }
125125
126- public void sendKeepAlive () throws IOException {
127- synchronized (writeLock ) {
128- dos .writeInt (ID_KEEP_ALIVE );
129- dos .flush ();
126+ public void sendKeepAlive () throws IOException {
127+ synchronized (this . writeLock ) {
128+ this . dos .writeInt (ID_KEEP_ALIVE );
129+ this . dos .flush ();
130130 }
131131 }
132132
133- public void remoteCloseServer () throws IOException {
134- synchronized (writeLock ) {
135- dos .writeInt (ID_CLOSE_SERVER );
136- dos .flush ();
133+ public void remoteCloseServer () throws IOException {
134+ synchronized (this . writeLock ) {
135+ this . dos .writeInt (ID_CLOSE_SERVER );
136+ this . dos .flush ();
137137 }
138- if (dis .readByte () != 0 ) {
138+ if (this . dis .readByte () != 0 ) {
139139 throw new SecurityException ("Client is not allowed to close the server" );
140140 }
141141 this .close ();
142142 }
143143
144144 @ Override
145145 public void close () throws IOException {
146- if (socket != null ) socket .close ();
146+ if (this . socket != null ) this . socket .close ();
147147 }
148148
149- public boolean isClosed () {
150- return socket == null || socket .isClosed ();
149+ public boolean isClosed () {
150+ return this . socket == null || this . socket .isClosed ();
151151 }
152152}
0 commit comments