Page 1 of 1

Bug in Java setVariable / getVariable

Posted: 19 Oct 2006, 13:57
by milosh6
Hello,

this the bug I might have discovered in your code.

When you set the variable for user or room on the server (in java extension) in this way:

Code: Select all

room.setVariable("IsPrivate", RoomVariable.TYPE_BOOLEAN, "false", true, false, null, false) 
and then try to read it in the flash client this way:

Code: Select all

room.getVariable("IsPrivate")
it behaves like this: If you have set the variable on the server to either true or false, flash client always gets true. If you have set the variable on the server to any other string, such as "" or anything else, the flash client gets false.

I hope I am clear. Currenly we are using the workaround that for setting boolean variables we use "" instead of false. Let me know if you discover this as a bug and when can we expect the fix.

Thanks.

Posted: 21 Oct 2006, 11:05
by Lapo
Hi,
the problem it's probably generated by an ambiguity of the AS 2.0 language in converting numbers or strings to Boolean.

The APIs use the values 0 and 1 to serialize the values "false" and "true" respectively. When they get to the client they are converted back to their original types.
There's however a small catch when casting those values with the Boolean() cast operation:

Code: Select all

Boolean(1) = true
Boolean(0) = false
but...

Code: Select all

Boolean("1") = true
Boolean("0") = true
I think the problem is in the deserialization. If the deserialized value is detected as Number it will be correctly cast back to Boolean, while if it's detected as String it won't.

You should expect this bug to be fixed in the next release which is due by the end of the month.

If we find a quicker solution before the release we'll post it here.

hope it helps

Posted: 11 Nov 2006, 11:21
by Lapo
milosh6,
I did a few tests to reproduce the problem using the setRoomVariables() method and everything seems to work correctly.

I've noticed that you mentioned the setRoomVariable() method (notice the missing "s"). Actually that's not the right method to use when settin room variables on the server side (it is also deprecated in the docs).
Try the setRoomVariables() instead.

cheers

Posted: 13 Nov 2006, 08:00
by milosh6
Thanks Lapo, we'll definitely try this out. But I didn’t see in the JAVA Docs that the method we are using would be deprecated.

Posted: 14 Nov 2006, 09:02
by Lapo
one more question, you didn't mention which version of the API you are using and I assumed you were talking about the AS 2.0 APIs. Is this correct?

Posted: 14 Nov 2006, 09:07
by milosh6
Correct.

Posted: 14 Nov 2006, 09:14
by Lapo
ok, great. That's what I tested

cheers

Posted: 14 Nov 2006, 09:17
by milosh6
We have been actually receiving exception when trying to use the method you recommended.

Exception:

Code: Select all

java.lang.Boolean
java.lang.ClassCastException: java.lang.Boolean
	at it.gotoandplay.smartfoxserver.extensions.ExtensionHelper.setUserVariables(ExtensionHelper.java:571)

Posted: 14 Nov 2006, 10:55
by Lapo
Can you provide the code that generates the exception?
Also the complete stack trace would be helpful

thanks

Posted: 14 Nov 2006, 14:55
by milosh6

Code: Select all

HashMap hm = new HashMap(); 
                hm.put("isGuest", isGuest); 
                GeeseeExtension.getHelper().setUserVariables(newUser, hm, false);
Stack:

Code: Select all

java.lang.Boolean
java.lang.ClassCastException: java.lang.Boolean
	at it.gotoandplay.smartfoxserver.extensions.ExtensionHelper.setUserVariables(ExtensionHelper.java:571)
	at geesee.handler.LoginRequest.execute(LoginRequest.java:176)
	at geesee.handler.Handler.run(Handler.java:54)
	at geesee.GeeseeExtension.handleInternalEvent(GeeseeExtension.java:137)
	at it.gotoandplay.smartfoxserver.controllers.MessageHandler.dispatchEvent(MessageHandler.java:141)
	at it.gotoandplay.smartfoxserver.controllers.SystemHandler.handleLoginRequest(SystemHandler.java:374)
	at it.gotoandplay.smartfoxserver.controllers.SystemHandler.processEvent(SystemHandler.java:164)
	at it.gotoandplay.smartfoxserver.controllers.SystemHandler.run(SystemHandler.java:112)
	at java.lang.Thread.run(Unknown Source)

Posted: 14 Nov 2006, 16:56
by Lapo
In this line:

Code: Select all

hm.put("isGuest", isGuest);
what type is the isGuest variable?

Posted: 14 Nov 2006, 18:38
by milosh6
It is a boolean variable. But similar exception occured when we tried it out with the String variable.

Posted: 15 Nov 2006, 07:58
by Lapo
yep, I see.
The expected Map should be a HashMap<String, UserVariable>
That's why you get the ClassCastException.

cheers