project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java
author Xeli
Sun, 21 Aug 2011 16:25:18 +0200
branchhedgeroid
changeset 5621 ea796c83ea47
parent 5508 dcf1b3645af6
child 5943 5b2b86a37089
permissions -rw-r--r--
added licenses
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
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    29
public class EngineProtocolNetwork implements Runnable{
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;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    46
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    47
	public EngineProtocolNetwork(GameConfig _config){
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    48
		config = _config;
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    49
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    50
			serverSocket = new ServerSocket(0);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    51
			port = serverSocket.getLocalPort();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    52
			Thread ipcThread = new Thread(this, "IPC - Thread");			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    53
			ipcThread.start();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    54
		} catch (UnknownHostException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    55
			e.printStackTrace();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    56
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    57
			e.printStackTrace();
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
	}
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
	public void run(){
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    62
		//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    63
		/*else if (mode == MODE_GAME)*/ gameIPC();
5433
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
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    66
	private void gameIPC(){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    67
		try{
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    68
			Socket sock = serverSocket.accept();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    69
			input = sock.getInputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    70
			output = sock.getOutputStream();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    71
			
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    72
			boolean clientQuit = false;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    73
			int msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    74
			byte[] buffer = new byte[BUFFER_SIZE];
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
			while(!clientQuit){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    77
				msgSize = 0;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    78
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    79
				input.read(buffer, 0, 1);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    80
				msgSize = buffer[0];
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
				input.read(buffer, 0, msgSize);
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
				switch(buffer[0]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    85
				case 'C'://game init
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
    86
					config.sendToEngine(this);
5433
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 '?'://ping - pong
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    89
					sendToEngine("!");
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    90
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    91
				case 'E'://error - quits game
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    92
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 'e':
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    95
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    96
					break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    97
				case 'i'://game statistics
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    98
					switch(buffer[1]){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
    99
					case 'r'://winning team
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 'D'://best shot
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'://best hedgehog
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 'K'://# hogs killed
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 'H'://team health graph
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 'T':// local team stats
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 'P'://teams ranking
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'://self damage
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 'S'://friendly fire
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
					case 'B'://turn skipped
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   118
						break;
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   119
					default:
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   120
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
					break;
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
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   125
					break;
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
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   128
					break;
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
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   133
		}catch(IOException e){
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   134
			e.printStackTrace();
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
5475
06a87ff38ffb The start of the IPC transers to the engine
Xeli
parents: 5433
diff changeset
   138
	public void sendToEngine(String s){
5433
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   139
		int length = s.length();
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   140
		
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   141
		try {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   142
			output.write(length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   143
			output.write(s.getBytes(), 0, length);
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   144
		} catch (IOException e) {
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   145
			e.printStackTrace();
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
	}
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   150
	
8f82045953c1 Main class files for the start-local-game screen
Xeli
parents:
diff changeset
   151
}