001: /*
002: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.spi;
022:
023: import java.io.IOException;
024: import java.net.InetAddress;
025: import java.net.InetSocketAddress;
026: import java.net.ServerSocket;
027: import java.net.Socket;
028: import java.util.Map;
029: import java.util.concurrent.atomic.AtomicInteger;
030:
031: import org.xsocket.connection.spi.IAcceptor;
032: import org.xsocket.connection.spi.IAcceptorCallback;
033: import org.xsocket.connection.spi.IIoHandler;
034:
035: /**
036: *
037: * @author grro@xsocket.org
038: */
039: final class ExampleAcceptor implements IAcceptor {
040:
041: private ServerSocket ssocket = null;
042: private volatile boolean isRunning = true;
043:
044: private static AtomicInteger nextId = new AtomicInteger();
045:
046: private IAcceptorCallback callback = null;
047: private InetSocketAddress address = null;
048:
049: public ExampleAcceptor(IAcceptorCallback callback,
050: InetSocketAddress address) {
051: this .callback = callback;
052: this .address = address;
053: }
054:
055: public InetAddress getLocalAddress() {
056: return ssocket.getInetAddress();
057: }
058:
059: public int getLocalPort() {
060: return ssocket.getLocalPort();
061: }
062:
063: public int getNumberOfOpenConnections() {
064: return 0;
065: }
066:
067: public void setOption(String name, Object value) throws IOException {
068: // TODO Auto-generated method stub
069:
070: }
071:
072: public Object getOption(String name) throws IOException {
073: // TODO Auto-generated method stub
074: return null;
075: }
076:
077: public Map<String, Class> getOptions() {
078: // TODO Auto-generated method stub
079: return null;
080: }
081:
082: public void listen() throws IOException {
083: ssocket = new ServerSocket(address.getPort());
084:
085: callback.onConnected();
086:
087: while (isRunning) {
088: try {
089: Socket socket = ssocket.accept();
090: if (socket != null) {
091: callback.onConnectionAccepted(newIoHandler(socket));
092: }
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097: }
098:
099: public void close() throws IOException {
100: isRunning = false;
101: ssocket.close();
102: }
103:
104: private IIoHandler newIoHandler(Socket socket) {
105: String id = ssocket.getLocalSocketAddress().hashCode() + "."
106: + System.currentTimeMillis() + "."
107: + nextId.incrementAndGet();
108: return new ExampleIoHandler(id, socket);
109: }
110: }
|