Project

General

Profile

Revision 603

Added by Greg Tress about 16 years ago

More HTTP access files

View differences:

trunk/code/projects/colonet/client/cgi/ColonetCGI.java
1

  
2
public class ColonetCGI {
3

  
4
	public static void main (String [] args) {
5
		if (args.length < 1)
6
			return;
7
		ColonetCGI ccgi = new ColonetCGI(args[0]);
8
	}
9
	
10
	public ColonetCGI (String strCommand) {
11
		ColonetCGIServerInterface ccsi = new ColonetCGIServerInterface(this);
12
		ccsi.connect("128.2.99.176", "10123");
13
		ccsi.sendData(ColonetCGIServerInterface.MOTOR2_SET + " 0 225", "200");
14
		ccsi.sendData(ColonetCGIServerInterface.MOTOR1_SET + " 0 225", "200");
15
	
16
	}
17

  
18
}
trunk/code/projects/colonet/client/cgi/ColonetCGIServerInterface.java
1
/*
2
* ColonetCGIServerInterface.java
3
* Gregory Tress
4
*/
5

  
6
import java.net.*;
7
import java.io.*;
8
import javax.swing.JOptionPane;
9

  
10

  
11
public class ColonetCGIServerInterface
12
{
13
/*	Old packet structure:
14

  
15
		COMMAND PACKET STRUCTURE
16
	1:	SEND_TO_ROBOT
17
	2:	# of robot, or GLOBAL_DEST
18
	3:	COLONET_COMMMAND
19
	4:	message code (i.e. ORB_SET)
20
	5:	any data, as many that fit in the packet
21

  
22
	REQUEST PACKET STRUCTURE
23
	1:	REQUEST_FROM_SERVER
24
	2:	# of robot
25
	3:	COLONET_REQUEST
26
	4:	???
27

  
28
	9/12/07 New server interface structure
29
		Client will no longer send full robot packets to the server.
30
		Commands will be defined as necessary.
31
	*/
32

  
33
	//General Colonet Interface
34
	public static final String SEND_TO_ROBOT = "0";
35
	public static final String REQUEST_FROM_SERVER = "1";
36
	public static final String RESPONSE_TO_CLIENT_REQUEST = "2";
37
	public static final String REQUEST_BOM_MATRIX = "144";
38
	public static final String REQUEST_XBEE_IDS = "145";
39

  
40
	public static final String COLONET_COMMAND = "13"; //0x0D
41
	public static final String COLONET_REQUEST = "14"; //0x0E
42
	public static final String CORONET_RESPONSE = "15"; //0x0F
43
	public static final String GLOBAL_DEST = "200";
44
	public static final String CLIENT_REQUEST_ROBOT_POSITIONS = "86";
45
	public static final String CLIENT_ASSIGN_ROBOT_ID = "87";
46
	public static final String MOVE_TO_ABSOLUTE_POSITION = "83"; //0x53
47

  
48
	//Queue instructions
49
	public static final String COLONET_QUEUE = "100";
50
	public static final String QUEUE_UPDATE = "101";
51
	public static final String QUEUE_ADD = "102";
52
	public static final String QUEUE_REMOVE = "103";
53
	public static final String QUEUE_REORDER = "104";
54

  
55
	//Use BATTERY to request battery level
56
	public static final String BATTERY = "56"; //0x38
57

  
58
	//MOTORS
59
	public static final String MOTORS_INIT = "23"; //0x17
60
	public static final String MOTOR1_SET = "24"; //0x18
61
	public static final String MOTOR2_SET = "25"; //0x19
62
	public static final String MOTORS_OFF = "26"; //0x1A
63
	public static final String MOVE = "27"; //0x1B
64
	public static final String MOVE_AVOID = "28"; //0x1C
65

  
66
	//BUZZER
67
	public static final String BUZZER_INIT = "0"; //0x00
68
	public static final String BUZZER_SET_VAL = "1"; //0x01
69
	public static final String BUZZER_SET_FREQ = "2"; //0x02
70
	public static final String BUZZER_CHIRP = "3"; //0x03
71
	public static final String BUZZER_OFF = "4"; //0x04
72

  
73
	//ORB
74
	public static final String ORB_INIT = "12"; //0x0C
75
	public static final String ORB_SET = "13"; //0x0D
76
	public static final String ORB_SET_COLOR = "14"; //0x0E
77
	public static final String ORB_DISABLE = "15"; //0x0F
78
	public static final String ORB_ENABLE = "16"; //0x10
79
	public static final String ORB_SET_DIO = "17"; //0x11
80
	public static final String LED_INIT = "18"; //0x12
81
	public static final String LED_USER = "19"; //0x13
82
	public static final String ORB_SET_NUM_NS = "20"; //0x14
83
	public static final String ORB_SET_NUM = "21"; //0x15
84
	public static final String ORB_SEND = "22"; //0x16
85

  
86
	ColonetCGI colonet;      //save reference to the entire applet locally
87
	Socket socket;
88
	OutputStreamWriter out;
89
	BufferedReader reader;
90
	//DataListener dataListener;
91

  
92
	/*
93
	* FUNCTION IMPLEMENTATIONS
94
	*/
95

  
96
	/**
97
	* Constructs a new ColonetCGIServerInterface. When constructing a ColonetCGIServerInterface, a valid Colonet object
98
	* reference must be provided to ensure that data is routed correctly.
99
	*
100
	* @param colonet The Colonet object to save locally. This reference cannot be changed once the
101
	*		ColonetSreverInterface has been contsructed.
102
	* @throws NullPointerException if colonet is null
103
	*
104
	*/
105
	public ColonetCGIServerInterface (ColonetCGI colonet) {
106
		this.colonet = colonet;
107
	}
108

  
109
	public boolean isReady () {
110
		if (socket == null || out == null || reader == null)
111
			return false;
112
		if (!socket.isConnected() || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown())
113
			return false;
114
		return true;
115
	}
116

  
117
	public boolean isInputReady () {
118
		try {
119
			if (reader.ready()) return true;
120
		} catch (Exception e) {
121
			return false;
122
		}
123
		return false;
124
	}
125

  
126
	public String getLine () throws IOException {
127
		return reader.readLine();
128
	}
129

  
130
	/**
131
	 * Create socket connection to Colonet server.
132
	 * If successful, start thread for listening for incoming data.
133
	 */
134
	public void connect (String strHost, String strPort) {
135
		//make sure hostname and port are valid
136
		if (strHost.equals("") || strPort.equals("")) {
137
			return;
138
		}
139
		int port = 0;
140
		try {
141
			port = Integer.parseInt(strPort);
142
		} catch (Exception e) {
143
			return;
144
		}
145

  
146
		//make sure we aren't already connected.
147
		if (socket != null && socket.isConnected()) {
148
			return;
149
		}
150

  
151
		try {
152
			socket = new Socket(strHost, port);
153
			socket.setKeepAlive(true);
154
		} catch (UnknownHostException e) {
155
			return;
156
		} catch (IOException e) {
157
			return;
158
		} catch (java.security.AccessControlException e) {
159
			return;
160
		}
161

  
162
		if (socket == null || !socket.isConnected()) {
163
			return;
164
		}
165
		try {
166
			out = new OutputStreamWriter(socket.getOutputStream());
167
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
168
		} catch (IOException e) {
169
			return;
170
		}
171

  
172
	}
173

  
174
	public void disconnect () {
175
		try {
176
			if (reader != null) {
177
				reader.close();
178
			}
179
			if (out != null) {
180
				out.close();
181
			}
182
		} catch (IOException e) {
183
		}
184
	}
185

  
186
	private void sendString (String s) {
187
		//make sure we can send
188
		if (!this.isReady()) {
189
			return;
190
		}
191
		//send packet
192
		try {
193
			Thread.sleep(50);	 //pause to be safe
194
			out.write(s);
195
			out.flush();
196
			//log.append("Sent: " + s + "\n");
197
		} catch (IOException e) {
198
		} catch (InterruptedException e) {
199
		}
200
	}
201

  
202
	public void sendData (String s, String robotNumber) {
203
		//create packet
204
		String packet = "";
205
		packet += ColonetCGIServerInterface.SEND_TO_ROBOT;
206
		if (robotNumber != null)
207
			packet += " " + robotNumber;
208
		packet += " " + ColonetCGIServerInterface.COLONET_COMMAND;
209
		packet += " " + s;	//add	 the command code here
210
		packet += "\n";
211
		sendString(packet);
212
	}
213

  
214

  
215

  
216

  
217

  
218

  
219
}

Also available in: Unified diff