Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServerUpdateCallback |
|
| 2.0;2 |
1 | /* | |
2 | * #%L | |
3 | * ServerUpdateCallback.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 | package com.allanbank.mongodb.client.state; | |
21 | ||
22 | import java.util.List; | |
23 | ||
24 | import com.allanbank.mongodb.bson.Document; | |
25 | import com.allanbank.mongodb.client.callback.FutureReplyCallback; | |
26 | import com.allanbank.mongodb.client.message.Reply; | |
27 | ||
28 | /** | |
29 | * ServerUpdateCallback provides a special callback update the server with the | |
30 | * first document in the reply. This is useful with {@code ismaster} or | |
31 | * {@code replSetGetStatus} commands. | |
32 | * | |
33 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
34 | * mutated in incompatible ways between any two releases of the driver. | |
35 | * @copyright 2012-2014, Allanbank Consulting, Inc., All Rights Reserved | |
36 | */ | |
37 | 205 | public class ServerUpdateCallback extends FutureReplyCallback { |
38 | ||
39 | /** The server to update the seconds behind for. */ | |
40 | private final Server myServer; | |
41 | ||
42 | /** | |
43 | * Creates a new ServerUpdateCallback. | |
44 | * | |
45 | * @param server | |
46 | * The server we are tracking the latency for. | |
47 | */ | |
48 | public ServerUpdateCallback(final Server server) { | |
49 | 236 | super(); |
50 | 236 | myServer = server; |
51 | 236 | } |
52 | ||
53 | /** | |
54 | * {@inheritDoc} | |
55 | * <p> | |
56 | * Overridden to update the server's latency based on the round trip reply | |
57 | * time. | |
58 | * </p> | |
59 | */ | |
60 | @Override | |
61 | public void callback(final Reply result) { | |
62 | 208 | if (myServer != null) { |
63 | 208 | update(result); |
64 | } | |
65 | ||
66 | 208 | super.callback(result); |
67 | 208 | } |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | * <p> | |
72 | * Overridden to mark the server as {@link Integer#MAX_VALUE} seconds behind | |
73 | * the primary. | |
74 | * </p> | |
75 | */ | |
76 | @Override | |
77 | public void exception(final Throwable error) { | |
78 | 24 | if (myServer != null) { |
79 | 24 | myServer.requestFailed(); |
80 | } | |
81 | ||
82 | 24 | super.exception(error); |
83 | 24 | } |
84 | ||
85 | /** | |
86 | * Updates the server with the first document from the reply. | |
87 | * | |
88 | * @param reply | |
89 | * The reply. | |
90 | */ | |
91 | private void update(final Reply reply) { | |
92 | 208 | if (reply != null) { |
93 | 208 | final List<Document> replyDocs = reply.getResults(); |
94 | 208 | if (replyDocs.size() >= 1) { |
95 | 194 | final Document doc = replyDocs.get(0); |
96 | ||
97 | 194 | myServer.update(doc); |
98 | } | |
99 | } | |
100 | 208 | } |
101 | } |