Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI: How to test if a user is online before connection to server

 
Bob Robertson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to experiement with RMI a little and have come up with a small program to test, but I need some advice from the group.

Right now, I have created the following:

A server
A local client console
A remote client console

I would like the remote console to only perform 2pcs of the functionality the full local client provides. Part of this functionality is to allow a user to record some information while offline. This is stored in a flat file. When the user is able to get back on the network, I want them to read from the flat file and synchronize the data to a database I've got written.

The question I have is, when main() is called on the client, if the user if offline, a number of exceptions will be thrown. I don't want this since I want to allow a user to work offline until they can synchronize. I've been told that programming by exception is a bad practice, so I would like opinions on how to test to see whether the user is online or not.

Thanks in advance!

kh
[ May 21, 2004: Message edited by: Bob Robertson ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that
programming by exception is a bad practice
is overly broad. There is nothing wrong with using an exception to detect the fact that the user is offline.
Bill
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point about Exceptions is that they're intended to handle exceptional conditions. They carry extra overhead over simple if/then logic tests because they have to package up the execution context and throw it back up the stack chain.

However, that overhead is trivial compared to something like setting up a network session.

Exceptions are Bad Practice where you're using them for trivial or often/repeated situations, but when you've got a show-stopping condition (and I think failure to connect can reasonbly classified as such), then use of Exceptions can make your code much cleaner and more reliable. In some cases, also more efficient, since higher-level services don't need to have "is this working?" tests coded. They'll know that things are working becuase otherwise the Exception would fly right past them.
 
Catalin Merfu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to design the local console to:
1. Save the data in a file if not connected;
2. Send the data to the remote server if not connected.
This is similar to a mail client that store
the messages in the Outbox and sends them when able to connect
to the SMTP server.

It's perfectly normal for the networking API to throw exceptions
and this can occur in 2 situations:
- When you connect;
- When you send the data.

All the time your application "knows" if it is connected or not.
Let assume you would model the connection status using a 'connected'
flag. When starting the client you are unconnected.

When the server connect method is called:
...
try
{
doLookup(); //connect
connected = true;

if( fileIsAvailable )
sendFile(); // this can throw an exception

removeLocalFile(); // don't let any IOException get through
}
catch( IOException ex )
{
connected = false;
}
...


When the save method is called:
...
if( connected )
{
try
{
sendData();
}
catch( IOException )
{
connected = false;
saveToFile(); // this may throw an exception that you have to report
}
}
else
{
saveToFile();
}
...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic