Request Handler not found??

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

Moderators: Lapo, Bax

claudiu.m
Posts: 23
Joined: 10 Sep 2013, 08:51
Location: Germany

Request Handler not found??

Postby claudiu.m » 09 Oct 2013, 12:47

Hello, I have a problem with my extension. I used the tutorial you posted on this page http://www.smartfoxserver.com/docs/1x/index.htm?http://www.smartfoxserver.com/docs/1x/docPages/sfsPro/recipes_RequestDispatching.htm and for my it's not working because I get this error:
Request handler not found
.

My extesion is this:

Code: Select all

public class ZufallsZahlenExtension extends SFSExtension {

   private static final ISFSApi ISFSApi = null;
   private Vector<ZufallsZahlenGenerieren> _game = new Vector<ZufallsZahlenGenerieren>();
   Map<String,String> invocationTable;
   
   @Override
   public void init() {
      // TODO Auto-generated method stub
      /**REQUEST HANDLER**/
      this.addRequestHandler(String.valueOf(Commands.GET_USER_DATA.ordinal()), GetUserData.class);
      
      /** IN GAME REQUSTS**/
      this.addRequestHandler(String.valueOf(Commands.GET_DICES.ordinal()), InGameRequest.class);
      this.addRequestHandler(String.valueOf(Commands.QUIT_GAME.ordinal()), InGameRequest.class);
      this.addRequestHandler(String.valueOf(Commands.GET_MYRAND_NR.ordinal()), MyRand.class);
      
      
      /** EVENT HANDLER**/
      addEventHandler(SFSEventType.USER_JOIN_ROOM, UserJoinedEH.class);
      invocationTable();
   }
   
   public void invocationTable()
   {
      /**INVOCATION TABLE**/
      invocationTable = new HashMap<String,String>();
      invocationTable.put(String.valueOf(Commands.GET_MYRAND_NR.ordinal()), "Myrand");
      invocationTable.put(String.valueOf(Commands.GET_DICES.ordinal()), "GetDices");
   }
   
   /**PUBLIC REQUEST HANDLER**/
   
   public void handleRequest(String cmd, ISFSObject obj, User user)
   {
      String handlerClassName = invocationTable.get(cmd);
      
      if(handlerClassName != null)
      {
         try
         {
            Class handlerClass = Class.forName(handlerClassName);
            IRequestHandler handler = (IRequestHandler) handlerClass.newInstance();
            handler.setExtension(this);
            handler.onRequest(obj, user);
         }
         catch(InstantiationException | IllegalAccessException issue)
         {
            System.out.println("There was a problem with the instantiation of the class " + handlerClassName);
            
         }
         
         catch (ClassNotFoundException issue)
         {
            // TODO Auto-generated catch block
            System.out.println("The class was not found!" + handlerClassName);
         }
      }
         
         
      else
      {
         throw new UnsupportedOperationException("Unknown request id: " + cmd);
      }
      
   }


this is my IRequestHandler

Code: Select all

package com.m2p.zuffalszahlen;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;

public interface IRequestHandler {
   
public void setExtension(SFSExtension ext);
public void onRequest(ISFSObject obj, User user);

   
}


   
   
   




and this is the my class

Code: Select all

package com.m2p.zuffalszahlen;

import java.util.ArrayList;
import java.util.List;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class GetDices implements IRequestHandler{

   SFSExtension sfs;
   @Override
   public void setExtension(SFSExtension ext) {
      // TODO Auto-generated method stub
      sfs = ext;
   }

   @Override
   public void onRequest(ISFSObject obj, User user) {
      // TODO Auto-generated method stub
      
      final List<Integer> myRandInt = new ArrayList<Integer>();
      final int[] myIntegers = {0,64,128,192,256,320,384,448,512,576,640,704,768,832,896,960,1024,1088,1152,1216};
      final List<Integer> myNumbers = new ArrayList<Integer>();
      
      for (int i = 0; i < myIntegers.length; i++)
      {
         myNumbers.add(myIntegers[i]);
         
      }
      
      ISFSObject nr = new SFSObject();
      nr.putIntArray("myNumbers", myNumbers);
      sfs.send(String.valueOf(Commands.GET_DICES.ordinal()), nr, user);
      
   }

}



I have a class where I put my commands like this:

Code: Select all

package com.m2p.zuffalszahlen;

enum Commands
{
   UNKNOWN,
   GET_USER_DATA,
   GET_DICES,
   GET_MYRAND_NR,
   
   //INGAME MESSAGES
   RUNDOM_NUMBER,
   QUIT_GAME,
   
}



Can you please help me?
Thank you in advance for your help!
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request Handler not found??

Postby Lapo » 09 Oct 2013, 14:34

Hi,
I don't understand several things:

1) why you need a IRequestHandler interface?
2) why your handler implements such interface
3) why your handler does not implement the request BaseClientRequestHandler class from com.smartfoxserver.v2.extensions package, which is essential

That looks very much like the cause of the issue.
You don't really need the first two steps.

Take a look at this video tutorial for a complete walkthrough: http://www.youtube.com/watch?v=nKGxhwJ0 ... 94B9D7C3E5

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
claudiu.m
Posts: 23
Joined: 10 Sep 2013, 08:51
Location: Germany

Re: Request Handler not found??

Postby claudiu.m » 09 Oct 2013, 14:41

