Bug in Java setVariable / getVariable

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Bug in Java setVariable / getVariable

Post 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.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Post 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.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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?
Lapo
--
gotoAndPlay()
...addicted to flash games
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Post by milosh6 »

Correct.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

ok, great. That's what I tested

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Post 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)
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Can you provide the code that generates the exception?
Also the complete stack trace would be helpful

thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Post 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)
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

In this line:

Code: Select all

hm.put("isGuest", isGuest);
what type is the isGuest variable?
Lapo
--
gotoAndPlay()
...addicted to flash games
milosh6
Posts: 27
Joined: 06 Sep 2006, 14:09

Post by milosh6 »

It is a boolean variable. But similar exception occured when we tried it out with the String variable.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

yep, I see.
The expected Map should be a HashMap<String, UserVariable>
That's why you get the ClassCastException.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply