File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes implement book by socket. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "implement book by socket." Watch "implement book by socket." New topic
Author

implement book by socket.

Paul Sun
Greenhorn

Joined: Aug 26, 2002
Posts: 19
Hi:
I am using socket to implement the assignment.
I asked a question about lock/unlock before,
With member's help, I do it in the background on
server side. But I am confused on how to notify client after unlock. Because I use socket, if the client wait for reply, code like this
while( true ) {
fromServer = in.readLine();
}
the client have to do nothing until the result of booking ticket come back. How to implement it?
Thanks.
Paul
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paul,
The best way to avoid a busy wait, is to call wait() on the object and after the event that you are waiting on occurs call notifyAll(). It is quite simple to do this in Java using the Observer interface and by having the class that you are wait()ing for extend Observable. The Observable calls setChanged() and notifyObservers() when the event completes and in turn the update() method on the Observer is called. So in your update() method on the Observer you could call notifyAll() to release the thread that is waiting for the event to complete.

Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Paul Sun
Greenhorn

Joined: Aug 26, 2002
Posts: 19
Hi Michael:
Thank you for your reply. But the problem is my design is view -> model -> dbclient, I have used Observer pattern on view-model, the model class extends Observable, so that I can not use it on
db client and model. And the db client is not a thread, when client need send data to server it will be called and work for it. So if client wait for the reply continuelly, user can do nothing at that time.
Thanks.
Paul
Paul Sun
Greenhorn

Joined: Aug 26, 2002
Posts: 19
Hi:
I can not work it out. If I make user wait reply and can not input( or other things) until info received from server, is it all right?

Is there somebody implement assignment with sockt?
Paul.
Jacob George
Ranch Hand

Joined: Jun 26, 2001
Posts: 46
Hi,
I think making the client wait till a response arrives from server is the best way to do it. Otherwise each request to server should be done in a seperate Thread. I think a single Threaded model is just fine. This is a common problem no matter the implementation is Sockets or RMI. In both the implementations, the call will wait till lock is acquired. I think this approach is quite OK. But dont know for certain since I am also doing the assignment and have not submitted yet.
regards Jacob
Paul Sun
Greenhorn

Joined: Aug 26, 2002
Posts: 19
Hi:
I think client wait for reply may not be a only way but at least it is simple to implement.
To image that you book tickets on web site,
after push book button on a web page, you have to wait the response. Can you book an ticket or do other things at that time? I think you can not.
ok, I make it wait until the reply arrive.
Thank you for reply. You give me a great help.
Paul
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paul,
All of my booking requests occurred on a separate thread. I also had a problem with Observer like you, so I implemented a "roll-your-own" Observable to overcome the problem. I just kept a Set of Observers and in my home grown notifyObservers() method, called update() on all the registered Observers.
Running each booking operation in a separate thread overcomes the problem of a deceased client that may have owned the lock when it died. So long as you have provided a means to release those locks at some point.
Hope this helps,
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implement book by socket.