Skip to content

Commit

Permalink
fix some buges]
Browse files Browse the repository at this point in the history
  • Loading branch information
makbn committed Sep 8, 2016
1 parent 4535dc6 commit 59bb842
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 86 deletions.
52 changes: 26 additions & 26 deletions src/main/java/signalgo/client/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import signalgo.client.util.GoConvertorHelper;
import signalgo.client.models.GoDataType;
import signalgo.client.util.GoBackStackHelper;

import signalgo.client.GoSocketListener.SocketState;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
Expand All @@ -21,10 +21,9 @@
import java.net.URI;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by Mehdi Akbarian on 2016-08-04.
Expand All @@ -38,7 +37,7 @@ public class Connector {
private boolean onRecievedExeption = false, isAlive;
private Selector selector;
private int mPort, timeoutMills = 10000;
private LinkedHashMap<String, ClientDuplex> mPendingServices;
private ConcurrentHashMap<String, ClientDuplex> mPendingServices;
private InputStream inputStream;
private OutputStream outputStream;
private GoCallbackHandler callbackHandler;
Expand All @@ -47,10 +46,11 @@ public class Connector {
private GoClientHelper clientHelper;
private GoConvertorHelper convertorHelper;
private GoSocketListener socketListener;
private GoSocketListener.SocketState currentState = GoSocketListener.SocketState.Disconnected;
private GoSocketListener.SocketState lastState = GoSocketListener.SocketState.Disconnected;

private SocketState currentState;
private SocketState lastState;
public Connector() {
this.currentState = SocketState.Disconnected;
this.lastState = SocketState.Disconnected;
convertorHelper = new GoConvertorHelper();
goStreamReader = new GoStreamReader();
goStreamWriter = new GoStreamWriter();
Expand All @@ -67,41 +67,47 @@ public Connector connectAsync(final String url) {
public void run() {
try {
connect(url);
} catch (IOException ex) {
} catch (Exception ex) {
exceptionHandler(ex);
}
}
}, false);
return this;
}

public Connector connect(String url) throws IOException {
public Connector connect(String url) throws Exception {
URI uri = URI.create(url);
Connector connector = connect(uri.getHost(), uri.getPort());
firstInitial();
goStreamWriter.typeAuthentication(outputStream);
if(!goStreamReader.onTypeAuthenticationResponse(inputStream))
throw new Exception("server cant authenticat client type!");
connectData(uri.getPath());
listen();
syncAllServices();
notifyListener(GoSocketListener.SocketState.Connected);
this.notifyListener(SocketState.Connected);
return connector;
}

private void notifyListener(GoSocketListener.SocketState currentState){
lastState=this.currentState;
this.currentState=currentState;
socketListener.onSocketChange(lastState,currentState);
private void notifyListener(SocketState currentState) {
this.lastState = this.currentState;
this.currentState = currentState;
if(socketListener!=null)
this.socketListener.onSocketChange(this.lastState, currentState);
}


private Connector connect(String hostName, int port) throws IOException {
this.mHostName = hostName;
this.mPort = port;
socket = new Socket();
socket.connect(new InetSocketAddress(mHostName, mPort), timeoutMills);
notifyListener(GoSocketListener.SocketState.connecting);
this.notifyListener(SocketState.connecting);
return this;
}

private void connectData(String url) {
List<String> list = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
list.add(url);
try {
byte[] b = convertorHelper.byteConvertor(list);
Expand All @@ -118,11 +124,7 @@ private void connectData(String url) {
MethodCallInfo methodCallInfo = new MethodCallInfo();
methodCallInfo.setGuid(UUID.randomUUID().toString());
methodCallInfo.setServiceName("/CheckConnection");
// Object o = send(methodCallInfo, boolean.class);
// if (o == null || !((Boolean) o)) {
// exceptionHandler(new Exception("server is available but connection address is not true"));
// socket.close();
// }

} catch (Exception ex) {
exceptionHandler(ex);
}
Expand All @@ -147,7 +149,7 @@ public void run() {
} catch (Exception e) {
exceptionHandler(e);
clientHelper.dispose();
notifyListener(GoSocketListener.SocketState.Disconnected);
notifyListener(SocketState.Disconnected);
}
}
}
Expand Down Expand Up @@ -182,14 +184,13 @@ public void registerService(ClientDuplex cd) {
for (Annotation annotation : annotations) {
if (annotation instanceof GoServiceName) {
if (mPendingServices == null) {
mPendingServices = new LinkedHashMap<String, ClientDuplex>();
mPendingServices = new ConcurrentHashMap<String, ClientDuplex>();
}
if (socket != null && socket.isConnected()) {
syncService(((GoServiceName) annotation).name());
} else {
mPendingServices.put(((GoServiceName) annotation).name(), cd);
}
cd.getConnector(this);
if (((GoServiceName) annotation).usage() != GoServiceName.GoUsageType.invoke) {
initForCallback(cd);
}
Expand All @@ -204,6 +205,7 @@ private void syncService(String name) {
try {
Object o = invoke("/RegisterService", name, Object.class);
if (mPendingServices != null && mPendingServices.containsKey(name)) {
((ClientDuplex)this.mPendingServices.get(name)).getConnector(this);
mPendingServices.remove(name);
}
} catch (Exception ex) {
Expand All @@ -212,8 +214,6 @@ private void syncService(String name) {
}

private void syncAllServices() {
if(mPendingServices==null)
return;
for (Map.Entry<String, ClientDuplex> entry : mPendingServices.entrySet()) {
syncService(entry.getKey());
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/signalgo/client/GoCallbackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void registerMethods(Object o) {
else
methods = o.getClass().getMethods();
for (Method m : methods) {
if (m.getAnnotationsByType(GoMethodName.class) != null) {
if (m.getAnnotation(GoMethodName.class) != null) {
methodNames.add(m.getName());
}
}
Expand All @@ -93,7 +93,6 @@ public void onServerCallBack(MethodCallInfo mci) throws Exception {
Object returnVal;
if (o instanceof Class) {
Object c = ((Class) o).newInstance();
((ClientDuplex) c).getConnector(connector);
returnVal = m.invoke(c, getParams(mci.getParameters(), m.getParameterTypes()));
} else {
returnVal = m.invoke(o, getParams(mci.getParameters(), m.getParameterTypes()));
Expand All @@ -106,12 +105,14 @@ public void onServerCallBack(MethodCallInfo mci) throws Exception {
}
}

private Object[] getParams(List<ParameterInfo> pis, Class<?>[] paramType) throws ClassNotFoundException, IOException {
private Object[] getParams(List<ParameterInfo> pis, Class<?>[] paramType) throws ClassNotFoundException {
Object[] params = new Object[pis.size()];
for (int i = 0; i < pis.size(); i++) {
//params[i] = (paramType[i].cast(pis.get(i).getValue()));
params[i] = convertorHelper.deserialize(pis.get(i).getValue(), paramType[i]);

try {
params[i]=convertorHelper.deserialize(pis.get(i).getValue(),paramType[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
return params;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/signalgo/client/GoStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public byte[] readBlockToEnd(InputStream inputStream) throws IOException{

}

public boolean onTypeAuthenticationResponse(InputStream inputStream) throws IOException {
byte[] response=read(inputStream,2);
return true;
}

private int readSize(InputStream inputStream)throws IOException{
byte[] size=read(inputStream,4);
return ByteBuffer.wrap(size).order(ByteOrder.nativeOrder()).getInt();
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/signalgo/client/GoStreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import signalgo.client.util.GoAsyncHelper;
import signalgo.client.util.GoConvertorHelper;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
*
* @author mehdi akbarian
Expand Down Expand Up @@ -51,6 +52,12 @@ public void run() {
});

}

public void typeAuthentication(OutputStream outputStream) throws IOException {
byte[] data=convertorHelper.byteConvertor("SignalGo/1.0");
byte[] d= Charset.forName("UTF-8").encode("SignalGo/1.0").array();
outputStream.write(data);
}

public void sendDeliveryNotify(final OutputStream outputStream, MethodCallbackInfo callInfo) throws JsonProcessingException, UnsupportedEncodingException, IOException {
byte[] data = convertorHelper.byteConvertor(callInfo);
Expand Down
54 changes: 25 additions & 29 deletions src/main/java/signalgo/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,31 @@
import java.io.IOException;

/**
* Created by white on 2016-08-17.
* Created by mehdi akbarian on 2016-08-06.
*/
public class Main {

/**
* Created by mehdi akbarian on 2016-08-06.
*/
static Connector connector;
static boolean a = true;
static TestService service;
public static void main(String[] args) throws IOException {
service = new TestService();
connector=new Connector();
connector.setTimeout(20000);
connector.registerService(service);
connector.connectAsync("http://82.102.13.99:9981/CPMServices");
connector.onSocketChangeListener(new GoSocketListener() {
public void onSocketChange(GoSocketListener.SocketState lastState, GoSocketListener.SocketState currentState) {
if(lastState==SocketState.Disconnected && currentState==SocketState.Connected && a ){
for(int i=0;i<100;i++){
service.hello();
System.err.println(""+i);
}
a=false;
public class main {
static Connector connector;
static boolean a = true;
static TestService service;
public static void main(String[] args) throws IOException{
service = new TestService();
connector=new Connector();
connector.setTimeout(20000);
connector.registerService(service);
connector.connectAsync("http://192.168.10.27:9981/CPMServices");
connector.onSocketChangeListener(new GoSocketListener() {
public void onSocketChange(GoSocketListener.SocketState lastState, GoSocketListener.SocketState currentState) {
if(lastState==SocketState.Disconnected && currentState==SocketState.Connected && a ){
for(int i=0;i<100;i++){
service.hello();
System.err.println(""+i);
}
}
public void socketExeption(Exception e) {
e.printStackTrace();
}
});
}
a=false;
}
}
public void socketExeption(Exception e) {
e.printStackTrace();
}
});
}
}
33 changes: 26 additions & 7 deletions src/main/java/signalgo/client/TestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,34 @@ public TestService() {
// super(connector);

}
@GoMethodName(name = "GetAllProjects2",type = GoMethodName.MethodType.invoke)
@GoMethodName(name = "hello",type = GoMethodName.MethodType.invoke)
public void hello(){
connector.autoInvokeAsync(new GoResponseHandler<MessageContract<List<Project>>>() {
@Override
public void onResponse(MessageContract<List<Project>> contract) {
MessageContract<List<Project>> o=contract;
System.err.println("hiiiiiiiiiiiiiiiiiiiiiiiii "+(++a));
connector.invokeAsync("hello","CPMService",new GoResponseHandler() {
public void onResponse(Object t) {
if(t!=null){
System.out.print(t.toString());

}
},new DateTime(0));
}
},"mehdi");
}

@GoMethodName(name = "GetUserName",type = GoMethodName.MethodType.emit)
public String bye(){
System.err.println("hhhhhhh");
//hello();
return "bye "+"mehdi";
}

@GoMethodName(name = "GetData",type = GoMethodName.MethodType.invoke)
public void getData(DateTime dateTime){
System.err.println("time = "+dateTime);
connector.autoInvokeAsync(new GoResponseHandler<MyClass>() {
@Override
public void onResponse(MyClass t) {
System.out.println("myClass : "+t.dateTime);
}
}, dateTime);
}


Expand Down
13 changes: 9 additions & 4 deletions src/main/java/signalgo/client/models/MethodCallInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MethodCallInfo {
private String serviceName;
private String methodName;
private Object data;
private int type;
private List<ParameterInfo> parameters;

public List<ParameterInfo> getParameters() {
Expand Down Expand Up @@ -58,8 +59,12 @@ public Object getData() {
public void setData(Object data) {
this.data = data;
}





public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setData(String data) {
this.data = data;
}

public boolean getIsException() {
public boolean isIsException() {
return isException;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/signalgo/client/util/GoClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public void endWait(String guid){
}

public void dispose() {
isDisposed = true;
if(waitedMethodsForResponse==null)
return;
isDisposed = true;
for (Map.Entry<String, GoKeyValue<GoAutoResetEvent, Object>> entry : waitedMethodsForResponse.entrySet()) {
entry.getValue().getKey().notify();
}
Expand Down
Loading

0 comments on commit 59bb842

Please sign in to comment.