|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SocketSynchChannelServer.java | 60% | 85.2% | 100% | 83.3% |
|
||||||||||||||
| 1 |
/**
|
|
| 2 |
*
|
|
| 3 |
* Copyright 2004 Hiram Chirino
|
|
| 4 |
*
|
|
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 6 |
* you may not use this file except in compliance with the License.
|
|
| 7 |
* You may obtain a copy of the License at
|
|
| 8 |
*
|
|
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 10 |
*
|
|
| 11 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 14 |
* See the License for the specific language governing permissions and
|
|
| 15 |
* limitations under the License.
|
|
| 16 |
*
|
|
| 17 |
**/
|
|
| 18 |
|
|
| 19 |
package org.activeio.net;
|
|
| 20 |
|
|
| 21 |
import java.io.IOException;
|
|
| 22 |
import java.net.ServerSocket;
|
|
| 23 |
import java.net.Socket;
|
|
| 24 |
import java.net.SocketException;
|
|
| 25 |
import java.net.SocketTimeoutException;
|
|
| 26 |
import java.net.URI;
|
|
| 27 |
|
|
| 28 |
import org.activeio.Channel;
|
|
| 29 |
import org.activeio.SynchChannelServer;
|
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* A SynchChannelServer that creates
|
|
| 33 |
* {@see org.activeio.net.TcpSynchChannel}objects from accepted
|
|
| 34 |
* tcp socket connections.
|
|
| 35 |
*
|
|
| 36 |
* @version $Revision$
|
|
| 37 |
*/
|
|
| 38 |
public class SocketSynchChannelServer implements SynchChannelServer { |
|
| 39 |
|
|
| 40 |
private ServerSocket serverSocket;
|
|
| 41 |
private int curentSoTimeout = 0; |
|
| 42 |
private final URI bindURI;
|
|
| 43 |
private final URI connectURI;
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
/**
|
|
| 47 |
* @param serverSocket2
|
|
| 48 |
* @param bindURI2
|
|
| 49 |
* @param connectURI2
|
|
| 50 |
*/
|
|
| 51 | 64 |
public SocketSynchChannelServer(ServerSocket serverSocket, URI bindURI, URI connectURI) {
|
| 52 | 64 |
this.serverSocket=serverSocket;
|
| 53 | 64 |
this.bindURI=bindURI;
|
| 54 | 64 |
this.connectURI=connectURI;
|
| 55 |
} |
|
| 56 |
|
|
| 57 | 164 |
public Channel accept(long timeout) throws IOException { |
| 58 | 164 |
try {
|
| 59 | 164 |
if (timeout == SynchChannelServer.WAIT_FOREVER_TIMEOUT)
|
| 60 | 0 |
setSoTimeout(0); |
| 61 | 164 |
else if (timeout == SynchChannelServer.NO_WAIT_TIMEOUT) |
| 62 | 0 |
setSoTimeout(1); |
| 63 |
else
|
|
| 64 | 164 |
setSoTimeout((int) timeout);
|
| 65 |
|
|
| 66 | 164 |
Socket socket = serverSocket.accept(); |
| 67 | 70 |
return createChannel(socket);
|
| 68 |
|
|
| 69 |
} catch (SocketTimeoutException ignore) {
|
|
| 70 |
} |
|
| 71 | 65 |
return null; |
| 72 |
} |
|
| 73 |
|
|
| 74 | 48 |
protected Channel createChannel(Socket socket) throws IOException { |
| 75 | 48 |
return new SocketSynchChannel(socket); |
| 76 |
} |
|
| 77 |
|
|
| 78 | 164 |
private void setSoTimeout(int i) throws SocketException { |
| 79 | 164 |
if (curentSoTimeout != i) {
|
| 80 | 64 |
serverSocket.setSoTimeout(i); |
| 81 | 64 |
curentSoTimeout = i; |
| 82 |
} |
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* @see org.activeio.Disposable#dispose()
|
|
| 87 |
*/
|
|
| 88 | 64 |
public void dispose() { |
| 89 | 64 |
if (serverSocket == null) |
| 90 | 0 |
return;
|
| 91 | 64 |
try {
|
| 92 | 64 |
serverSocket.close(); |
| 93 |
} catch (IOException ignore) {
|
|
| 94 |
} |
|
| 95 | 64 |
serverSocket = null;
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* @return Returns the bindURI.
|
|
| 100 |
*/
|
|
| 101 | 36 |
public URI getBindURI() {
|
| 102 | 36 |
return bindURI;
|
| 103 |
} |
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* @return Returns the connectURI.
|
|
| 107 |
*/
|
|
| 108 | 158 |
public URI getConnectURI() {
|
| 109 | 158 |
return connectURI;
|
| 110 |
} |
|
| 111 |
|
|
| 112 | 64 |
public void start() throws IOException { |
| 113 |
} |
|
| 114 |
|
|
| 115 | 30 |
public void stop(long timeout) throws IOException { |
| 116 |
} |
|
| 117 |
|
|
| 118 | 8 |
public Object narrow(Class target) {
|
| 119 | 8 |
if( target.isAssignableFrom(getClass()) ) {
|
| 120 | 8 |
return this; |
| 121 |
} |
|
| 122 | 0 |
return null; |
| 123 |
} |
|
| 124 |
|
|
| 125 | 30 |
public String toString() {
|
| 126 | 30 |
return "Socket Server: "+getConnectURI(); |
| 127 |
} |
|
| 128 |
} |
|
||||||||||