Server.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00024 package ATPInterface;
00025 
00026 import java.net.*;
00027 import java.io.IOException;
00028 import java.util.*;
00029 
00050 public class Server {
00051 
00053     protected InetAddress ip;
00054 
00056     protected int port;
00057 
00070     protected Map openSessions;
00071 
00075     boolean verboseOutput = false;
00076 
00077 
00079     protected static void printSyntaxHelp() {
00080         System.err.println("arguments: IP PORT [options]");
00081         System.err.println("possible options:");
00082         System.err.println("    -v   verbose output to stdout");
00083         System.err.println();
00084     }
00085 
00090     public static void main(String[] args) {
00091         
00092         System.err.println();
00093 
00094         System.out.println();
00095         System.out.println("ATP (Assumption-based Theorem prover Protocol) Server");
00096 
00097         if (args.length < 2) {
00098             printSyntaxHelp();
00099             return;
00100         }
00101 
00102         InetAddress ip;
00103         try {
00104             ip = InetAddress.getByName(args[0]);
00105         } catch (UnknownHostException e) {
00106             System.err.println("Unknown host IP address: " + args[0]);
00107             printSyntaxHelp();
00108             return;
00109         }
00110 
00111         Integer port;
00112         try {
00113             port = new Integer(args[1]);
00114         } catch (NumberFormatException e) {
00115             System.err.println("The PORT argument is not a valid integer number!");
00116             printSyntaxHelp();
00117             return;
00118         }
00119 
00120         Server server = new Server(ip, port.intValue(), args, 2, args.length - 1);
00121         server.run();        
00122     }
00123 
00125     public Server(InetAddress ip, int port, String[] options, int firstOptionIndex, int lastOptionIndex) {
00126         this.ip = ip;
00127         this.port = port;
00128         
00129         parseOptions(options, firstOptionIndex, lastOptionIndex);
00130 
00131         openSessions = Collections.synchronizedMap(new TreeMap());
00132     }
00133 
00134     protected void parseOptions(String[] args, int startIndex, int endIndex) {
00135         for (int iArgs = startIndex; iArgs <= endIndex; ++iArgs) {
00136             String opt = args[iArgs];
00137             if (opt.equals("-v")) {
00138                 verboseOutput = true;
00139                 System.out.println("Set option \"verbose output\" to TRUE");
00140             }
00141         }
00142     }
00143 
00153     public void run() {
00154         
00155         ServerSocket srvSocket;
00156 
00157         try {
00158             srvSocket = new ServerSocket(port, 5, ip);
00159         } catch (IOException e) {
00160             System.err.println("FATAL ERROR: server socket could not be created!");
00161             System.err.println("IOException message: " + e.getMessage());
00162             return;
00163         }
00164 
00165         System.out.println("Server socket created!");
00166 
00167         try {
00168             while(true) {
00169                 
00170                 Socket newSocket;
00171                 newSocket = null;
00172                 System.out.println("Server socket created!");
00173                 try {
00174                     newSocket = srvSocket.accept();
00175                 } catch (IOException e) {
00176                     System.err.println("FATAL ERROR: server not able to accept "
00177                                        + "incoming connections!");
00178                     System.err.println("IOException message: " + e.getMessage());
00179                     return;
00180                 }
00181                 
00182                 System.out.println("Incoming connection accepted!");
00183                 
00184                 try {
00185                     Connection newConn = new Connection(newSocket, openSessions, verboseOutput);
00186                     newConn.start();
00187                 } catch(Exception e) {
00188                     System.err.println(e.getMessage());
00189                     e.printStackTrace();
00190                     if ((newSocket != null) && (!newSocket.isClosed())) {
00191                         
00192                         try {
00193                             newSocket.close();
00194                         } catch (Exception e1) {
00195                         }
00196                     }
00197                 }
00198                 
00199             }  // while(true)
00200 
00201         } finally {
00202             try {
00203                 srvSocket.close();
00204             } catch (IOException eIO) {}
00205         }
00206     }  // run()
00207 
00208 }


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16