1111import signalgo .client .util .GoConvertorHelper ;
1212import signalgo .client .models .GoDataType ;
1313import signalgo .client .util .GoBackStackHelper ;
14-
14+ import signalgo . client . GoSocketListener . SocketState ;
1515import java .io .*;
1616import java .lang .annotation .Annotation ;
1717import java .lang .reflect .AnnotatedElement ;
2121import java .net .URI ;
2222import java .nio .channels .Selector ;
2323import java .util .ArrayList ;
24- import java .util .LinkedHashMap ;
25- import java .util .List ;
2624import java .util .Map ;
2725import java .util .UUID ;
26+ import java .util .concurrent .ConcurrentHashMap ;
2827
2928/**
3029 * Created by Mehdi Akbarian on 2016-08-04.
@@ -38,7 +37,7 @@ public class Connector {
3837 private boolean onRecievedExeption = false , isAlive ;
3938 private Selector selector ;
4039 private int mPort , timeoutMills = 10000 ;
41- private LinkedHashMap <String , ClientDuplex > mPendingServices ;
40+ private ConcurrentHashMap <String , ClientDuplex > mPendingServices ;
4241 private InputStream inputStream ;
4342 private OutputStream outputStream ;
4443 private GoCallbackHandler callbackHandler ;
@@ -47,10 +46,11 @@ public class Connector {
4746 private GoClientHelper clientHelper ;
4847 private GoConvertorHelper convertorHelper ;
4948 private GoSocketListener socketListener ;
50- private GoSocketListener .SocketState currentState = GoSocketListener .SocketState .Disconnected ;
51- private GoSocketListener .SocketState lastState = GoSocketListener .SocketState .Disconnected ;
52-
49+ private SocketState currentState ;
50+ private SocketState lastState ;
5351 public Connector () {
52+ this .currentState = SocketState .Disconnected ;
53+ this .lastState = SocketState .Disconnected ;
5454 convertorHelper = new GoConvertorHelper ();
5555 goStreamReader = new GoStreamReader ();
5656 goStreamWriter = new GoStreamWriter ();
@@ -67,41 +67,47 @@ public Connector connectAsync(final String url) {
6767 public void run () {
6868 try {
6969 connect (url );
70- } catch (IOException ex ) {
70+ } catch (Exception ex ) {
7171 exceptionHandler (ex );
7272 }
7373 }
7474 }, false );
7575 return this ;
7676 }
7777
78- public Connector connect (String url ) throws IOException {
78+ public Connector connect (String url ) throws Exception {
7979 URI uri = URI .create (url );
8080 Connector connector = connect (uri .getHost (), uri .getPort ());
8181 firstInitial ();
82+ goStreamWriter .typeAuthentication (outputStream );
83+ if (!goStreamReader .onTypeAuthenticationResponse (inputStream ))
84+ throw new Exception ("server cant authenticat client type!" );
8285 connectData (uri .getPath ());
8386 listen ();
8487 syncAllServices ();
85- notifyListener (GoSocketListener . SocketState .Connected );
88+ this . notifyListener (SocketState .Connected );
8689 return connector ;
8790 }
8891
89- private void notifyListener (GoSocketListener .SocketState currentState ){
90- lastState =this .currentState ;
91- this .currentState =currentState ;
92- socketListener .onSocketChange (lastState ,currentState );
92+ private void notifyListener (SocketState currentState ) {
93+ this .lastState = this .currentState ;
94+ this .currentState = currentState ;
95+ if (socketListener !=null )
96+ this .socketListener .onSocketChange (this .lastState , currentState );
9397 }
98+
99+
94100 private Connector connect (String hostName , int port ) throws IOException {
95101 this .mHostName = hostName ;
96102 this .mPort = port ;
97103 socket = new Socket ();
98104 socket .connect (new InetSocketAddress (mHostName , mPort ), timeoutMills );
99- notifyListener (GoSocketListener . SocketState .connecting );
105+ this . notifyListener (SocketState .connecting );
100106 return this ;
101107 }
102108
103109 private void connectData (String url ) {
104- List <String > list = new ArrayList <String >();
110+ ArrayList <String > list = new ArrayList <String >();
105111 list .add (url );
106112 try {
107113 byte [] b = convertorHelper .byteConvertor (list );
@@ -118,11 +124,7 @@ private void connectData(String url) {
118124 MethodCallInfo methodCallInfo = new MethodCallInfo ();
119125 methodCallInfo .setGuid (UUID .randomUUID ().toString ());
120126 methodCallInfo .setServiceName ("/CheckConnection" );
121- // Object o = send(methodCallInfo, boolean.class);
122- // if (o == null || !((Boolean) o)) {
123- // exceptionHandler(new Exception("server is available but connection address is not true"));
124- // socket.close();
125- // }
127+
126128 } catch (Exception ex ) {
127129 exceptionHandler (ex );
128130 }
@@ -147,7 +149,7 @@ public void run() {
147149 } catch (Exception e ) {
148150 exceptionHandler (e );
149151 clientHelper .dispose ();
150- notifyListener (GoSocketListener . SocketState .Disconnected );
152+ notifyListener (SocketState .Disconnected );
151153 }
152154 }
153155 }
@@ -182,14 +184,13 @@ public void registerService(ClientDuplex cd) {
182184 for (Annotation annotation : annotations ) {
183185 if (annotation instanceof GoServiceName ) {
184186 if (mPendingServices == null ) {
185- mPendingServices = new LinkedHashMap <String , ClientDuplex >();
187+ mPendingServices = new ConcurrentHashMap <String , ClientDuplex >();
186188 }
187189 if (socket != null && socket .isConnected ()) {
188190 syncService (((GoServiceName ) annotation ).name ());
189191 } else {
190192 mPendingServices .put (((GoServiceName ) annotation ).name (), cd );
191193 }
192- cd .getConnector (this );
193194 if (((GoServiceName ) annotation ).usage () != GoServiceName .GoUsageType .invoke ) {
194195 initForCallback (cd );
195196 }
@@ -204,6 +205,7 @@ private void syncService(String name) {
204205 try {
205206 Object o = invoke ("/RegisterService" , name , Object .class );
206207 if (mPendingServices != null && mPendingServices .containsKey (name )) {
208+ ((ClientDuplex )this .mPendingServices .get (name )).getConnector (this );
207209 mPendingServices .remove (name );
208210 }
209211 } catch (Exception ex ) {
@@ -212,8 +214,6 @@ private void syncService(String name) {
212214 }
213215
214216 private void syncAllServices () {
215- if (mPendingServices ==null )
216- return ;
217217 for (Map .Entry <String , ClientDuplex > entry : mPendingServices .entrySet ()) {
218218 syncService (entry .getKey ());
219219 }
0 commit comments