[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/io ServerSocketFacadeImpl.java,NONE,
Brought to you by:
srt
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/io In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22302/src/java/net/sf/asterisk/io Added Files: ServerSocketFacadeImpl.java SocketConnectionFacade.java ServerSocketFacade.java SocketConnectionFacadeImpl.java Log Message: --- NEW FILE: ServerSocketFacadeImpl.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.io; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class ServerSocketFacadeImpl implements ServerSocketFacade { private ServerSocket serverSocket; public ServerSocketFacadeImpl(int port) throws IOException { this.serverSocket = new ServerSocket(port); } public SocketConnectionFacade accept() throws IOException { Socket socket; socket = serverSocket.accept(); return new SocketConnectionFacadeImpl(socket); } public void close() throws IOException { serverSocket.close(); } } --- NEW FILE: SocketConnectionFacade.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.io; import java.io.IOException; /** * The SocketConnectionFacade provides read and write operation for * communication over TCP/IP sockets.<br> * It hides the details of the underlying I/O system used for socket * communication. * * @author srt * @version $Id: SocketConnectionFacade.java,v 1.1 2005/03/08 16:48:35 srt Exp $ */ public interface SocketConnectionFacade { /** * Reads a line of text from the socket connection. The current thread is * blocked until either the next line is received or an IOException * encounters. * * @return the line of text received excluding any newline character * @throws IOException if the connection has been closed. */ String readLine() throws IOException; /** * Sends a given String to the socket connection. * * @param s the String to send. * @throws IOException if the String cannot be sent, maybe because the * connection has already been closed. */ void write(String s) throws IOException; /** * Flushes the socket connection, that is sends any buffered but yet unsent * data. * * @throws IOException if the connection cannot be flushed. */ void flush() throws IOException; /** * Closes the socket connection including its input and output stream and * frees all associated ressources.<br> * When calling close() any Thread currently blocked by a call to readLine() * will be unblocked and receive an IOException. * * @throws IOException if the socket connection cannot be closed. */ void close() throws IOException; } --- NEW FILE: ServerSocketFacade.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.io; import java.io.IOException; /** * * @author srt * @version $Id: ServerSocketFacade.java,v 1.1 2005/03/08 16:48:35 srt Exp $ */ public interface ServerSocketFacade { /** * Waits for a new incoming connection. * * @return * @throws IOException */ SocketConnectionFacade accept() throws IOException; /** * Unbinds and closes the server socket. * * @throws IOException if the server socket cannot be closed. */ void close() throws IOException; } --- NEW FILE: SocketConnectionFacadeImpl.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class SocketConnectionFacadeImpl implements SocketConnectionFacade { private final Socket socket; private final BufferedReader reader; private final BufferedWriter writer; public SocketConnectionFacadeImpl(String host, int port) throws IOException { this.socket = new Socket(host, port); this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())); } SocketConnectionFacadeImpl(Socket socket) throws IOException { this.socket = socket; this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())); } public String readLine() throws IOException { return reader.readLine(); } public void write(String s) throws IOException { writer.write(s); } public void flush() throws IOException { writer.flush(); } public void close() throws IOException { this.socket.close(); } } |