• 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

Network Mode: Starting Server & Client together

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

I know this question might have been trashed many times here:

How do i start both the client and the server at once?

What i did was to invoke the main() method of the server from the client, which worked. Is this allowed, or is there a more acceptable approach?

Thanks in advance
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

why whould you like to start the client and server together ? If this
is a way to provide the standalone mode, this is a wrong approach and could
give you an automatic failure : it is required to not use any network code
in the standalone mode (no localhost loop).

But maybe i don't understand your question.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In network mode you would do somewhat like this:



When you want to use the database, then you ask the registry for an instance of your dao. Somewhat like this:



Now, in standalone mode, you need not to register the DAO in the RMI registry. Therefore, you only have to do somewhat like this:



Then you can simply use that same instance to do everything.

In other words, you could do somewhat like this:



Using the DAOHotel interface, my entire application does not care if the object was created locally or if it comes from the RMI registry.

Or at least this is the way I am doing it.

I hope that helps!
[ April 18, 2008: Message edited by: Edwin Dalorzo ]
 
Eric Ushie
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwin,

Thanks so much for your answer. It is quite elaborate, but may be i should have asked: how many main() classes do you have?

Actually,i'm asking this because in reality, a server application should be an entirely seperate entity from clients, both having their own main() methods - this is the approach i used, so i had 2 main() methods, which left me wondering how to start the server's main method from the client context.

From your description, i can figure out that both the server and client applications are started from the same main method - correct me if i'm wrong.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric Ushie

As you well said, it can be customary to separate the server from the client.

What I wrote up there was for explanatory purposes. Actually I am still working on the server UI and I have not defined that method yet.

One of my assignment requiments is that everything needs to be packed in a single jar. Also, the requirements state the command line parameters to start the server, the client, or standalone application. All from the same jar.

That is why I ended up creating a single main method, capable to start either the client, the server or the standalone application.

Now, if you create two main methods that means that you designed your application in a such a way that in the future it will be possible to separate the client application startup from the server application startup. I have to say that I think that is a smart idea.

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

which left me wondering how to start the server's main method from the client context.



Hi Eric,
Assuming you have separated the client code form the server code.
Why do you have to call the server main method from the client main method.

All you have to make sure is the server is already running. You communicate with the server using look up on the rmi registry. But for us i think we have to send everything in one jar file.

This is how i am doing. I created Configuration.java class under suncertify.config package. This class has a main method that takes the arguments "server","alone", or left alone with no argument.Inside that class i am creating the gui based on what i receive as an argument.

1)If i receive an argument "server",( I assume the evaluator is running it on the server) I am creating the gui that requires the server,port and database info. After receiving those i would call the server constructor passing the arguments(new DataServer(server,port,database). rest is registering the remote object.

2)If i receive an empty argument ( I assume the evaluator has already did the server part, hopefully running) then i would create a gui that requires server and port info and bring up the real front end gui. The communication to the server is made using the lookup on the rmi registry.


3)If i receive no argument at all, then i am creating gui that requires the database location. The whole application is run locally with out the network code(no rmi calls).


Java Gurus,
Please tell us. Is this approach ok?


John
------------------------
SCJP 5.0







[/B]

[ April 23, 2008: Message edited by: John Mattman ]
[ April 23, 2008: Message edited by: John Mattman ]
 
Eric Ushie
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John Mattman,

I dont know what assignment you are working on, but most assignments have the modes: "alone", "server" and "". Yours should too - i stand corrected;

where:
"alone" means the gui alone should run (without any network code invoked)

"server" means only the server code should run

"" (meaning the command line arg is left out entirely) means both the server and client must run.

I fear that your point (2) might have contravened this requirement if you assume in your code that the application should have been started previously with the "server" mode.

By this i mean, If sun says "" means both the server and client must run it means you must (1) start the server,
then (2) start the client in the same execution instance/main() method entry point.

having 2 main() methods doesnt mean you cannot start the application from a single jar. In your manifest file you can specify the class whose main() method should be the entry point.

- i hope this helps you.

my initial query was if it is permissible to do this without suffering the evaluator's wrath? But knowing what i now know, i believe i can justify my choice and keep my cool. Better still, i can stick with Edwin's simple approach and be happier for it.

Thanks for your contribution
 
John Mattman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I fear that your point (2) might have contravened this requirement if you assume in your code that the application should have been started previously with the "server" mode.

By this i mean, If sun says "" means both the server and client must run it means you must (1) start the server,
then (2) start the client in the same execution instance/main() method entry point.


May be i misunderstood the requirement. In that case when the application is run with no arguments, the gui should require server, port,database location to start the server as well as client right?

I was always under the impression that why should the client should know about the server configuration?. I will change my design if that is what the meaning is.Thank you for correcting me and sorry for misguiding you.

John
[ April 23, 2008: Message edited by: John Mattman ]
 
John Mattman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I fear that your point (2) might have contravened this requirement if you assume in your code that the application should have been started previously with the "server" mode.

By this i mean, If sun says "" means both the server and client must run it means you must (1) start the server,
then (2) start the client in the same execution instance/main() method entry point.




I still could not figure out how a remote server can be started from the client. Can anybody clarify my doubt please?

---------------------
John
 
Eric Ushie
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the SCJD exam, you are allowed to assume that your server resides in the same machine as your GUI client. Therefore feel free to do something that looks like this:

public class ClientAppEntryPoint{
.
.
.
public static void main(String [] arg){
.
.
.
// both server & client must run
if(arg[0].equals("")){
int port = ConfigHandler.getServerPortNumber();
String databaseLocation = ConfigHandler.getDatabaseLocation();

//start the server application
new DataServerEntryPoint(port, databaseLocation);

//you can now go ahead to start your client GUI blah
.
.
.
}
}
 
John Mattman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
So if my suncertify.properties has something like

server.name=some value Then that means the server is installed locally right?.

if there is no server.name property in the properties file then, the server is installed remotely and the gui should let the user type the server name right?
[ April 24, 2008: Message edited by: John Mattman ]
 
Eric Ushie
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Your app must start with a Config Window that contains a panel to allow you set up your server.name, server.port, database.location, etc in the following manner:

First you create your .properties file.

If your app starts in Network mode, then you must provide a UI in your Config Window which loads server.name, server.port and database.location from your .properties file - granted that the file has these values populated.

If these values are not populated in your .properties file, which is a sure possibility if the application is running for the first time or if by some chance the file got corrupted or wiped out completely, What do you do?

You must provide default values in your application that populates these fields peradventure the file is empty or holds null values for any of your keys.

For Server mode, your settings will be different, for instance the UI doesnt need fields like server.name - since the server name will automatically be localhost.

For Stand alone mode, you will not need server.name and server.port, but you sure will need database.location.

i hope this helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic