Can't get Java QPID to work - java

I'm trying to create a simple AMQP application in Java. The application should simply bounce a number between two clients. Each time the number is received the client should add 1 to it and send it back to the other client. However, I cannot get the example to work at all. I can send the first number, but it is never received. In the webinterface of the broker I see zero connections and messages. I am doing something wrong, but can't figure out what it is. Hopefully someone here can spot the error(s). Below is the code I've written.
Messenger mng = Proton.messenger();
mng.start();
mng.subscribe("localhost:5672");
Message msg = Proton.message();
msg.setAddress("localhost:5672");
msg.setSubject("foobar");
if (args.length > 2 && args[2].equals("foo"))
{
System.out.println("Sending initial: 1");
msg.setBody(new AmqpValue("1"));
mng.put(msg);
mng.send();
System.out.println("Sent initial: 1");
}
try
{
while (true)
{
mng.recv(1);
while(mng.incoming() > 0) {
Message message = mng.get();
int consumed = Integer.parseInt(message.getBody().toString());
System.out.println("Received: " + consumed);
consumed = consumed % 100;
if (consumed == 0)
{
long seconds = (System.currentTimeMillis() - start) / 1000;
start = System.currentTimeMillis();
System.out.println("Last hundred messages took (s): " + seconds);
}
msg.setBody(new AmqpValue(""+(consumed+1)));
mng.put(msg);
mng.send();
}
}
}
catch (Exception e)
{
System.out.println("proton error: " + e.getMessage());
}

Well, I turned off my firewall completely and changed the code a bit to listen and subscribe to my local IP address and that worked well for me:
Messenger mng = Proton.messenger();
mng.start();
mng.subscribe("amqp://~xxx.xxx.xxx.xxx");
Message msg = Proton.message();
msg.setAddress("amqp://yyy.yyy.yyy.yyy");
....

Related

How to retry UDP packet request after wait time in android

I'm requesting data by sending a request command over UDP, and if it works, I'm supposed to receive a response. However, in the case that a response doesn't come through, I want to wait 3 seconds, then send a request again. I want to try this 3 times before "giving up" and displaying an error.
The code I have implemented is currently sending out the request once, but isn't sending it again one it realizes there is no response and I'm not sure where I'm going wrong.
int counter = 0;
static final int Limit = 3;
public void sendReq() {
new Thread((Runnable) () -> {
try {
...
// Sending Request command here ...
// Receiving
while(true) {
DatagramPacket dp = new DatagramPacket(buf, buf.length);
socket.receive(dp);
byte[] response = dp.getData();
if(response.length == 0 || dp == null) {
counter = counter + 1;
if(counter == Limit) {
// give up, display error
} else {
Thread.sleep(3000); // Wait 3 seconds
sendReq();
}
} else {
// Do stuff with response
}
}
}
On the device I'm sending my command to, I made it so it holds its response on the first request, and I know the response isn't coming back. However my code isn't retrying.
What should I do?

JackMidi.eventWrite - time parameter

I'm using this library: https://github.com/jaudiolibs/jnajack
I created a simple project to reproduce my issue: https://github.com/sc3sc3/MidiJnaJackTest
I have a JackPort outputPort running and appears in QjackCtl in 'Output Ports'.
In QjackCtl this outputPort is connected to GMIDImonitor, to observe Midi traffic.
I send MidiMessages to GMIDImonitor via method below.
I can't figure out the value of the time parameter.
When I set time = jackClient.getFrameTime() then the message does not arrive in GMIDImonitor.
When I set it to for example to 300 then one message is being sent eternally in a loop.
Any help? Thanks
public void processMidiMessage(ShortMessage shortMessage) {
System.out.println("processMidiMessage: " + shortMessage + ", on port: " + this.outputPort.getName());
try {
JackMidi.clearBuffer(this.outputPort);
} catch (JackException e) {
e.printStackTrace();
}
try {
int time = 300;
JackMidi.eventWrite(this.outputPort, time, shortMessage.getMessage(), shortMessage.getLength());
} catch (JackException e) {
e.printStackTrace();
}
}

Building a Fing like network scanning to our android application. It works right now but takes painstakingly long to process

we were wanting to build a functionality where if a user is connected to a WiFi network, we can display the details of other devices connected to the same WiFi. How do we go about it?
Right now, we are able to achieve it by:
pinging all the IP addresses on the current network(looping from 1-255) - this is the most time consuming step (code snippet below)
for the IP addresses that responded, fetching their MAC addresses and finally
fetching the manufacturer for the MAC addresses using an external API
we have success in this but the issue is that it takes way too long - around 4-5 minutes to do this. I have a feeling that someone can point us towards a better, faster solution.
There was a similar question(although it was about iOS) but didn't find any answer, hence posting this again. Please pardon if that was against the rules. Any help in this would be highly appreciated.
Here's the snippet of code which is taking too long to give the results back(step 1)
for (int i = 0; i < 255; i++) {
String host = "";
try {
String subnet = "192.168.2";
host = subnet + "." + i;
Process exec = Runtime.getRuntime().exec(String.format(CMD, host));
int i1 = exec.waitFor();
if (i1 == 0) {
InetAddress a = InetAddress.getByName(host);
Log.i("TAG", "run: " + a.getHostAddress());
} else {
throw new IOException("Unable to get ping from runtime");
}
} catch (IOException | InterruptedException e) {
try {
InetAddress a = InetAddress.getByName(host);
if (a.isReachable(200)) {
Log.i("TAG", "run: " + a.getHostAddress());
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}

Depth of the IBM MQ

Can you please help me, I am trying to find the ibm mq depth with PCFAgent over SSL channel.
Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
System.setProperty("javax.net.ssl.trustStore","abc-dev.jks");
System.setProperty("javax.net.ssl.trustStorePassword","abcdabcd");
System.setProperty("javax.net.ssl.keyStore", "abc-dev.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "abcdabcd");
MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_128_CBC_SHA";
int attrs[] = { 2016, 3 };
System.out.println("parameter creation");
PCFParameter parameters[] = { new MQCFST(2016, "*"), new MQCFIN(20, 1),
new MQCFIL(1002, attrs) };
String name = null;
Integer depth = null;
System.out.println("parameter creation end");
try {
PCFAgent agent;
if (args.length == 1) {
System.out.print("Connecting to local queue manager " + args[0]
+ "... ");
agent = new PCFAgent(args[0]);
} else {
System.out.print("Connecting to queue manager at " + args[0]
+ ":" + args[1] + " over channel " + args[2] + "... ");
agent = new PCFAgent(args[0], Integer.parseInt(args[1]),
args[2]);
}
System.out.println("Connected.");
System.out.print("Sending PCF request... ");
com.ibm.mq.MQMessage responses[] = agent.send(13, parameters);
System.out.println("Received reply.");
for (int i = 0; i < responses.length; i++) {
MQCFH cfh = new MQCFH(responses[i]);
if (cfh.reason == 0) {
for (int j = 0; j < cfh.parameterCount; j++) {
PCFParameter p = PCFParameter
.nextParameter(responses[i]);
switch (p.getParameter()) {
case 2016:
name = (String) p.getValue();
break;
case 3: // '\003'
depth = (Integer) p.getValue();
break;
}
}
System.out.println("Queue " + name + " curdepth " + depth);
} else {
System.out.println("PCF error:\n" + cfh);
for (int j = 0; j < cfh.parameterCount; j++)
System.out.println(PCFParameter
.nextParameter(responses[0]));
}
}
System.out.print("Disconnecting... ");
agent.disconnect();
System.out.println("Done.");
} catch (ArrayIndexOutOfBoundsException abe) {
System.out
.println("Usage: \n\tjava ListQueueDepth queue-manager\n\tjava ListQueueDepth host port channel");
} catch (NumberFormatException nfe) {
System.out.println("Invalid port: " + args[1]);
System.out
.println("Usage: \n\tjava ListQueueDepth queue-manager\n\tjava ListQueueDepth host port channel");
} catch (MQException mqe) {
System.err.println(mqe);
} catch (IOException ioe) {
System.err.println(ioe);
}
When I try run this program remotely, I am getting the following exception:
com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2035
EDIT to add additional clarifying details from the comments:
The MQ Admin found an error in the AMQERR01.LOG related to the SYSTEM.DEFAULT.MODEL.QUEUE at the same time the application receives the 2035.
The same program works for a non-SSL channel when I take off the security settings and ciphersuite.
Well, I don't see where you set a UserID in the code. Therefore, you are sending a blank UserID which if the channel's MCAUSER is blank means, it becomes 'mqm' UserID because of an old security hole.
For MQ v7.1 or higher, the default CHLAUTH rule will block the connection if you are on a 'SYSTEM' channel. Or for MQ v8.0 or higher, the channel may require a UserID and Password. You need to look at either the event queue or queue manager logs for the reason for the RC of 2035 (not authorized).
int attrs[] = { 2016, 3 };
PCFParameter parameters[] = { new MQCFST(2016, "*"),
new MQCFIN(20, 1),
new MQCFIL(1002, attrs) };
Also, what is with the hard-coded values? Did you decompile someone else's program?
Why aren't you using the IBM MQ supplied defines? Here's what it should look like:
int[] attrs = {
CMQC.MQCA_Q_NAME,
CMQC.MQIA_CURRENT_Q_DEPTH
};
PCFParameter[] parameters = {
new MQCFST (CMQC.MQCA_Q_NAME, "*"),
new MQCFIN (CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL),
new MQCFIL (CMQCFC.MQIACF_Q_ATTRS, attrs)
};
The PCFAgent class creates a dynamic queue for the response messages from the command server. By default the model queue used is SYSTEM.DEFAULT.MODEL.QUEUE. The user needs to have OAM permission +get +dsp on the model queue.
If the program works when connecting to a non-SSL channel then the connection must be resolving to a different userid that has the required OAM permission against the SYSTEM.DEFAULT.MODEL.QUEUE.
The error in the queue manager's AMQERR01.LOG would show the userid that is missing the permission(s) along with the the specific permission(s) missing.
Depending on what version of MQ client jar files you are using, if you do not specify a UserID the MQ client will either send blank UserID or the UserID the java process is running under. On the queue manager side if the MCAUSER attribute of the channel is blank, then it would inherit either mqm if a blank UserID was received or the UserID your process is running under. If the MCAUSER attribute of the channel is not blank that is the UserID that will be used for permission.

How to discover host in a WiFi network in Android (JAVA)

I'm developing an Android application, I have to discover each hosts in a WiFi network, I have implemented a function that "ping" all IP address between 1 and 255 of a certain network.
This solution it work perfectly, but there is a problem, the execution time.. Every time that i start my application I wait about 256 second, it too long, i can't wait this time.
This is my source code (i found this code on Stack, i modified the code to work in my condition):
public class ScanNetwork {
ArrayList < String > hosts;
int i;
boolean finish = false;
public ArrayList < String > ScanNetwork(String ipAddress) {
hosts = new ArrayList < String > ();
final String subnet = ipAddress.substring(0, ipAddress.lastIndexOf("."));
for (i = 0; i < 256; i++) {
String currentHost = subnet + "." + i;
Process p1 = null;
int returnVal = 0;
try {
p1 = Runtime.getRuntime().exec("ping -w 50 -c 1 " + currentHost);
returnVal = p1.waitFor();
} catch (IOException e) {
System.out.println("Log: " + e.toString());
} catch (InterruptedException e) {
System.out.println("Log: " + e.toString());
}
boolean reachable = (returnVal == 0);
if (reachable) {
if (!hosts.contains(currentHost)) {
hosts.add(currentHost);
System.out.println(currentHost);
}
}
}
return hosts;
}
}
This source code is perfect but the execution time is excessive, there are other way to obtain all the host in the network ?
How i can solve this problem ?
The problem I see is that you are doing all the pings sequentially - the loop is spending most of its time waiting for replies. Try starting up a few AsyncTasks, each of which has an assigned range of addresses to search, and let them work in parallel.
Note that for a typical 192.168.1.x network, ".0" (all 0 bits) ".255" (all 1 bits) will not correspond to a host and doesn't need checking.
Also don't forget that not everybody responds to a ping (this is more likely in a corporate network, less so at home)

Categories

Resources