project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocolNetwork.java
author Xeli
Thu, 14 Jul 2011 15:59:29 +0200
branchhedgeroid
changeset 5437 19a7b798f73a
parent 5433 8f82045953c1
permissions -rw-r--r--
Removed double icons introduced in 5411:9b82f4f9fba6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
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 int mode = -1;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    25
	private ServerSocket serverSocket;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    26
	private InputStream input;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    27
	private OutputStream output;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    28
	public int port;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    29
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    30
	public EngineProtocolNetwork(int _mode){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    31
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    32
			mode = _mode;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    33
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    34
			serverSocket = new ServerSocket(0);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    35
			port = serverSocket.getLocalPort();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    36
			Thread ipcThread = new Thread(this, "IPC - Thread");			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    37
			ipcThread.start();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    38
		} catch (UnknownHostException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    39
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    40
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    41
			e.printStackTrace();
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 EngineProtocolNetwork(String uuid){
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
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    48
	public void run(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
		if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
		else if (mode == MODE_GAME) gameIPC();
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 genLandPreviewIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
	}
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
	private void gameIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    58
		try{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    59
			Socket sock = serverSocket.accept();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    60
			input = sock.getInputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    61
			output = sock.getOutputStream();
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
			boolean clientQuit = false;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    64
			int msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    65
			byte[] buffer = new byte[BUFFER_SIZE];
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
			while(!clientQuit){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    68
				msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    69
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    70
				input.read(buffer, 0, 1);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    71
				msgSize = buffer[0];
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    72
				Log.e("bla", "bla" + msgSize + " + " + buffer[0] + " + " + buffer[1]);
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
				input.read(buffer, 0, msgSize);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    75
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    76
				switch(buffer[0]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    77
				case 'C'://game init
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    78
					Log.e("bla", "send init");
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
					sendToEngine(GAMEMODE_LOCAL);//Start localgame
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    81
					
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    82
					//seed info
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    83
					
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    84
					
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 '?'://ping - pong
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    87
					sendToEngine("!");
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 'E'://error - quits game
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    90
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 'e':
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    93
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
					}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   121
				case 'q'://game ended remove save file
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
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   124
				case 'Q'://game ended but not finished
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   125
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   126
					break;
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
		}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
	private void sendToEngine(String s){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   137
		int length = s.length();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   138
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   139
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   140
			output.write(length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   141
			output.write(s.getBytes(), 0, length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   142
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   143
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   144
		}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   145
		
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
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   148
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   149
}