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