View Javadoc

1   /*
2    * telnetservice, Open Source Telnet Service Library
3    *
4    * Copyright (c) 2007-2008, Joseph Walid Gédéon, and individual
5    * contributors as indicated by the @authors tag. See copyright.txt in the
6    * distribution for a full listing of individual contributors.
7    * All rights reserved.
8    *
9    * This is free software; you can redistribute it and/or modify it
10   * under the terms of the Modified BSD License as published by the Free
11   * Software Foundation.
12   *
13   * This software is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Modified
16   * BSD License for more details.
17   *
18   * You should have received a copy of the Modified BSD License along with
19   * this software; if not, write to the Free Software Foundation, Inc.,
20   * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the
21   * FSF site: http://www.fsf.org.
22   */
23  package fr.gedeon.telnetservice.coreexample1;
24  
25  import org.apache.log4j.Logger;
26  
27  import fr.gedeon.telnetservice.TelnetService;
28  import fr.gedeon.telnetservice.TelnetServiceStartException;
29  
30  public class Example1
31  {
32      public static class ServiceState
33      {
34          boolean isRunning = false;
35      }
36  
37      public static void main(String[] args) throws TelnetServiceStartException {
38          Example1 example = new Example1();
39          example.initialize();
40          example.run();
41      }
42  
43      protected ServiceState serviceState;
44      protected TelnetService telnetService;
45      
46      public void initialize() {
47          this.serviceState = new ServiceState();
48  
49          this.telnetService = new TelnetService();
50          this.telnetService.setBindPort(2323);
51  
52          this.telnetService.unregisterAllExtensions();
53          this.telnetService.registerExtension(new TelnetServiceExtensionExample1Impl(this.serviceState));
54      }
55      
56      public void run() throws TelnetServiceStartException {
57          Logger logger = Logger.getLogger(Example1.class);
58  
59          this.serviceState.isRunning = true;
60          this.telnetService.start();
61  
62          while (this.serviceState.isRunning) {
63              try {
64                  Thread.sleep(1331);
65              }
66              catch (InterruptedException e) {
67                  logger.debug("Interrupted", e);
68              }
69          }
70      }
71  }