Android Open Source - android-chess Rest Server






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jwtc.android.chess.convergence;
/*www . j a  v a 2 s .  c o  m*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

import jwtc.chess.JNI;

import android.util.Log;

public class RestServer {
  private Thread m_thread;
  public static final String TAG = "RestServer";
  protected JNI _jni;
  protected int _portNumber;
  protected ServerSocket _serverSocket;
  protected boolean _run;
  
  public RestServer(int portNumber){
    //_jni = jni;
    _portNumber   = portNumber;
    _run       = true;
    _jni       = new JNI();
    _serverSocket  = null;
  }
  
  public boolean start(){
    
    _run = true;
    
    try{
      _serverSocket = new ServerSocket(_portNumber);
    } catch(Exception ex){
      _serverSocket = null;
      Log.e(TAG, ex.toString());
      return false;
    }
    
    m_thread = new Thread(new Runnable(){

       public void run() {
         int i = 0;
         try {
           Log.i(TAG, "Trying to open socket");
           
           
           while(_run){
             Log.i(TAG, "Waiting for client socket");
               Socket clientSocket = _serverSocket.accept();
               Log.i(TAG, "Accepted");
               PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
               BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
               Log.i(TAG, "Start read from client");
               
               boolean bValid = false;
               String inputLine;
               while ((inputLine = in.readLine()) != null) {
                Log.i(TAG, "Got [" + inputLine + "]");
                if(inputLine.indexOf("GET / ") == 0){
                  Log.i(TAG, "Valid request");
                 bValid = true;
                }
                if(inputLine.length() == 0){
                  break;
                }
               }
               String outputLine;
               if(bValid){
                 outputLine = "HTTP/1.0 200 OK\r\n\r\nChessCallBack({\"FEN\":\"" + _jni.toFEN() + "\"});\r\n\r\n";
                 i++;
               } else {
                 outputLine = "HTTP/1.0 200 OK\r\n\r\n-\r\n\r\n";
               }
               Log.i(TAG, "Writing response " + i);
               out.println(outputLine);
               clientSocket.close();
               Log.i(TAG, "Closed client socket");
           }
           _serverSocket.close();
         } catch(Exception ex){
           Log.e(TAG, "Exception " + ex);
         }
       }
    });
    
    m_thread.start();
    
    return true;
  }
  
  public boolean isAlive(){
    if(m_thread == null){
      Log.i(TAG, "isAlive -> thread = null");
      return false;
    }
    if(m_thread.isAlive()){
      Log.i(TAG, "isAlive -> thread = " + m_thread.isAlive() + ", serversocket " + (_serverSocket != null));
      return  _serverSocket != null && (false == _serverSocket.isClosed());
    }
    return false;
  }
  
  public void stop(){
    _run = false;
    if(_serverSocket != null && (false == _serverSocket.isClosed())){
      try{
        _serverSocket.close();
      }catch(Exception ex){}
    }
    _serverSocket = null;
    m_thread = null;
  }
  
  public static String getIpAddress() { 
        try {
            for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = (NetworkInterface)en.nextElement();
                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = (InetAddress)enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        String ipAddress=inetAddress.getHostAddress().toString();
                        Log.i("IP address",""+ipAddress);
                        if(ipAddress.startsWith("192.168.") || ipAddress.startsWith("172.16.") || ipAddress.startsWith("10.0.")){ 
                          return ipAddress;
                        }
                    }
                }
            }
        } catch (Exception ex) {
            Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
        }
        return null; 
  }
}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java