Hi Lapo,
I found this : http://www.smartfoxserver.com/docs/1x/index.htm?http://www.smartfoxserver.com/docs/1x/docPages/sfsPro/recipes_RequestDispatching.htm and I found it very nice but it looks like I am not able to use it because of that error.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request Handler not found??

Postby Lapo » 09 Oct 2013, 15:03

Pay attention, that is the documentation for SmartFoxServer 1.x, but you are working with 2X as I understand.
The docs for 2X is located here:
http://docs2x.smartfoxserver.com/
Lapo

--

gotoAndPlay()

...addicted to flash games
claudiu.m
Posts: 23
Joined: 10 Sep 2013, 08:51
Location: Germany

Re: Request Handler not found??

Postby claudiu.m » 09 Oct 2013, 15:09

Oh I just did not saw that, i am sorry.
Is there a possibility to organize the code like in the example from SFSX1?
Thank you for taking your time to help me.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request Handler not found??

Postby Lapo » 09 Oct 2013, 16:18

In general I would recommend using the SFSExtension class, because it's more versatile and helps keeping the various handlers separated.
But you can extend BaseSFSExtension instead to use a SFS1-like model.

See here an overview:
http://docs2x.smartfoxserver.com/Advanc ... extensions

And the javadoc for reference:
docs2x.smartfoxserver.com/api-docs/javadoc/server/
Lapo

--

gotoAndPlay()

...addicted to flash games
claudiu.m
Posts: 23
Joined: 10 Sep 2013, 08:51
Location: Germany

Re: Request Handler not found??

Postby claudiu.m » 10 Oct 2013, 09:10

Hi Lapo,
I have a question about how to better organize my code. Do I have to create for every ClientRequest a new ClientRequestHandler Class or am I able to clasify them like that:

Code: Select all

/**REQUEST HANDLER**/
      this.addRequestHandler(String.valueOf(Commands.GET_USER_DATA.ordinal()), GetUserData.class);
      
      /** IN GAME REQUSTS**/
      this.addRequestHandler(String.valueOf(Commands.GET_DICES.ordinal()), InGameRequest.class);
      this.addRequestHandler(String.valueOf(Commands.QUIT_GAME.ordinal()), InGameRequest.class);
      this.addRequestHandler(String.valueOf(Commands.GET_MYRAND_NR.ordinal()), InGameRequest.class);
      //this.addRequestHandler(String.valueOf(Commands.GET_MYRAND_NR.ordinal()), MyRand.class);
      
      
      /** EVENT HANDLER**/
      addEventHandler(SFSEventType.USER_JOIN_ROOM, UserJoinedEH.class);

So for example I have 3 RequestHandlers that use one BaseClientRequestHandler class "InGameRequest".
If this is possible how do I structure the code in "InGameRequest" class .
If there is a better possibility to do this please let me now. I am interested in having a good structue of my extension.

Thank you for you help
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Request Handler not found??

Postby Lapo » 11 Oct 2013, 11:08

There seem to be some confusion. Let me try to clarify a couple of basic ideas:

--> For starters you need to decide which "style" of Extension you want to create: SFS1.x or 2X

» SmartFoxServer 1.x style Extensions
In this case your class will extend BaseSFSExtension and you will use a "manual" system for handling the various requests like this:

Code: Select all

public class MyExtension extends BaseSFSExtension
{
   @Override
   public void init()
   {
      
   }
   
   @Override
   pubic void handleClientRequest(String cmdName, User sender, ISFSObject params) throws SFSException
   {
      if (cmdName.equals("move"))
      {
         // handle movement
      }
      
      else if (cmdName.equals("shoot"))
      {
         // handle shooting
      }
      
      // etc...
   }
}



» SmartFoxServer 2X style Extensions
In this case your Extension class will inherit from SFSExtension and it will use separate classes for each client request:

Code: Select all

public class My2XExtension extends SFSExtension
{
   @Override
   public void init()
   {
      addRequestHandler("move", MoveHandler.class);
      addRequestHandler("shoot", ShootHandler.class);
   }
   
   @Override
   public void destroy()
   {
       super.destroy();
   }
}


This is a more organized way of managing the code because handlers are separated from the main extension and suggest that the same should be done for the model.
The main Extension remains the primary "point of reference" for all Handlers. For example you can add methods to it for accessing the model of the game, helper classes, singletons and any other resource that it may be required.

Also ClientRequest handlers can become MultiHandlers to further organize your commands in a logical way, using the "dot syntax".
For more details see here:
http://docs2x.smartfoxserver.com/Advanc ... extensions
under the Advanced Extension feature section.

» New since SFS2X 2.7.0
If you don't like to pass a Class object as your request handler and would prefer to create your own instances you can now do that as well. Since version 2.7 we have added overloaded methods that accepts instance objects as well. This can be helpful when you want to mange handler instantiation manually.

Hopes this clarifies the scenario.

If it doesn't full answer your question let me know.
Lapo

--

gotoAndPlay()

...addicted to flash games
claudiu.m
Posts: 23
Joined: 10 Sep 2013, 08:51
Location: Germany

Re: Request Handler not found??

Postby claudiu.m » 14 Oct 2013, 08:18

Thank you very much Lapo, this clarified my question.
I will let you know if I have further questions.
Thank you again !

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 95 guests