Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReceiveRunnable |
|
| 4.5;4.5 |
1 | /* | |
2 | * #%L | |
3 | * ReceiveRunnable.java - mongodb-async-driver - Allanbank Consulting, Inc. | |
4 | * %% | |
5 | * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. | |
6 | * %% | |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
8 | * you may not use this file except in compliance with the License. | |
9 | * You may obtain a copy of the License at | |
10 | * | |
11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
12 | * | |
13 | * Unless required by applicable law or agreed to in writing, software | |
14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
16 | * See the License for the specific language governing permissions and | |
17 | * limitations under the License. | |
18 | * #L% | |
19 | */ | |
20 | ||
21 | package com.allanbank.mongodb.client.connection.socket; | |
22 | ||
23 | import java.util.logging.Level; | |
24 | ||
25 | import com.allanbank.mongodb.MongoDbException; | |
26 | import com.allanbank.mongodb.error.ConnectionLostException; | |
27 | import com.allanbank.mongodb.util.IOUtils; | |
28 | ||
29 | /** | |
30 | * Runnable to receive messages from an {@link AbstractSocketConnection}. | |
31 | * | |
32 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
33 | * mutated in incompatible ways between any two releases of the driver. | |
34 | * @copyright 2013, Allanbank Consulting, Inc., All Rights Reserved | |
35 | */ | |
36 | /* package */class ReceiveRunnable implements Runnable { | |
37 | ||
38 | /** The socket we are reading from. */ | |
39 | private final AbstractSocketConnection mySocketConnection; | |
40 | ||
41 | /** | |
42 | * Creates a new ReceiveRunnable. | |
43 | * | |
44 | * @param socketConnection | |
45 | * The socket we are reading from. | |
46 | */ | |
47 | 173 | /* package */ReceiveRunnable(final AbstractSocketConnection socketConnection) { |
48 | 173 | mySocketConnection = socketConnection; |
49 | 173 | } |
50 | ||
51 | /** | |
52 | * Processing thread for receiving responses from the server. | |
53 | */ | |
54 | @Override | |
55 | public void run() { | |
56 | try { | |
57 | 397 | while (mySocketConnection.isOpen()) { |
58 | try { | |
59 | 385 | mySocketConnection.doReceiveOne(); |
60 | ||
61 | // Check if we are shutdown. Note the shutdown() method | |
62 | // makes sure the last message gets a reply. | |
63 | 231 | if (mySocketConnection.isShuttingDown() |
64 | && mySocketConnection.isIdle()) { | |
65 | // All done. | |
66 | return; | |
67 | } | |
68 | } | |
69 | 154 | catch (final MongoDbException error) { |
70 | 154 | if (mySocketConnection.isOpen()) { |
71 | 1 | mySocketConnection.myLog.log( |
72 | Level.WARNING, | |
73 | "Error reading a message: " | |
74 | + error.getMessage(), error); | |
75 | ||
76 | 1 | mySocketConnection.shutdown( |
77 | new ConnectionLostException(error), false); | |
78 | } | |
79 | // All done. | |
80 | return; | |
81 | 225 | } |
82 | } | |
83 | } | |
84 | finally { | |
85 | // Make sure the connection is closed completely. | |
86 | 160 | IOUtils.close(mySocketConnection); |
87 | 12 | } |
88 | 12 | } |
89 | } |