project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java
author Xeli
Tue, 09 Aug 2011 20:53:37 +0200
branchhedgeroid
changeset 5506 2b0c4fcde4c6
parent 5475 06a87ff38ffb
child 5508 dcf1b3645af6
permissions -rw-r--r--
Added color and team hog count
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
import android.util.Log;
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 class EngineProtocolNetwork implements Runnable{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    13
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    14
	public static final String GAMEMODE_LOCAL = "TL";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    15
	public static final String GAMEMODE_DEMO = "TD";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    16
	public static final String GAMEMODE_NET = "TN";
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    17
	public static final String GAMEMODE_SAVE = "TS";
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 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
    20
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    21
	public static final int MODE_GENLANDPREVIEW = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    22
	public static final int MODE_GAME = 1;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    23
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    24
	private ServerSocket serverSocket;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    25
	private InputStream input;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    26
	private OutputStream output;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    27
	public int port;
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    28
	private final GameConfig config;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    29
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    30
	public EngineProtocolNetwork(GameConfig _config){
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    31
		config = _config;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    32
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    33
			serverSocket = new ServerSocket(0);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    34
			port = serverSocket.getLocalPort();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    35
			Thread ipcThread = new Thread(this, "IPC - Thread");			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    36
			ipcThread.start();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    37
		} catch (UnknownHostException 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
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    40
			e.printStackTrace();
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
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    43
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    44
	public void run(){
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    45
		//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    46
		/*else if (mode == MODE_GAME)*/ gameIPC();
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    47
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    48
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
	private void genLandPreviewIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    51
	}
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
	private void gameIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
		try{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
			Socket sock = serverSocket.accept();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    56
			input = sock.getInputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    57
			output = sock.getOutputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    58
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    59
			boolean clientQuit = false;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    60
			int msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    61
			byte[] buffer = new byte[BUFFER_SIZE];
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
			while(!clientQuit){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    64
				msgSize = 0;
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
				input.read(buffer, 0, 1);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    67
				msgSize = buffer[0];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    68
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    69
				input.read(buffer, 0, msgSize);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    70
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    71
				switch(buffer[0]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    72
				case 'C'://game init
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    73
					config.sendToEngine(this);
5433
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 '?'://ping - pong
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    76
					sendToEngine("!");
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 'E'://error - quits game
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    81
				case 'e':
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    84
				case 'i'://game statistics
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    85
					switch(buffer[1]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    86
					case 'r'://winning team
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 'D'://best shot
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 'k'://best hedgehog
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 'K'://# hogs killed
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 'H'://team health graph
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 'T':// local team stats
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 'P'://teams ranking
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
					case 's'://self damage
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   101
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   102
					case 'S'://friendly fire
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 'B'://turn skipped
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   105
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   106
					default:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   107
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
				case 'q'://game ended remove save file
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   113
				case 'Q'://game ended but not finished
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   114
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   115
					break;
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
			}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   119
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
		}catch(IOException e){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   121
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   122
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   123
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   124
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
   125
	public void sendToEngine(String s){
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   126
		int length = s.length();
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
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   129
			output.write(length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   130
			output.write(s.getBytes(), 0, length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   131
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   132
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   133
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   134
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   135
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   136
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   137
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   138
}