Change room name fails - server api

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
aakture
Posts: 23
Joined: 05 Jun 2012, 18:46

Change room name fails - server api

Postby aakture » 22 Dec 2016, 21:11

I'm trying to change a room name right after the room is created, and it fails. I'm using 2.12.1 server api, this is all triggered when the extension is initialized. I've tried two of the createRoom methods (there is no User in my case, so I am passing null). Seems like it's trying to notify name change listeners along the way, and then an NPE... any help?

Code is:

Code: Select all

   
      CreateRoomSettings createRoomSettings = new CreateRoomSettings();
      String roomName = roomNameProvider.newRoomName(themeContext, segmentId);
      createRoomSettings.setName(roomName);
      createRoomSettings.setGame(true);
      createRoomSettings.setDynamic(false);
      createRoomSettings.setRoomSettings(EnumSet.of(SFSRoomSettings.ROOM_NAME_CHANGE));
      Room room = isfsApi.createRoom(zone, createRoomSettings, null, false, null, false, false);
      room.setFlag(SFSRoomSettings.ROOM_NAME_CHANGE, true);
      try {
         String newName = roomNameProvider.newRoomName(themeContext, segmentId, room.getId());
         log.info("change room name from: {} to: {}", roomName, newName);
         isfsApi.changeRoomName(null, room, newName);
      } catch (Exception ex) {
         log.error("changing room name failed", ex);
      }
      


Exception:


Code: Select all

13:08:56,355 INFO  [main] room.RoomFactory     - change room name from: Dream-11.2.32 to: Dream-11.2.31
13:08:56,355 ERROR [main] room.RoomFactory     - changing room name failed
java.lang.NullPointerException
   at com.smartfoxserver.bitswarm.core.BitSwarmEngine.writeToSocket(BitSwarmEngine.java:402)
   at com.smartfoxserver.bitswarm.core.BitSwarmEngine.write(BitSwarmEngine.java:396)
   at com.smartfoxserver.bitswarm.io.Response.write(Response.java:70)
   at com.smartfoxserver.v2.api.response.SFSResponseApi.notifyRoomNameChange(SFSResponseApi.java:685)
   at com.smartfoxserver.v2.api.SFSApi.changeRoomName(SFSApi.java:1761)
   at com.goldenrat.sfs.model.room.RoomFactory.createRoom(RoomFactory.java:109)
   at com.goldenrat.sfs.model.room.RoomManager.createIt(RoomManager.java:498)
   at com.goldenrat.sfs.model.room.RoomManager.createRoom(RoomManager.java:478)
   at com.goldenrat.sfs.model.room.RoomManager.lambda$null$84(RoomManager.java:412)
   at java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1691)
   at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
   at com.goldenrat.sfs.model.room.RoomManager.lambda$handleGameContextResponse$85(RoomManager.java:404)
   at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
   at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
   at com.goldenrat.sfs.model.room.RoomManager.handleGameContextResponse(RoomManager.java:402)
   at com.goldenrat.sfs.model.room.RoomManager.createRooms(RoomManager.java:394)
   at com.goldenrat.sfs.extensions.BaccaratExtension.init(BaccaratExtension.java:105)
   at com.smartfoxserver.v2.entities.managers.SFSExtensionManager.createExtension(SFSExtensionManager.java:303)
   at com.smartfoxserver.v2.entities.managers.SFSZoneManager.createZone(SFSZoneManager.java:425)
   at com.smartfoxserver.v2.entities.managers.SFSZoneManager.initializeZones(SFSZoneManager.java:239)
   at com.smartfoxserver.v2.SmartFoxServer.start(SmartFoxServer.java:292)
   at com.smartfoxserver.v2.Main.main(Main.java:27)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Change room name fails - server api

Postby Lapo » 23 Dec 2016, 10:22

Hi,
I suspect you're doing this before the server is ready to talk to the network, which causes the problem.

The init() method of an Extension runs prior to the socket engine being ready to communicate, so init() is a good place to setup your main objects / data structures but not for triggering operations that require sending network updates.

I am also a little confused as to why you're creating a Room with a certain name, only to change it in the next line. Can't you just determine what name you need to use prior to creating the Room?

If that's not possible then I'd recommend moving your room creation code in the SFSEvent.SERVER_READY handler.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
aakture
Posts: 23
Joined: 05 Jun 2012, 18:46

Re: Change room name fails - server api

Postby aakture » 27 Dec 2016, 18:38

Thanks, that helps, I suspect it will solve it. I wanted to rename the room because it's useful for me to have the room id in the room name itself for admin functions using a web api I built that requires roomId (kick user from room, shutdown room, etc). This way I can see what the roomId is as it will be part of the room name. Alternatively I guess I could have used room name in my web api (but I think calls my my api makes to the ISFSApi mostly require roomId).
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Change room name fails - server api

Postby Lapo » 28 Dec 2016, 09:17

I understand that, but it's not clear why you create a Room with name X and in the next line of code you change it's name to Y.

This is redundant. My suggestion was just to name the Room Y from the get go, instead of doing it in two steps. It will also reduce the amount of traffic generated, since renaming the Room will generate an update towards all clients.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
aakture
Posts: 23
Joined: 05 Jun 2012, 18:46

Re: Change room name fails - server api

Postby aakture » 29 Dec 2016, 02:10

well... I wanted to have the roomId in the room name itself. I don't see a way to create a room and supply a room id (so I could create it with the known id value). So I create the room, then immediately get the room id, and change the room name adding the room id as part of it's name. For example, Create a room called Fortune-1.2.3. Then I find the room id is 12, so I change it to Fortune-1.2.12.

The only use is when I see a room name in a log file, or in some of my web apis, I can easily get the room id to do some admin functions that require the room id. If renaming it like this is a bad practice, I can do without. Thanks again.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Change room name fails - server api

Postby Lapo » 29 Dec 2016, 10:28

Yeah, it wastes bandwidth. Which may or may not be an issue for you, so ultimately it's up to you to decide.

Since the Room id has no particular meaning you couldn't you use another ID? For example the ID of the owner of the Room, or a random number, or your own auto-increment ID.

Just throwing a couple ideas around :)

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
aakture
Posts: 23
Joined: 05 Jun 2012, 18:46

Re: Change room name fails - server api

Postby aakture » 29 Dec 2016, 19:22

One last thing... yes, I was using an incremental ID starting at 0, which coincidentally seems to be the same as the assigned room id, which would probably work fine, but I guess there is no guarantee it would always match the SFS assigned room id. Also, I'm creating these server side, so I guess bandwidth actually won't be a problem. And it only happens at startup, after that, room creation is infrequent for our app. Many thanks for your input, much appreciated.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Change room name fails - server api

Postby Lapo » 30 Dec 2016, 08:17

Does it need to match the Room ID? If so, why?
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
aakture
Posts: 23
Joined: 05 Jun 2012, 18:46

Re: Change room name fails - server api

Postby aakture » 30 Dec 2016, 17:40

Doesn't have too, but would be nice, because I sometimes see issues in our log files where I output the room name. And I have built numerous web api's for room admin functions that require room id as an argument. I would like to easily grab the room id from the name without having to look it up using the admin tool. I could have written my web api to take room name in hindsight (or output room id in log files). Not a big deal either way, but having the actual room id in the room name makes many things like this easier for me.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 53 guests