Hi all,
I have implemented Ondrej's method using TCP/IP connection and had it working but lost the source code and started once again from scratch. But this time, I cannot get it to work. Can anyone tell me what I am doing wrong?
Here is the code in C#:
public class MyRequest
{
public string Text { get; set; }
public bool Status { get; set; }
}
public class MyResponse
{
public int Length { get; set; }
}
class Program
{
private static IDuplexTypedMessageReceiver<MyResponse, MyRequest> myReceiver;
static void Main(string[] args)
{
IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
myReceiver.MessageReceived += OnMessageReceived;
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel =
aMessaging.CreateDuplexInputChannel("tcp://XXX.XXX.XXX.X:8060/");
myReceiver.AttachDuplexInputChannel(anInputChannel);
Console.WriteLine("The service is running. To stop press enter.");
Console.ReadLine();
myReceiver.DetachDuplexInputChannel();
}
private static void OnMessageReceived(object sender,
TypedRequestReceivedEventArgs<MyRequest> e)
{
Console.WriteLine("Received: " + e.RequestMessage.Text);
MyResponse aResponse = new MyResponse();
aResponse.Length = e.RequestMessage.Text.Length;
myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
}
}
This is the java code:
public static class MyRequest
{
public String Text;
}
public static class MyResponse
{
public int Length;
}
private Handler myRefresh = new Handler();
private EditText myMessageTextEditText;
private EditText myResponseEditText;
private Button mySendRequestBtn;
private IDuplexTypedMessageSender<MyResponse, MyRequest> mySender;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMessageTextEditText = (EditText) findViewById(R.id.inputPinNo);
myResponseEditText = (EditText) findViewById(R.id.editText);
mySendRequestBtn = (Button) findViewById(R.id.SendDetailsBtn);
mySendRequestBtn.setOnClickListener(myOnSendRequestClickHandler);
Thread anOpenConnectionThread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
openConnection();
}
catch (Exception err)
{
EneterTrace.error("Open connection failed.", err);
}
}
});
anOpenConnectionThread.start();
}
@Override
public void onDestroy()
{
mySender.detachDuplexOutputChannel();
super.onDestroy();
}
private void openConnection() throws Exception
{
IDuplexTypedMessagesFactory aSenderFactory =
new DuplexTypedMessagesFactory();
mySender = aSenderFactory.createDuplexTypedMessageSender(MyResponse.class, MyRequest.class);
mySender.responseReceived().subscribe(myOnResponseHandler);
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel =
aMessaging.createDuplexOutputChannel("tcp://XXX.XXX.XXX.X:8060/");
mySender.attachDuplexOutputChannel(anOutputChannel);
}
private void onSendRequest(View v)
{
MyRequest aRequestMsg = new MyRequest();
aRequestMsg.Text = myMessageTextEditText.getText().toString();
try
{
mySender.sendRequestMessage(aRequestMsg);
}
catch (Exception err)
{
EneterTrace.error("Sending the message failed.", err);
}
}
private void onResponseReceived(Object sender, final TypedResponseReceivedEventArgs<MyResponse> e)
{
myRefresh.post(new Runnable()
{
@Override
public void run()
{
myResponseEditText.setText(Integer.toString(e.getResponseMessage().Length));
}
});
}
private EventHandler<TypedResponseReceivedEventArgs<MyResponse>> myOnResponseHandler
= new EventHandler<TypedResponseReceivedEventArgs<MyResponse>>()
{
@Override
public void onEvent(Object sender,
TypedResponseReceivedEventArgs<MyResponse> e)
{
onResponseReceived(sender, e);
}
};
private OnClickListener myOnSendRequestClickHandler = new OnClickListener()
{
@Override
public void onClick(View v)
{
onSendRequest(v);
}
};
}
I used the same IP address in both projects.
Thanks! Any help will be immensely appreciated!