Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implement book by socket.

 
Paul Sun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Sun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic