Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadController |
|
| 0.0;0 |
1 | /* | |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
4 | * | |
5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
6 | * | |
7 | * The contents of this file are subject to the terms of either the GNU | |
8 | * General Public License Version 2 only ("GPL") or the Common Development | |
9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
10 | * may not use this file except in compliance with the License. You can obtain | |
11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
13 | * language governing permissions and limitations under the License. | |
14 | * | |
15 | * When distributing the software, include this License Header Notice in each | |
16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
17 | * Sun designates this particular file as subject to the "Classpath" exception | |
18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
19 | * accompanied this code. If applicable, add the following below the License | |
20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
21 | * identifying information: "Portions Copyrighted [year] | |
22 | * [name of copyright owner]" | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * If you wish your version of this file to be governed by only the CDDL or | |
27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
28 | * elects to include this software in this distribution under the [CDDL or GPL | |
29 | * Version 2] license." If you don't indicate a single choice of license, a | |
30 | * recipient has the option to distribute your version of this file under | |
31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
34 | * only if the new code is made subject to such option by the copyright | |
35 | * holder. | |
36 | * | |
37 | */ | |
38 | ||
39 | package com.sun.grizzly; | |
40 | ||
41 | import java.io.IOException; | |
42 | import java.nio.channels.SelectableChannel; | |
43 | import java.nio.channels.SelectionKey; | |
44 | import java.util.Iterator; | |
45 | import java.util.concurrent.atomic.AtomicInteger; | |
46 | ||
47 | /** | |
48 | * ReadController class represents {@link Controller}, | |
49 | * which is not itself independent. | |
50 | * | |
51 | * Should be used for handling OP_READ operations | |
52 | * Supports TCP derived protocols | |
53 | * | |
54 | * @author Alexey Stashok | |
55 | */ | |
56 | 17 | public class ReadController extends Controller { |
57 | ||
58 | /** | |
59 | * Gets {@link SelectorHandler}'s clone, registered | |
60 | * on this{@link ReadController} | |
61 | * | |
62 | * @param selectorHandler original {@link SelectorHandler} | |
63 | * @return passed {@link SelectorHandler} clone, registered | |
64 | * on this{@link ReadController} | |
65 | */ | |
66 | public SelectorHandler getSelectorHandlerClone(SelectorHandler selectorHandler) { | |
67 | 30 | Iterator<SelectorHandler> it = selectorHandlers.iterator(); |
68 | 32 | while(it.hasNext()) { |
69 | 30 | SelectorHandler cloneSelectorHandler = it.next(); |
70 | 30 | if (cloneSelectorHandler.getStateHolder() == selectorHandler.getStateHolder()) { |
71 | 28 | return cloneSelectorHandler; |
72 | } | |
73 | 2 | } |
74 | ||
75 | 2 | return null; |
76 | } | |
77 | ||
78 | ||
79 | /** | |
80 | * Removes {@link SelectorHandler}'s clone, registered | |
81 | * on this{@link ReadController} | |
82 | * | |
83 | * @param selectorHandler | |
84 | */ | |
85 | public void removeSelectorHandlerClone(SelectorHandler selectorHandler) { | |
86 | 0 | SelectorHandler cloneSelectorHandler = getSelectorHandlerClone(selectorHandler); |
87 | 0 | if (cloneSelectorHandler != null) { |
88 | 0 | removeSelectorHandler(cloneSelectorHandler); |
89 | } | |
90 | 0 | } |
91 | ||
92 | /** | |
93 | * Add a {@link Channel} | |
94 | * to be processed by{@link ReadController}'s | |
95 | * {@link SelectorHandler} | |
96 | * | |
97 | * @param channel new channel to be managed by ReadController | |
98 | * @param protocol name of the protocol channel corresponds to | |
99 | */ | |
100 | public void addChannel(SelectableChannel channel, SelectorHandler selectorHandler) { | |
101 | 30 | selectorHandler.register(channel, SelectionKey.OP_READ); |
102 | 30 | } |
103 | ||
104 | /** | |
105 | * Start the Controller. If the Pipeline and/or Handler has not been | |
106 | * defined, the default will be used. | |
107 | */ | |
108 | @Override | |
109 | public void start() throws IOException { | |
110 | 17 | notifyStarted(); |
111 | ||
112 | 17 | int selectorHandlerCount = selectorHandlers.size(); |
113 | 17 | readySelectorHandlerCounter = new AtomicInteger(selectorHandlerCount); |
114 | 17 | stoppedSelectorHandlerCounter = new AtomicInteger(selectorHandlerCount); |
115 | ||
116 | 17 | for (SelectorHandler selectorHandler : selectorHandlers) { |
117 | 17 | Runnable selectorRunner = new SelectorHandlerRunner(this, selectorHandler); |
118 | // check if there is java.nio.Selector already open, | |
119 | // if so, just notify the controller onReady() listeners | |
120 | 17 | if (selectorHandler.getSelector() != null) { |
121 | 17 | notifyReady(); |
122 | } | |
123 | ||
124 | 17 | if (selectorHandlerCount > 1) { |
125 | // if there are more than 1 selector handler - run it in separate thread | |
126 | 0 | new Thread(selectorRunner, "GrizzlySelectorRunner-read-" + selectorHandler.protocol()).start(); |
127 | } else { | |
128 | // else run it in current thread | |
129 | 17 | selectorRunner.run(); |
130 | } | |
131 | 17 | } |
132 | ||
133 | 17 | waitUntilSeletorHandlersStop(); |
134 | 17 | selectorHandlers.clear(); |
135 | 17 | } |
136 | ||
137 | /** | |
138 | * {@inheritDoc} | |
139 | */ | |
140 | @Override | |
141 | public void stop() throws IOException { | |
142 | 17 | if (stoppedSelectorHandlerCounter != null) { |
143 | 17 | waitUntilSeletorHandlersStop(); |
144 | } | |
145 | 17 | } |
146 | } |