Skip to content

Commit 59bb842

Browse files
committed
fix some buges]
1 parent 4535dc6 commit 59bb842

File tree

10 files changed

+158
-86
lines changed

10 files changed

+158
-86
lines changed

src/main/java/signalgo/client/Connector.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import signalgo.client.util.GoConvertorHelper;
1212
import signalgo.client.models.GoDataType;
1313
import signalgo.client.util.GoBackStackHelper;
14-
14+
import signalgo.client.GoSocketListener.SocketState;
1515
import java.io.*;
1616
import java.lang.annotation.Annotation;
1717
import java.lang.reflect.AnnotatedElement;
@@ -21,10 +21,9 @@
2121
import java.net.URI;
2222
import java.nio.channels.Selector;
2323
import java.util.ArrayList;
24-
import java.util.LinkedHashMap;
25-
import java.util.List;
2624
import java.util.Map;
2725
import 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
}

src/main/java/signalgo/client/GoCallbackHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void registerMethods(Object o) {
7171
else
7272
methods = o.getClass().getMethods();
7373
for (Method m : methods) {
74-
if (m.getAnnotationsByType(GoMethodName.class) != null) {
74+
if (m.getAnnotation(GoMethodName.class) != null) {
7575
methodNames.add(m.getName());
7676
}
7777
}
@@ -93,7 +93,6 @@ public void onServerCallBack(MethodCallInfo mci) throws Exception {
9393
Object returnVal;
9494
if (o instanceof Class) {
9595
Object c = ((Class) o).newInstance();
96-
((ClientDuplex) c).getConnector(connector);
9796
returnVal = m.invoke(c, getParams(mci.getParameters(), m.getParameterTypes()));
9897
} else {
9998
returnVal = m.invoke(o, getParams(mci.getParameters(), m.getParameterTypes()));
@@ -106,12 +105,14 @@ public void onServerCallBack(MethodCallInfo mci) throws Exception {
106105
}
107106
}
108107

109-
private Object[] getParams(List<ParameterInfo> pis, Class<?>[] paramType) throws ClassNotFoundException, IOException {
108+
private Object[] getParams(List<ParameterInfo> pis, Class<?>[] paramType) throws ClassNotFoundException {
110109
Object[] params = new Object[pis.size()];
111110
for (int i = 0; i < pis.size(); i++) {
112-
//params[i] = (paramType[i].cast(pis.get(i).getValue()));
113-
params[i] = convertorHelper.deserialize(pis.get(i).getValue(), paramType[i]);
114-
111+
try {
112+
params[i]=convertorHelper.deserialize(pis.get(i).getValue(),paramType[i]);
113+
} catch (IOException e) {
114+
e.printStackTrace();
115+
}
115116
}
116117
return params;
117118
}

src/main/java/signalgo/client/GoStreamReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public byte[] readBlockToEnd(InputStream inputStream) throws IOException{
2020

2121
}
2222

23+
public boolean onTypeAuthenticationResponse(InputStream inputStream) throws IOException {
24+
byte[] response=read(inputStream,2);
25+
return true;
26+
}
27+
2328
private int readSize(InputStream inputStream)throws IOException{
2429
byte[] size=read(inputStream,4);
2530
return ByteBuffer.wrap(size).order(ByteOrder.nativeOrder()).getInt();

src/main/java/signalgo/client/GoStreamWriter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import signalgo.client.util.GoAsyncHelper;
1111
import signalgo.client.util.GoConvertorHelper;
1212
import com.fasterxml.jackson.core.JsonProcessingException;
13-
1413
import java.io.IOException;
1514
import java.io.OutputStream;
1615
import java.io.UnsupportedEncodingException;
1716
import java.nio.ByteBuffer;
17+
import java.nio.charset.Charset;
1818
import java.util.logging.Level;
1919
import java.util.logging.Logger;
2020

21+
2122
/**
2223
*
2324
* @author mehdi akbarian
@@ -51,6 +52,12 @@ public void run() {
5152
});
5253

5354
}
55+
56+
public void typeAuthentication(OutputStream outputStream) throws IOException {
57+
byte[] data=convertorHelper.byteConvertor("SignalGo/1.0");
58+
byte[] d= Charset.forName("UTF-8").encode("SignalGo/1.0").array();
59+
outputStream.write(data);
60+
}
5461

5562
public void sendDeliveryNotify(final OutputStream outputStream, MethodCallbackInfo callInfo) throws JsonProcessingException, UnsupportedEncodingException, IOException {
5663
byte[] data = convertorHelper.byteConvertor(callInfo);

src/main/java/signalgo/client/Main.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,31 @@
33
import java.io.IOException;
44

55
/**
6-
* Created by white on 2016-08-17.
6+
* Created by mehdi akbarian on 2016-08-06.
77
*/
8-
public class Main {
9-
10-
/**
11-
* Created by mehdi akbarian on 2016-08-06.
12-
*/
13-
static Connector connector;
14-
static boolean a = true;
15-
static TestService service;
16-
public static void main(String[] args) throws IOException {
17-
service = new TestService();
18-
connector=new Connector();
19-
connector.setTimeout(20000);
20-
connector.registerService(service);
21-
connector.connectAsync("http://82.102.13.99:9981/CPMServices");
22-
connector.onSocketChangeListener(new GoSocketListener() {
23-
public void onSocketChange(GoSocketListener.SocketState lastState, GoSocketListener.SocketState currentState) {
24-
if(lastState==SocketState.Disconnected && currentState==SocketState.Connected && a ){
25-
for(int i=0;i<100;i++){
26-
service.hello();
27-
System.err.println(""+i);
28-
}
29-
a=false;
8+
public class main {
9+
static Connector connector;
10+
static boolean a = true;
11+
static TestService service;
12+
public static void main(String[] args) throws IOException{
13+
service = new TestService();
14+
connector=new Connector();
15+
connector.setTimeout(20000);
16+
connector.registerService(service);
17+
connector.connectAsync("http://192.168.10.27:9981/CPMServices");
18+
connector.onSocketChangeListener(new GoSocketListener() {
19+
public void onSocketChange(GoSocketListener.SocketState lastState, GoSocketListener.SocketState currentState) {
20+
if(lastState==SocketState.Disconnected && currentState==SocketState.Connected && a ){
21+
for(int i=0;i<100;i++){
22+
service.hello();
23+
System.err.println(""+i);
3024
}
31-
}
32-
public void socketExeption(Exception e) {
33-
e.printStackTrace();
34-
}
35-
});
36-
}
25+
a=false;
26+
}
27+
}
28+
public void socketExeption(Exception e) {
29+
e.printStackTrace();
30+
}
31+
});
32+
}
3733
}

src/main/java/signalgo/client/TestService.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,34 @@ public TestService() {
2424
// super(connector);
2525

2626
}
27-
@GoMethodName(name = "GetAllProjects2",type = GoMethodName.MethodType.invoke)
27+
@GoMethodName(name = "hello",type = GoMethodName.MethodType.invoke)
2828
public void hello(){
29-
connector.autoInvokeAsync(new GoResponseHandler<MessageContract<List<Project>>>() {
30-
@Override
31-
public void onResponse(MessageContract<List<Project>> contract) {
32-
MessageContract<List<Project>> o=contract;
33-
System.err.println("hiiiiiiiiiiiiiiiiiiiiiiiii "+(++a));
29+
connector.invokeAsync("hello","CPMService",new GoResponseHandler() {
30+
public void onResponse(Object t) {
31+
if(t!=null){
32+
System.out.print(t.toString());
33+
3434
}
35-
},new DateTime(0));
35+
}
36+
},"mehdi");
37+
}
38+
39+
@GoMethodName(name = "GetUserName",type = GoMethodName.MethodType.emit)
40+
public String bye(){
41+
System.err.println("hhhhhhh");
42+
//hello();
43+
return "bye "+"mehdi";
44+
}
45+
46+
@GoMethodName(name = "GetData",type = GoMethodName.MethodType.invoke)
47+
public void getData(DateTime dateTime){
48+
System.err.println("time = "+dateTime);
49+
connector.autoInvokeAsync(new GoResponseHandler<MyClass>() {
50+
@Override
51+
public void onResponse(MyClass t) {
52+
System.out.println("myClass : "+t.dateTime);
53+
}
54+
}, dateTime);
3655
}
3756

3857

src/main/java/signalgo/client/models/MethodCallInfo.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class MethodCallInfo {
1717
private String serviceName;
1818
private String methodName;
1919
private Object data;
20+
private int type;
2021
private List<ParameterInfo> parameters;
2122

2223
public List<ParameterInfo> getParameters() {
@@ -58,8 +59,12 @@ public Object getData() {
5859
public void setData(Object data) {
5960
this.data = data;
6061
}
61-
62-
63-
64-
62+
63+
public int getType() {
64+
return type;
65+
}
66+
67+
public void setType(int type) {
68+
this.type = type;
69+
}
6570
}

src/main/java/signalgo/client/models/MethodCallbackInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void setData(String data) {
3030
this.data = data;
3131
}
3232

33-
public boolean getIsException() {
33+
public boolean isIsException() {
3434
return isException;
3535
}
3636

src/main/java/signalgo/client/util/GoClientHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public void endWait(String guid){
8585
}
8686

8787
public void dispose() {
88-
isDisposed = true;
8988
if(waitedMethodsForResponse==null)
9089
return;
90+
isDisposed = true;
9191
for (Map.Entry<String, GoKeyValue<GoAutoResetEvent, Object>> entry : waitedMethodsForResponse.entrySet()) {
9292
entry.getValue().getKey().notify();
9393
}

0 commit comments

Comments
 (0)