Coverage Report - org.jtheque.core.managers.lifecycle.Client
 
Classes in this File Line Coverage Branch Coverage Complexity
Client
0 %
0/22
0 %
0/4
2.667
 
 1  
 package org.jtheque.core.managers.lifecycle;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.log.ILoggingManager;
 21  
 import org.jtheque.core.managers.view.able.IViewManager;
 22  
 import org.jtheque.utils.io.FileUtils;
 23  
 
 24  
 import java.io.BufferedReader;
 25  
 import java.io.DataInputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStreamReader;
 28  
 import java.net.Socket;
 29  
 
 30  
 /**
 31  
  * Represent a network client. It's an instance of JTheque who listen to new open of the program.
 32  
  *
 33  
  * @author Baptiste Wicht
 34  
  */
 35  
 final class Client extends Thread {
 36  
     private BufferedReader reader;
 37  
     private final Socket socket;
 38  
 
 39  
     /**
 40  
      * Construct a new Client.
 41  
      *
 42  
      * @param socket The socket to listen the data.
 43  
      */
 44  
     @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
 45  
     Client(Socket socket) {
 46  0
         super();
 47  
 
 48  0
         this.socket = socket;
 49  
 
 50  
         try {
 51  0
             reader = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream())));
 52  0
         } catch (IOException e) {
 53  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 54  0
         }
 55  0
     }
 56  
 
 57  
     @Override
 58  
     public void run() {
 59  0
         while (!socket.isClosed()) {
 60  
             try {
 61  0
                 if ("open".equals(reader.readLine())) {
 62  0
                     Managers.getManager(IViewManager.class).getViews().getMainView().toFirstPlan();
 63  
 
 64  0
                     interrupt();
 65  
                 }
 66  0
             } catch (IOException e) {
 67  0
                 Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 68  0
             }
 69  
         }
 70  0
     }
 71  
 
 72  
     @Override
 73  
     public void interrupt() {
 74  0
         super.interrupt();
 75  
 
 76  0
         FileUtils.close(reader);
 77  
 
 78  
         try {
 79  0
             socket.close();
 80  0
         } catch (IOException e) {
 81  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 82  0
         }
 83  0
     }
 84  
 }