• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with pass-by-reference ?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,

when i m trying to get the list of clients(vectors or linkedList/arraylist/List) from server object in my application logic remote object to update the clients. i m getting 0 size of list and i can'T iterate through that.

is there some typical procedure/restriction in RMI to handle this problem.

this is the code:



this is class where i wanted to access the same list :



i m calling the register() from some client(swing GUI) and sending the client as argument to add in that list.
after adding to the list i m calling getClientname() from messageImpl and it should show one element in messageimpl class but it is not.
i tried lot of ways to handle this like vector/ lists and custom class "serverengine" also but i m still getting 0 elements in the list in the messageimpl class.
load of thanks for any help.
jibby
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Java uses pass-by-reference. But not with RMI though. In RMI, all method arguments and return values are serialized/deserialized. In essence a copy is made.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, I have to say this code sample is terribly hard to follow. You've got so many methods and variables with similar names, redundant member variables, some static, some not, and random letter-case. It's almost physically painful to read. You seriously need to pick a coding convention and learn to follow it!

Second, as Rob says, objects in RMI may (or may not) be serialized and copied when passed as remote method arguments. Plain, unexported objects are copied; servant objects are passed by (remote) reference, though.

I suspect -- I don't know for sure, as you're not showing all your code (and it would hurt my brain to read it if you did!) -- but I suspect that the problem is what Rob's getting at: your MessageImpl class in the client is ultimately looking at the static variable in a local copy of the ServerServicesImpl class, not the remote instance which actually has a callback registered. In the client, is your instance of MessageImpl a remote object, or a local object?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great thanks for reply and welcoming Ernest Friedman-Hill

although messageImpl is remoteobject but that is instantiating in client like this:



i m really lost in this problem and that is reflecting on code also that i started hit and try approach .
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jibby Lala wrote: thanks for reply and welcoming Ernest Friedman-Hill

although messageImpl is remoteobject but that is instantiating in client like this:



OK, that's the problem. Although MessageImpl implements UnicastRemoteObject, your client is not using it as a remote object, but rather creating its own local copy. That local copy on the client doesn't have access to the static variable on the server, which is in another JVM altogether, or maybe even on another computer.

For this to work, the client would need to be talking remotely to a MessageImpl that was exported from the server.
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as far as my understanding

you are saying to instantiate messageimpl like this :



and bind that to RMI also :



am i right ?
thnks

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but in the server JVM, not the client one; the client one would have to use Naming.lookup() to find it.

I realize this may be completely incompatible with what you're trying to implement, but the fact remains that for an object to be able to have direct access to a static variable in the server VM, then it has to physically exist there, in the server VM.

If MessageImpl objects are going to be sent back and forth between the client and server, then while the object is in the server, the code that tries to access the server variable should indeed work. But while the object is on the client, it obviously won't work.
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now if i want now another object/objects like Message then i have to do the same procedure for every object ,there isn't any other way to do that and that will not lead me to this horrible problem. and in which i don't need to repeat this whole RMI procedure for every object.
thanks
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far, I've been trying to help you solve your immediate problem, without talking about your overall design. Maybe I should have talked about the design first, because the problem you're having shouldn't really come up.

A simple chat system would have just two remote interfaces: a server class with just a few methods like this:



and a Callback interface like



The "message" class could contain just a String to hold the message text, or it could include fancier stuff like a list of client names to send a private chat message to, icons, etc. Message is just a plain old Java class though -- it's not remote. It also needs to be totally self contained. It knows nothing about Callbacks, and nothing about ChatServers. On the contrary, it's the ChatServer and Client implementations that call methods on Messages, extract data from them, and operate on them. Messages need to be sent back and forth over the wire so they should not include references to any other objects or any machine-dependent paths or resources.

You'd have a server class that implements the server interface, and is exported via Naming.rebind(). The client would call connect(). The implementation of connect() would save remote references to callbacks in a non-static List member. sendMessage() would loop over those callbacks and send the Message to each Callback by calling receive() and passing the Message to it. sendMessage() might examine the Message to decide which clients to send the message to. disconnect() could search the list and remove the Callback.

If you want your clients to get other information from the server, then just add methods to the ChatServer class. For example, getUsers() would return a list of the names of the connected users; the server could get this list by iterating over the Callback list and calling a method like getName() on each Callback. There's never a need for Message or Callback or any other class to access the server's private data directly; just add remote methods that the client can call as needed.




 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well first i just wanted to give you BIG HUG for such a commendable volunteering,

i didn't get that exactly because you are not the mentioning serialized object what i wanted to confirm as replacement of remoteobject,

The "message" class could contain just a String to hold the message text, or it could include fancier stuff like a list of client names to send a private chat message to, icons, etc. Message is just a plain old Java class though -- it's not remote. It also needs to be totally self contained. It knows nothing about Callbacks, and nothing about ChatServers. On the contrary, it's the ChatServer and Client implementations that call methods on Messages, extract data from them, and operate on them. Messages need to be sent back and forth over the wire so they should not include references to any other objects or any machine-dependent paths or resources.



secondly, yes i looked at this model or similar what you sent at several tutorial and example, but my goal was to come one with some new model and to achieve DECOUPLING(the client list should sent tO logic and logic do what ever it wants and then call updateclient () method in it and that' what i tried to do ),

The chat applicaiton was just a really simple use case for that to saw the implications, I have to keep in mind other applcaition which could have lengthy logic, i saw some tutorial in which they were using more then two remote interfaces,so i was trying to come up with similar model like one for server stuff as you said, one for application logic like the "messageimpl" remote object and one for callback but as we saw there is flaw in that because the business logic would consist of several objects and it seem infeasible to implement remote interface and formatlities for every object.


what if i made messageimpl class not remote and instead make it serializable? would it work without the problem what i faced and as desired decoupling requirement.

thanks
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, it will also work by just creating the remote instance of ServerServicesImpl in messageimpl class and making message class serializable instead of remote.
 
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic