View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.service.server;
14  
15  import org.abstracthorizon.danube.connection.ConnectionHandler;
16  import org.abstracthorizon.danube.service.Service;
17  
18  import java.net.InetAddress;
19  import java.net.InetSocketAddress;
20  
21  /**
22   * This abstract cass introduces port for potential socket to listen to
23   * and connection handler to be invoked with created connection.
24   *
25   * @author Daniel Sendula
26   */
27  public abstract class ServerService extends Service {
28  
29      /** Socket address */
30      protected InetSocketAddress socketAddress;
31  
32      /** Connection handler new connection to be handed to */
33      protected ConnectionHandler connectionHandler;
34  
35      /**
36       * Default constructor
37       */
38      public ServerService() {
39      }
40  
41      /**
42       * Returns the port service is expecting connections on
43       * @return the port
44       */
45      public int getPort() {
46          if (socketAddress == null) {
47              return -1;
48          } else {
49              return socketAddress.getPort();
50          }
51      }
52  
53      /**
54       * Sets the port. It has to be set before {@link #create()} method is called.
55       * @param port the port
56       */
57      public void setPort(int port) {
58          if (socketAddress == null) {
59              socketAddress = new InetSocketAddress(port);
60          } else {
61              String ip = socketAddress.getAddress().getHostAddress();
62              socketAddress = new InetSocketAddress(ip, port);
63          }
64      }
65  
66      public void setAddress(String address) {
67          int port = getPort();
68          socketAddress = new InetSocketAddress(address, port);
69      }
70  
71      public String getAddress() {
72          if (socketAddress == null) {
73              return "*";
74          } else {
75              InetAddress inetAddress = socketAddress.getAddress();
76              if (inetAddress == null) {
77                  return "*";
78              } else {
79                  return inetAddress.getHostAddress();
80              }
81          }
82      }
83  
84      public void setSocketAddress(InetSocketAddress socketAddress) {
85          this.socketAddress = socketAddress;
86      }
87  
88      public InetSocketAddress getSocketAddress() {
89          if (socketAddress == null) {
90              socketAddress = new InetSocketAddress("*", 0);
91          }
92          return socketAddress;
93      }
94  
95      /**
96       * Returns connection handler connections are handed to.
97       * @return connection handler
98       */
99      public ConnectionHandler getConnectionHandler() {
100         return connectionHandler;
101     }
102 
103     /**
104      * Sets connection handler
105      * @param connectionHandler connection handler
106      */
107     public void setConnectionHandler(ConnectionHandler connectionHandler) {
108         this.connectionHandler = connectionHandler;
109     }
110 
111 }