project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java
author Xeli
Tue, 09 Aug 2011 20:54:56 +0200
branchhedgeroid
changeset 5508 dcf1b3645af6
parent 5475 06a87ff38ffb
child 5621 ea796c83ea47
permissions -rw-r--r--
Fixed IPC provide gameconfig
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
     1
package org.hedgewars.mobile.EngineProtocol;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     2
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     3
import java.io.IOException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     4
import java.io.InputStream;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     5
import java.io.OutputStream;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     6
import java.net.ServerSocket;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     7
import java.net.Socket;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     8
import java.net.UnknownHostException;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     9
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    10
public class EngineProtocolNetwork implements Runnable{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    11
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    12
	public static final String GAMEMODE_LOCAL = "TL";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    13
	public static final String GAMEMODE_DEMO = "TD";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    14
	public static final String GAMEMODE_NET = "TN";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    15
	public static final String GAMEMODE_SAVE = "TS";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    16
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    17
	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
    18
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    19
	public static final int MODE_GENLANDPREVIEW = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    20
	public static final int MODE_GAME = 1;
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
	private ServerSocket serverSocket;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    23
	private InputStream input;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    24
	private OutputStream output;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    25
	public int port;
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    26
	private final GameConfig config;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    27
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    28
	public EngineProtocolNetwork(GameConfig _config){
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    29
		config = _config;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    30
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    31
			serverSocket = new ServerSocket(0);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    32
			port = serverSocket.getLocalPort();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    33
			Thread ipcThread = new Thread(this, "IPC - Thread");			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    34
			ipcThread.start();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    35
		} catch (UnknownHostException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    36
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    37
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    38
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    39
		}
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
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    42
	public void run(){
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    43
		//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    44
		/*else if (mode == MODE_GAME)*/ gameIPC();
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    45
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    46
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    47
	private void gameIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    48
		try{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
			Socket sock = serverSocket.accept();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
			input = sock.getInputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    51
			output = sock.getOutputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    52
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    53
			boolean clientQuit = false;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
			int msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
			byte[] buffer = new byte[BUFFER_SIZE];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    56
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    57
			while(!clientQuit){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    58
				msgSize = 0;
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
				input.read(buffer, 0, 1);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    61
				msgSize = buffer[0];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    62
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    63
				input.read(buffer, 0, msgSize);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    64
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    65
				switch(buffer[0]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    66
				case 'C'://game init
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    67
					config.sendToEngine(this);
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    68
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    69
				case '?'://ping - pong
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    70
					sendToEngine("!");
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    71
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    72
				case 'E'://error - quits game
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    75
				case 'e':
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    78
				case 'i'://game statistics
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    79
					switch(buffer[1]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    80
					case 'r'://winning team
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    81
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    82
					case 'D'://best shot
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    83
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    84
					case 'k'://best hedgehog
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    85
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    86
					case 'K'://# hogs killed
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    87
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    88
					case 'H'://team health graph
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    89
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    90
					case 'T':// local team stats
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    91
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    92
					case 'P'://teams ranking
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    93
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    94
					case 's'://self damage
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    95
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    96
					case 'S'://friendly fire
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    97
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    98
					case 'B'://turn skipped
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    99
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   100
					default:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   101
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   102
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   103
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   104
				case 'q'://game ended remove save file
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   105
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 'Q'://game ended but not finished
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   108
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   109
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   110
				}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   111
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   112
			}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   113
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   114
		}catch(IOException e){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   115
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   116
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   117
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   118
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
   119
	public void sendToEngine(String s){
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
		int length = s.length();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   121
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   122
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   123
			output.write(length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   124
			output.write(s.getBytes(), 0, length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   125
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   126
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   127
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   128
		
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
}