Archived

This forum has been archived. Please start a new discussion on GitHub.

Newbie problem, receiving Objects without information

Hi, I'm new to ICE, I was trying the value example from Ice\Value, so I decided to modify it writing two simple classes, the Room class and the User class (The final goal of this is to develop some sort of chat with "rooms"), well the problem I have is that whenever I receive and object (A vector of rooms) it comes with no data only the size is correct, here is some of the code:

...
...
ServidorPrx initial = ServidorPrxHelper.checkedCast(base);
if(initial == null)
{
System.err.println("invalid object reference");
return 1;
}

Ice.ObjectFactory factory = new ObjectFactory();
communicator().addObjectFactory(factory, ":: Demo::Sala");
communicator().addObjectFactory(factory, ":: Demo::Jugador");

initial.crearSala("first");
initial.crearSala("second");
List salas = initial.listar();

Iterator it= salas.iterator();

while(it.hasNext()) {
Sala nueva = (Sala)it.next();

System.out.println(nueva.nombreSala+ " - "+nueva.nPregunta);
}

What this displays is only
- -
- -

and in the initial class (server side) If I print the Vector object before sending it, it is correct:

public List listar(Current __current) {
System.out.println("Regresando salas.."+salas.toString());
Iterator it= salas.iterator();

while(it.hasNext()) {
Sala nueva = (Sala)it.next();
System.out.println(nueva.nombreSala+ " - "+nueva.nPregunta);

}

return salas;
}

I hope you can help me here, I didn't paste more code to keep this simple because maybe I'm doing something wrong there, thank you!

(sorry about names, are in spanish, simply what listar() do is just send the Vector of rooms "salas" to the client)

