• 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

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic