Closed LotekB closed 4 years ago
Why do you want to send your struct wrapped in a variant? Structs should extend the Struct.class and can therefore be send without wrapping it in a variant.
A struct should be looking like the samples in test sources (e.g. org.freedesktop.dbus.test.helper.structs.SampleStruct). The position-annotation is important and is zero based!
If you wrap your object in a variant, the result will look like 'v(byyyyyyyyyyyyssssssssuiqyiiiis)'. So you have to expect a Variant on the other side as well. You then have to get the value from the variant and so on.
Hello David,
The working client (written in C++) sends and it expects Variant. I can't change it. The Variant sent from Java (server) to client is OK. Client accepts it without any problems.
My question is how to unpack the received Variant
Thanks for your help, regards, Bogdan
My Structure looks like: static public class TmphFilterStruct extends Struct { // "byyyyyyyyyyyyssssssssuiqyiiiis" @Position(0) public boolean m_enabled; @Position(1) public byte m_mnemoState; @Position(2) public byte m_spidState; @Position(3) public byte m_apidListState; @Position(4) public byte m_pidListState; @Position(5) public byte m_pCatListState; @Position(6) public byte m_typeListState; @Position(7) public byte m_stypeListState; @Position(8) public byte m_pi1ListState; @Position(9) public byte m_descrState; @Position(10) public byte m_vcidState; @Position(11) public byte m_dutypeState; @Position(12) public byte m_gsidState; @Position(13) public String m_mnemo; @Position(14) public String m_apidList; @Position(15) public String m_pidList; @Position(16) public String m_pCatList; @Position(17) public String m_typeList; @Position(18) public String m_stypeList; @Position(19) public String m_pi1List; @Position(20) public String m_descr; @Position(21) public UInt32 m_spid; @Position(22) public int m_vcid; @Position(23) public UInt16 m_gsid; @Position(24) public byte m_dutype; @Position(25) public int m_ds0; @Position(26) public int m_ds1; @Position(27) public int m_ds2; @Position(28) public int m_ds3; @Position(29) public String m_spidList;
TmphFilterStruct(TmphFilter tmphFilter) {
m_enabled = (tmphFilter.m_enabled.toByte() > 0);
m_mnemoState = tmphFilter.m_mnemoState.toByte();
m_spidState = tmphFilter.m_spidState.toByte();
m_apidListState = tmphFilter.m_apidListState.toByte();
m_pidListState = tmphFilter.m_pidListState.toByte();
m_pCatListState = tmphFilter.m_pCatListState.toByte();
m_typeListState = tmphFilter.m_typeListState.toByte();
m_stypeListState = tmphFilter.m_stypeListState.toByte();
m_pi1ListState = tmphFilter.m_pi1ListState.toByte();
m_descrState = tmphFilter.m_descrState.toByte();
m_vcidState = tmphFilter.m_vcidState.toByte();
m_dutypeState = tmphFilter.m_dutypeState.toByte();
m_gsidState = tmphFilter.m_gsidState.toByte();
m_mnemo = tmphFilter.m_mnemo;
m_apidList = tmphFilter.m_apidList;
m_pidList = tmphFilter.m_pidList;
m_pCatList = tmphFilter.m_pCatList;
m_typeList = tmphFilter.m_typeList;
m_stypeList = tmphFilter.m_stypeList;
m_pi1List = tmphFilter.m_pi1List;
m_descr = tmphFilter.m_descr;
m_spid = tmphFilter.m_spid;
m_vcid = tmphFilter.m_vcid;
m_gsid = tmphFilter.m_gsid;
m_dutype = tmphFilter.m_dutype;
m_ds0 = tmphFilter.m_ds0;
m_ds1 = tmphFilter.m_ds1;
m_ds2 = tmphFilter.m_ds2;
m_ds3 = tmphFilter.m_ds3;
m_spidList = tmphFilter.m_spidList;
}
}
On 25.10.2019 14:18, David M. wrote:
Why do you want to send your struct wrapped in a variant? Structs should extend the Struct.class and can therefore be send without wrapping it in a variant.
A struct should be looking like the samples in test sources (e.g. org.freedesktop.dbus.test.helper.structs.SampleStruct https://github.com/hypfvieh/dbus-java/blob/master/dbus-java/src/test/java/org/freedesktop/dbus/test/helper/structs/SampleStruct.java). The position-annotation is important and is zero based!
If you wrap your object in a variant, the result will look like 'v(byyyyyyyyyyyyssssssssuiqyiiiis)'. So you have to expect a Variant on the other side as well. You then have to get the value from the variant and so on.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hypfvieh/dbus-java/issues/77?email_source=notifications&email_token=AM7OHYHMA2ZYXEF3VNVLTVTQQLPZLA5CNFSM4JFCPQ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECIFSLY#issuecomment-546330927, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7OHYESLGL4ZSXXM24S5EDQQLPZLANCNFSM4JFCPQ7Q.
How does your code for receiving look like?
Usually you have an java interface with some methods in it. Then you get the remote object from DBus by using somthing like:
MyRemoteInterface foo = (MyRemoteInterface) dbusSession.getRemoteObject("what.ever", "/where/ever");
Variant<MyStructData> variantStruct = foo.getMyStructDataWrappedInVariant();
MyStructData struct = variantStruct.getValue();
// continue to work with struct
The interface would look like:
@DBusInterfaceName("what.ever")
public interface MyRemoteInterface extends DBusInterface {
Variant<MyStructData> getMyStructDataWrappedInVariant();
}
So what are you doing?
The generated interface is:
@DBusInterfaceName(value = "xx.yyy.zzz.MyIf")
public interface MyIf extends DBusInterface
{
...
public void requestSetupFilter(Variant
the implementation is:
public class MyImpl implements MyIf {
...
public void requestSetupFilter( Variant
The requestSetupFilter()is the non-blocking (one-way) call from C++ client.
The Variant
The "first attempt" shown above throws the exception: "[LJava.lang.Object ; cannot be cast to x.y.x.TmphDataTypes$TmphFieltrStruct"
On the debug screen dump below you can see, that the tmphFilterVariant is ofDBusStructType, its value is an Object. So tmphFilterVariant.getValue() results in Object, that cannot be cast to the TmphFilterStruct(why?). The structure of the object seems to be OK (see debug dump)
Debug dump
After the first attempt shown above I've started to use the code from the primitizeRecurse() method from the CrossTestClient.java I've tried:
Object o = tmphFilterVariant.getValue();
Type t = tmphFilterVariant.getType();
if( t instanceof ParameterizedType ) {
@SuppressWarnings("unchecked")
Class
if (Struct.class.isAssignableFrom(c)) {
Struct s = (Struct)o; <<<<<<< << InvocationTargetException
Object[] os = s.getParameters();
Type[] ts = ((ParameterizedType) t).getActualTypeArguments();
<<<<<<< Works if two above statements are commented } }
On 25.10.2019 15:29, David M. wrote:
How does your code for receiving look like?
Usually you have an java interface with some methods in it. Then you get the remote object from DBus by using somthing like:
MyRemoteInterface foo= (MyRemoteInterface) dbusSession.getRemoteObject("what.ever","/where/ever");
Variant
variantStruct= foo.getMyStructDataWrappedInVariant(); MyStructData struct= variantStruct.getValue(); // continue to work with struct
The interface would look like:
@DBusInterfaceName("what.ever") public interface MyRemoteInterface extends DBusInterface { Variant
getMyStructDataWrappedInVariant(); } So what are you doing?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hypfvieh/dbus-java/issues/77?email_source=notifications&email_token=AM7OHYH2XVG4GN2UA73Y7VTQQLYDRA5CNFSM4JFCPQ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECILIHA#issuecomment-546354204, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7OHYB5RHFVNSPLQ6K2KFLQQLYDRANCNFSM4JFCPQ7Q.
Now I see what's going on.
Due to the fact that the struct is wrapped in a Variant object, the information on which class to use for this struct is getting lost.
When de-serializing the DBus message the resulting Variant will contain a struct of dummy type DBusStructType (you can see that when calling variant.getType() or see your ClassCastException). When reading variant.getValue() you will receive all fields of the struct in order as Object[] instead of the struct class itself.
There is no way to 'teach' the library to do this automatically as the signature of a struct is not unique. There may be multiple structs with the same signature but resulting in different classes.
So to get a Struct out of a Variant, you have to know which kind of Struct it is and re-construct it manually.
To provide some more help, I added the class StructHelper to dbus-java. This should be able to convert a Variant to a given Struct subclass, if Variant is compatible.
Please check my sandbox project to see how it works.
Hello David,
Based on your TwoPartServer test I was able to reproduce the described problem. Please find the source code attached. The Problem is 100% reproducible. Currently I am using dbus-java-3.0.2 library.
Once more thank you for your help, with best regards,
Bogdan
On 25.10.2019 15:29, David M. wrote:
How does your code for receiving look like?
Usually you have an java interface with some methods in it. Then you get the remote object from DBus by using somthing like:
MyRemoteInterface foo= (MyRemoteInterface) dbusSession.getRemoteObject("what.ever","/where/ever");
Variant
variantStruct= foo.getMyStructDataWrappedInVariant(); MyStructData struct= variantStruct.getValue(); // continue to work with struct
The interface would look like:
@DBusInterfaceName("what.ever") public interface MyRemoteInterface extends DBusInterface { Variant
getMyStructDataWrappedInVariant(); } So what are you doing?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hypfvieh/dbus-java/issues/77?email_source=notifications&email_token=AM7OHYH2XVG4GN2UA73Y7VTQQLYDRA5CNFSM4JFCPQ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECILIHA#issuecomment-546354204, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7OHYB5RHFVNSPLQ6K2KFLQQLYDRANCNFSM4JFCPQ7Q.
File attachments are not supported on github when answer tickets by mail, so I cannot see whatever you wanted to share.
Did you try my solution I posted before?
Hello David,
Thank you very much It works like a dream. What I've sent you was just the example illustrating the problem. I haven't notice (and have not expected) your immediate response with ready solution :)
I wonder if the
if (structVariant.getType() instanceof DBusStructType) { if (structVariant.getValue() instanceof Object[]) {
Class<?>[] argTypes = Arrays.stream((Object[]) tmphFilterVariant.getValue()).map(a -> a.getClass())
.toArray(new IntFunction<Class<?>[]>() {
@Override
public Class<?>[] apply(int size) {
return new Class<?>[size];
}
});
part couldn't be moved to the StructHelper - according exception can be thrown if the conditions are not fulfilled.
If it's possible I'll close the ticket, if not, please do this for me.
regards,
Bogdan
On 10/26/2019 10:03 AM, David M. wrote:
File attachments are not supported on github when answer tickets by mail, so I cannot see whatever you wanted to share.
Did you try my solution I posted before?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hypfvieh/dbus-java/issues/77?email_source=notifications&email_token=AM7OHYCYOKGCUKRYOFQ7ST3QQP2WXA5CNFSM4JFCPQ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECKCSUQ#issuecomment-546580818, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM7OHYCPAPPVRVYQM3JA5RLQQP2WXANCNFSM4JFCPQ7Q.
--
Bogdan Lotko
bogdan@lotko.at Phone: +43 1 2852458 Mobile: +43 676 6615012
Your quoted part is in deed duplicated code in the sample. I've removed that part. The conversion is already done in StructHelper.
As this solution is working for you, I'll close the ticket.
Hello, I have a problem with reception of the Variant sent as signal argument. The signature of MyStruct is "(byyyyyyyyyyyyssssssssuiqyiiiis)".
Creation and sending of this Variant works without any problems: Variant v = new Variant( new MyStruct() );
or
Variant v = new Variant<>( new MyStruct() );
Reception: The server requestSetMyStruct( Variant msVariant ) method shall receive, and unpack the Variant.
The type of the msVariant is DBusStructType so probably the signature of the method shall be:
requestSetMyStruct( Variant msVariant )
Anyway I am not able to convert the msVariant to MyStruct.
following the primitizeRecurse() method from the CrossTestClient.java I've tried:
public MyStruct requestSetMyStruct( VariantmsVariant ) {
Object o = msVariant .getValue();
Type t = msVariant .getType();
if( t instanceof ParameterizedType ) { Class
if (Struct.class.isAssignableFrom(c)) { Object[] os = ((Struct) o).getParameters(); <<<<<<<<<< throws the InvocationTargetException Exception here Type[] ts = ((ParameterizedType) t).getActualTypeArguments(); } } ... return ms; }
Thank you for your help, with best regards, Bogdan