aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Connecting from the Client to Local and Remote Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Connecting from the Client to Local and Remote Server" Watch "Connecting from the Client to Local and Remote Server" New topic
Author

Connecting from the Client to Local and Remote Server

Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hello Everybody,


I am trying to come up with a clean API for my client to invoke My BusinessServices class
that I instatntiate in my GUI controller object.

I am following a similar approach to my BusinessServices class as it is described in the excellent postings
by Robert Perillo here and here.

Now somehow from my client, in this case the GUI Controller when I am working in standalone mode I need to pass the location (pathname) of the Database file,
and when I am working in a networked client mode I need to pass an IP address (or hostname) and a port number.

I am not sure if the path to go would be like making a Connector class like in the Monkhouse book you have a DvdConnector object for a direct connection and a different DvdConnector object
for a remote connection, where both DvdConnector objects have a different signature: pathname versus hostname and port number.


Any ideas? suggestions?

Thanks in advance,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD, CCDH, CCAH http://www.linkedin.com/in/carlosamorillo
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Carlos!!!

I am following a similar approach to my BusinessServices class as it is described in the excellent postings
by Robert Perillo here and here


Alright! Good choice!!!

Now somehow from my client, in this case the GUI Controller when I am working in standalone mode I need to pass the location (pathname) of the Database file, and when I am working in a networked client mode I need to pass an IP address (or hostname) and a port number.


Hum... how about arranging these objects when the application is starting? For instance, following the design proposed by me in these posts, you just need to have one constructor in your services class, which expects the interface provided by Sun (or your interface, if you created one that extends the interface provided by Sun). Now, when the application starts in standalone mode, you create the Data object and pass it to the services class. When it starts in server mode, you also create the Data object and pass it to the services class. The constructor of your GUI also has to have one constructor, which expects your services interface. In standalone mode, your GUI will have a reference to the DefaultServices class, and in client mode, your GUI will have a reference to the RMIRemoteServices, but since these 2 classes obey the Services API, then your GUI just needs a constructor expecting the Services interface. This way, you will promote polimorphism, since your GUI will work properly, but will not know where the data comes from.

The secret is connecting these objects properly when the application is starting.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5402
    
  13

Hi Carlos,

My GuiController uses a LocalConnector and a RMIConnector to retrieve the appropriate connection. The first one just needs the dbLocation, The second one needs the server address and the port number.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto,


I still don't quite clearly understand you, but
on the server side, that's exactly what I am doing.

The network server needs the pathname of the database file.

However, from the Controller in my GUI in standalone mode I should be able to instantiate the BusinessServices object.

In standalone I should be able to instantiate it just with the pathname of the database file.

In remote mode as a network client I should be able to instantiate it with the hostname/ip address and a port number.


I am going to try to take the approach with 2 different Connector objects like Roel.


Thanks,


Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5402
    
  13

Hi Carlos,

The network server also needs the port it will run on. Actually the network server is not part of this discussion, because you have to start the registry and bind your business service implementation to the registry.

The constructor of my controller takes 3 arguments:
  • mode: the application mode in which the client application has to run (standalone or network)
  • database location: the path to the database file (in standalone mode) or the server's ip address (network mode)
  • port number: the port number the server application is running on (network mode)


  • And the rest of the constructor is nothing more than this code snippet:
    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Thanks.
    Yes, you are correct the network server also needs the port number where it's going to be bound to besides the pathname to the database file.

    By the way, I am getting very close to the end!


    Following the connector approach, I was able to start my network server in Solaris under NetBeans
    and connect 2 network clients: 1 from NetBeans in Solaris and 1 from my laptop in Windows XP and get the JTable object in their
    respective GUIs populated with all the Hotel Room records across the network:


    Thanks once again for your help!

    Carlos.
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2266
        
        3

    Hey, Carlos!

    The network server needs the pathname of the database file.


    Exactly.

    Champion, here's an example of how you can do the stuff. This is the example I put in the article I wrote for a Java magazine here in Brazil (I'm trying to see with the guys of the magazine if I can translate it to english and put it on the internet). The example is a more complete, but try to see the modeling and see how you can connect the objects. In the example of the magazine, I said that it is possible to create an abstract class, called OKButtonListener, that is extended by StandaloneOKButtonListener and ClientOKButtonListener. The OKButtonListener has an abstract method called getServices(), that returns an implementation of the Services interface. The StandaloneOKButtonListener returns a DefaultServices object, and the ClientOKButtonListener returns a RMIRemoteServices object. You can then improve this design, and create an hierarchy of interfaces for each configuration window, etc. For this example, just notice the modeling and see how you can connect the objects.



    And that's it! Your window will not know whether the data is local or remote. In case of client mode, you instantiate a RMIRemoteServices object, based on the information the user provided in the configuration window and pass it to the MainWindow object.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Connecting from the Client to Local and Remote Server