KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > YapClientThread


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.cs;
22
23 import com.db4o.cs.messages.*;
24 import com.db4o.foundation.*;
25 import com.db4o.foundation.network.*;
26
27 class YapClientThread extends Thread JavaDoc{
28     
29     private YapClient i_stream;
30     private YapSocket i_socket;
31     final Queue4 messageQueue;
32     final Lock4 messageQueueLock;
33     
34     
35     YapClientThread(YapClient client, YapSocket a_socket, Queue4 messageQueue_, Lock4 messageQueueLock_){
36         synchronized(this){
37             i_stream = client;
38             messageQueue = messageQueue_;
39             i_socket = a_socket;
40             messageQueueLock = messageQueueLock_;
41         }
42     }
43     
44     synchronized boolean isClosed(){
45         return i_socket == null;
46     }
47     
48     synchronized void close(){
49         i_stream = null;
50         i_socket = null;
51         //interrupt();
52
}
53     
54     public void run() {
55         while(i_socket != null){
56             try {
57                 if(i_stream == null){
58                     return;
59                 }
60                 final Msg message;
61                 try {
62                     message=Msg.readMessage(i_stream.getTransaction(), i_socket);
63                 }
64                 catch(Exception JavaDoc exc) {
65                     messageQueueLock.run(new Closure4() {
66                         public Object JavaDoc run() {
67                             messageQueue.add(Msg.ERROR);
68                             close();
69                             messageQueueLock.awake();
70                             return null;
71                         }
72                     });
73                     
74                     close();
75                     return;
76                 }
77                 if(i_stream == null){
78                     return;
79                 }
80                 if(Msg.PING.equals(message)){
81                     i_stream.writeMsg(Msg.OK);
82                 }else if(Msg.CLOSE.equals(message)){
83                     i_stream.logMsg(35, i_stream.toString());
84                     if(i_stream == null){
85                         return;
86                     }
87                     
88                     // TODO: There was a strange notify call here,
89
// possibly to accelerate shutting down.
90

91                     // Old code was: i_stream.notify(), but we found
92
// no reference to YapStream.wait().
93

94                     // The possible intention was probably the following:
95

96                     // messageQueueLock.awake();
97

98                     i_stream = null;
99                     i_socket = null;
100                 }else /*if (message != null)*/{
101                     messageQueueLock.run(new Closure4() {
102                         public Object JavaDoc run() {
103                             messageQueue.add(message);
104                             messageQueueLock.awake();
105                             return null;
106                         }
107                     });
108                 }
109             } catch (Exception JavaDoc exc) {
110                 close();
111                 return;
112             }
113         }
114     }
115 }
116
Popular Tags