Hey Patrick:
So, I am trying to get some code working where I have a given phidget getting opened twice in the same thread of execution. Here is my question.
Here is the code I am working with. I have removed the attach methods from the listing for ease of reading, but they are there in the running code:
Code:
package dcmotor;
import com.phidgets.MotorControlPhidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.CurrentChangeEvent;
import com.phidgets.event.CurrentChangeListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;
import com.phidgets.event.ErrorEvent;
import com.phidgets.event.ErrorListener;
import com.phidgets.event.InputChangeEvent;
import com.phidgets.event.InputChangeListener;
import com.phidgets.event.MotorVelocityChangeEvent;
import com.phidgets.event.MotorVelocityChangeListener;
import com.phidgets.event.ServerConnectEvent;
import com.phidgets.event.ServerConnectListener;
import com.phidgets.event.ServerDisconnectEvent;
import com.phidgets.event.ServerDisconnectListener;
/**
*
* @author mlham
*/
public class ControllerTester {
public static void main(String[] args) throws PhidgetException {
try{
MotorControlPhidget mcp1 = new MotorControlPhidget();
... events attached
MotorControlPhidget mcp2 = new MotorControlPhidget();
... events attached
String ipAddr = args[0];
int port = Integer.parseInt(args[1]);
mcp1.open(41718, ipAddr,port);
// mcp2.open(41718, ipAddr, port);
Thread.sleep(10000);
System.out.println("Here is the answer to the attached mcp1?" + mcp1.isAttached());
// System.out.println("Here is the answer to the attached mcp2?" + mcp2.isAttached());
// mcp2.waitForAttachment(2000);
mcp1.setAcceleration(0, 50);
System.out.println("Here is the acceleration: " + mcp1.getAcceleration(0));
} catch (InterruptedException ie){
ie.printStackTrace();
}
}
}
Here is what I get when I run this with the current commented out code blocks.
Code:
MotorControlPhidget v-1 #-1
PhidgetMotorControl v110 #41718 (attached)
Here is the answer to the attached mcp1?true
Here is the acceleration: 50.0
Now, if I run this with say this command:
Code:
java -cp dist/DCMotor.jar:dist/lib/phidget21.jar dcmotor.ControllerTester 192.168.123.134 5001 > log2
I am able to run it as many times as I can and not get duplicate message errors.
However, if I run the code with comments not in:
Code:
String ipAddr = args[0];
int port = Integer.parseInt(args[1]);
mcp1.open(41718, ipAddr,port);
mcp2.open(41718, ipAddr, port);
Thread.sleep(1000); //Changed to reduce listing :)
System.out.println("Here is the answer to the attached mcp1?" + mcp1.isAttached());
System.out.println("Here is the answer to the attached mcp2?" + mcp2.isAttached());
mcp1.setAcceleration(0, 50);
I get this for output:
Code:
MotorControlPhidget v-1 #-1
PhidgetMotorControl v110 #41718 (attached)
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Here is the answer to the attached mcp1?true
Here is the answer to the attached mcp2?false
Here is the acceleration: 50.0
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
So, this doesn't make sense to me. Why can more than one thread on the same box have access to the phidgets, but if I run them from the same thread, I get a duplicate error? Why does the code/system care?
Just wondering, cause it makes writing code much harder if we have to first come up with a singleton for each of the phidgets we want to use, then create rapper objects that hold the actions we want to use, then pass the singletons in to the objects so they can use it to make the calls.
Is this a bug in the java library? Meaning that it is using/holding state and handing it back even though it being called from the remote open that is re-entrant?
Thanks in advance.
Mason