Comments

  • Before we can help you, can you please set your signature as described in this post?

    Also, it would be good to see the Slice definition of your classes and operations.

    Cheers,

    Michi.
  • Hi, sorry about the signature, I forgot it, I hope this one is correcto now. Well about the slice here it is:
    #ifndef SIMPLE_ICE
    #define SIMPLE_ICE
    
    module Demo
    {
        
        interface Puntaje
        {    	
        	int incrementaPuntaje();
        	int decrementaPuntaje();
        	int resetPuntaje();
        	int setEstado(bool e);
        	bool dameEstado();
        	int getPuntaje();
        	
        };
        
        class Jugador implements Puntaje
        {
        	
        	string nombre;
        	bool estado;
        	int puntaje;
        	
        	
        };
    
       sequence<string> pregunta;
       ["java:type:java.util.Vector"] sequence<Jugador> jugadores;
       
        interface ReguladorS
        {
        	
    		int agregaJugador(Jugador actual);
    		pregunta getPregunta();
    		bool listos();
    		int incrementaPregunta();
    		jugadores getJugadores();
    		string getNombre();
    		
        };    
    
        
        class Sala implements ReguladorS
        {
        	pregunta preguntaActual;
        	jugadores listaJugadores;
        	int nPregunta;
        	string nombreSala;
        	
        };
        
        ["java:type:java.util.Vector"] sequence<Sala> Salas;
        class Servidor 
        {
        	int crearSala(string nombre);
        	int unirse(string nombre);
        	void getJugador(out Jugador impl, out Jugador* proxy);
        	void shutdown();
        	Salas listar();
        	
        };
    
    };
    
    #endif
    

    I think the problem is in :

    ["java:type:java.util.Vector"] sequence<Sala> Salas;

    I have tried

    ["java:type:java.util.Vector"] sequence<Sala*> Salas; and ["java:type:java.util.Vector<Sala>"] sequence<Sala> Salas;

    but both yield the same problem.

    Jugador constructor:
    	public Jugador (String nombre) {
    		puntaje = 0;
    		estado = false;
    		this.nombre = nombre;
    	}
    </code>
    Sala:
    
    
    public class Sala extends Demo.Sala implements Serializable {
    	
     	String[] preguntaActual = new String[5];
    	Vector listaJugadores = new Vector();
    	int nPregunta = 0;
    	String nombreSala;
    
    	 
    	public Sala() {
    		nombreSala = "";
    	}
    	
    	
    	public Sala(String nombre) {
    		nombreSala = nombre;
    	}
    
    .....
    ...
    

    and finally, ServidorI that implements the classes :
    	public int crearSala(String nombre, Ice.Current __current) {
    		System.out.println("Agregando salas");
    		
    		Sala nuevax = new Sala(nombre);
    		nuevax.agregaJugador(new Jugador("mav"));
    		
    		salas.add(nuevax);
    		
    		return 0;
    	}
    

    I will post them fully If you need, but I am sure I am doing something wrong there, server is the same as the Value example in Ice/value, and client same, it only calls ServidorI classes with an initial object.
  • benoit
    benoit Rennes, France
    Hi,

    Your code isn't thread safe. There could be multiple threads modifying concurrently the "salas" vector. You should synchronize the crearSala() and listar() methods.

    However, this is probably not the cause of your problem. I'm afraid I can't see anything obviously wrong with the code you posted. Can you try to simplify as much as possible your client and server and post a small self-compilable test case that we could use to reproduce the problem?

    Thanks,

    Benoit.
  • Simplified

    Hi, thank you for your quick response, well at first I thought that two, so I tried that simple code with only one client, and still the same, here, I tried to simplify the Sala, Jugador and ServidorI classes, I also post the client and server:

    Server:
    public class Server extends Ice.Application
    {
        public int
        run(String[] args)
        {
            Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Value");
            Ice.Object object = new ServidorI(adapter);
            
            adapter.add(object, communicator().stringToIdentity("initial"));
            adapter.activate();
            communicator().waitForShutdown();
            return 0;
        }
    
        public static void
        main(String[] args)
        {
            Server app = new Server();
            int status = app.main("Server", args, "config.server");
            System.exit(status);
        }
    }
    

    Client:
    public class Client extends Ice.Application {
        
    	public int run(String[] args)
        {
    		try {
    			Ice.Properties properties = communicator().getProperties();
    	        final String refProperty = "Value.Initial";
    	        String ref = properties.getProperty(refProperty);
    	        if(ref.length() == 0)
    	        {
    	            System.err.println("property `" + refProperty + "' not set");
    	            return 1;
    	        }
    	
    	        Ice.ObjectPrx base = communicator().stringToProxy(ref);
    	        ServidorPrx initial = ServidorPrxHelper.checkedCast(base);
    	        if(initial == null)
    	        {
    	            System.err.println("invalid object reference");
    	            return 1;
    	        }
    	        
    	        
    	        Ice.ObjectFactory factory = new ObjectFactory();
    	        communicator().addObjectFactory(factory, "::Demo::Sala");
    	        communicator().addObjectFactory(factory, "::Demo::Jugador");
    	        
    	        initial.crearSala("some");
    	        initial.crearSala("some2");
    	        initial.crearSala("some3");
    	        initial.crearSala("some4");
    	        Msg msg = initial.listar();
    	        Vector salas = (Vector)msg.salas;
    	      
    	       Iterator it= salas.iterator();
    	        
    	        while(it.hasNext()) {
    	        	Sala nueva = (Sala)it.next();
    	        	
    	        	System.out.println(nueva.nombreSala+ " - "+nueva.nPregunta+"- "+nueva.ice_id());
    	        	System.out.println(nueva.getNombre());
    	        }
    	        
    	    
    	        initial.shutdown();
            
            } catch(Exception e) {
            	e.printStackTrace();
            }
    

    ServidorI:
    class ServidorI extends Servidor implements Serializable {
    	
        /**
    	 * 
    	 */
    	public static final long serialVersionUID = 1L;
    	public Sala nueva = new Sala();
    	public Jugador nuevo = new Jugador();
            public JugadorPrx nuevoProx;
            public Vector<Sala> salas = new Vector<Sala>();
       
    	ServidorI(Ice.ObjectAdapter adapter)
        {
    		nuevoProx = JugadorPrxHelper.uncheckedCast(adapter.addWithUUID(nuevo));
    		
            adapter.addWithUUID(nuevo);
            adapter.addWithUUID(nueva);
        }
    	
    	public int crearSala(String nombre, Ice.Current __current) {
    		System.out.println("Agregando salas");
    		
    		Sala nuevax = new Sala(nombre);
    		nuevax.agregaJugador(new Jugador("mav"));
    		
    		salas.add(nuevax);
    		
    
    		
    		return 0;
    	}
    	
    
        public void
        shutdown(Ice.Current current)
        {
        	current.adapter.getCommunicator().shutdown();
        }
    
    
    	public Msg listar(Current __current) {
    		System.out.println("Regresando salas.."+salas.toString());
    		
    		 Msg msg = new Msg();
             
             
    		Iterator it= salas.iterator();
            
            while(it.hasNext()) {
            	Sala nueva = (Sala)it.next();
            	System.out.println(nueva.nombreSala+ " - "+nueva.nPregunta);
            	
            }
    		 
    		 
            msg.salas = salas;
            
    		return msg;
    	}
    
    }
    

    Sala:
    public class Sala extends Demo.Sala implements Serializable {
    	
     	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	String[] preguntaActual = new String[5];
    	Vector<Jugador> listaJugadores = new Vector<Jugador>();
    	
    	int nPregunta = 0;
    	String nombreSala;
    
    	 
    	public Sala() {
    		nombreSala = "";
    	}
    	
    	
    	public Sala(String nombre) {
    		nombreSala = nombre;
    	}
    
    }
    
    public class Jugador extends Demo.Jugador implements Serializable {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	String nombre;
    	boolean estado;
    	int puntaje;
    	
    	public Jugador () {
    		
    	}
    	
    	public Jugador (String nombre) {
    		puntaje = 0;
    		estado = false;
    		this.nombre = nombre;
    	}
    	
    }
    

    Slice:
    #ifndef SIMPLE_ICE
    #define SIMPLE_ICE
    
    module Demo
    {
        
     
        
        class Jugador implements Puntaje
        {
        	
        	string nombre;
        	bool estado;
        	int puntaje;
        	
        	
        };
    
       sequence<string> pregunta;
       ["java:type:java.util.Vector"] sequence<Jugador> jugadores;
     
        class Sala implements ReguladorS
        {
        	pregunta preguntaActual;
        	jugadores listaJugadores;
        	int nPregunta;
        	string nombreSala;
        	
        };
        
        ["java:type:java.util.Vector<Sala>"] sequence<Sala> S;
        
        struct Msg {
    		S salas;
        };
        
        class Servidor 
        {
        	int crearSala(string nombre);
        	int unirse(string nombre);
        	void shutdown();
        	Msg listar();
        
        	
        };
    
    };
    
    #endif
    

    That's all, I really can't see the problem, maybe it has to do something with the Server and Client.. or the Slice file.. I hope you can help me, or maybe point me another way to do it. Thank you
  • mes
    mes California
    Hi,

    I believe your problem is caused by the fact that your subclass Sala is declaring its own data member nombreSala, which overrides the one declared by the Slice-generated base class Demo.Sala. If you look at the generated code for Demo.Sala, you will see that it has already declared a data member that corresponds to each member in the Slice definition. If you remove these duplicate data members, your example should work as you expect.

    Take care,
    - Mark
  • Yes

    Indeed, that was the problem;) , I forgot it overrides the ones declared in slice, thank you very much. :)
    mes wrote:
    Hi,

    I believe your problem is caused by the fact that your subclass Sala is declaring its own data member nombreSala, which overrides the one declared by the Slice-generated base class Demo.Sala. If you look at the generated code for Demo.Sala, you will see that it has already declared a data member that corresponds to each member in the Slice definition. If you remove these duplicate data members, your example should work as you expect.

    Take care,
    - Mark