project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/EngineProtocolNetwork.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7855 ddcdedd3330b
parent 7695 6237d2f002ba
child 7859 519d5bc91dd3
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 
       
    20 package org.hedgewars.hedgeroid.EngineProtocol;
       
    21 
       
    22 import java.io.IOException;
       
    23 import java.io.InputStream;
       
    24 import java.io.OutputStream;
       
    25 import java.net.ServerSocket;
       
    26 import java.net.Socket;
       
    27 import java.net.UnknownHostException;
       
    28 
       
    29 public class EngineProtocolNetwork extends Thread{
       
    30 
       
    31 	public static final String GAMEMODE_LOCAL = "TL";
       
    32 	public static final String GAMEMODE_DEMO = "TD";
       
    33 	public static final String GAMEMODE_NET = "TN";
       
    34 	public static final String GAMEMODE_SAVE = "TS";
       
    35 	
       
    36 	public static final int BUFFER_SIZE = 255; //From iOS code which got it from the origional frontend
       
    37 	
       
    38 	public static final int MODE_GENLANDPREVIEW = 0;
       
    39 	public static final int MODE_GAME = 1;
       
    40 
       
    41 	private ServerSocket serverSocket;
       
    42 	private InputStream input;
       
    43 	private OutputStream output;
       
    44 	public int port;
       
    45 	private final GameConfig config;
       
    46 	private boolean clientQuit = false;
       
    47 
       
    48 	public EngineProtocolNetwork(GameConfig _config){
       
    49 		config = _config;
       
    50 		try {
       
    51 			serverSocket = new ServerSocket(0);
       
    52 			port = serverSocket.getLocalPort();
       
    53 			Thread ipcThread = new Thread(this, "IPC - Thread");			
       
    54 			ipcThread.start();
       
    55 		} catch (UnknownHostException e) {
       
    56 			e.printStackTrace();
       
    57 		} catch (IOException e) {
       
    58 			e.printStackTrace();
       
    59 		}
       
    60 	}
       
    61 	
       
    62 	public void run(){
       
    63 		//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
       
    64 		/*else if (mode == MODE_GAME)*/ gameIPC();
       
    65 	}
       
    66 	
       
    67 	private void gameIPC(){
       
    68 		Socket sock = null;
       
    69 		try{
       
    70 			sock = serverSocket.accept();
       
    71 			input = sock.getInputStream();
       
    72 			output = sock.getOutputStream();
       
    73 			
       
    74 			int msgSize = 0;
       
    75 			byte[] buffer = new byte[BUFFER_SIZE];
       
    76 
       
    77 			while(!clientQuit){
       
    78 				msgSize = 0;
       
    79 
       
    80 				input.read(buffer, 0, 1);
       
    81 				msgSize = buffer[0];
       
    82 
       
    83 				input.read(buffer, 0, msgSize);
       
    84 				System.out.println("IPC" + (char)buffer[0] + " : " + new String(buffer, 1,msgSize-1, "US_ASCII"));
       
    85 				switch(buffer[0]){
       
    86 				case 'C'://game init
       
    87 					config.sendToEngine(this);
       
    88 					break;
       
    89 				case '?'://ping - pong
       
    90 					sendToEngine("!");
       
    91 					break;
       
    92 				case 'e'://Send protocol version
       
    93 					System.out.println(new String(buffer));
       
    94 					break;
       
    95 				case 'i'://game statistics
       
    96 					switch(buffer[1]){
       
    97 					case 'r'://winning team
       
    98 						break;
       
    99 					case 'D'://best shot
       
   100 						break;
       
   101 					case 'k'://best hedgehog
       
   102 						break;
       
   103 					case 'K'://# hogs killed
       
   104 						break;
       
   105 					case 'H'://team health graph
       
   106 						break;
       
   107 					case 'T':// local team stats
       
   108 						break;
       
   109 					case 'P'://teams ranking
       
   110 						break;
       
   111 					case 's'://self damage
       
   112 						break;
       
   113 					case 'S'://friendly fire
       
   114 						break;
       
   115 					case 'B'://turn skipped
       
   116 						break;
       
   117 					default:
       
   118 					}
       
   119 					break;
       
   120 				case 'E'://error - quits game
       
   121 					System.out.println(new String(buffer));
       
   122 					return;
       
   123 				case 'q'://game ended remove save file
       
   124 
       
   125 				    return;
       
   126 				case 'Q'://game ended but not finished
       
   127 
       
   128 					return;
       
   129 				}
       
   130 
       
   131 			}
       
   132 		}catch(IOException e){
       
   133 			e.printStackTrace();
       
   134 		}finally{
       
   135 			try {
       
   136 				if(sock != null) sock.close();
       
   137 			} catch (IOException e) {}
       
   138 			try{
       
   139 				if(serverSocket != null) serverSocket.close();
       
   140 			} catch (IOException e) {}
       
   141 		}
       
   142 	}
       
   143 
       
   144 	public void sendToEngine(String s){
       
   145 		int length = s.length();
       
   146 		
       
   147 		try {
       
   148 			output.write(length);
       
   149 			output.write(s.getBytes(), 0, length);
       
   150 		} catch (IOException e) {
       
   151 			e.printStackTrace();
       
   152 		}
       
   153 	}
       
   154 	
       
   155 	public void quitIPC(){
       
   156 		clientQuit = true;
       
   157 	}
       
   158 	
       
   159 }