22 package com.realtime.crossfire.jxclient.server.crossfire;
24 import java.io.EOFException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.ServerSocket;
29 import java.net.Socket;
30 import java.nio.charset.Charset;
31 import java.util.concurrent.Semaphore;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 import org.junit.Assert;
35 import org.junit.Test;
47 private Semaphore
sem;
55 @Test(timeout = 30000)
57 sem =
new Semaphore(0);
63 connection.
connect(
"localhost", port);
87 final ServerSocket server =
new ServerSocket(0);
88 final Thread thread =
new Thread(
new Runnable() {
94 private final Charset charset = Charset.forName(
"ISO-8859-1");
106 final byte[] data = readPacket(in);
111 for (paramsIndex = 0; paramsIndex < data.length; paramsIndex++) {
112 if (data[paramsIndex] == (byte)
' ') {
116 final String cmd =
new String(data, 0, paramsIndex, charset);
117 if (paramsIndex < data.length && data[paramsIndex] == (byte)
' ') {
122 writeString(out,
"version 1 1 info");
126 processSetup(out,
new String(data, paramsIndex, data.length-paramsIndex, charset));
130 processRequestinfo(out,
new String(data, paramsIndex, data.length-paramsIndex, charset));
133 case "toggleextendedtext":
142 Assert.fail(
"received unexpected command: "+cmd);
147 }
catch (
final IOException ex) {
148 Assert.fail(ex.getMessage());
149 throw new AssertionError(ex);
160 private byte[] readPacket(@NotNull
final InputStream in)
throws EOFException {
164 }
catch (
final EOFException ignored) {
167 final int packetLen = tmp*0x100+readByte(in);
168 final byte[] data =
new byte[packetLen];
171 while (pos < data.length) {
172 final int len = in.read(data, pos, data.length-pos);
174 throw new EOFException(
"unexpected end of file reached");
178 }
catch (
final IOException ex) {
179 Assert.fail(ex.getMessage());
180 throw new AssertionError(ex);
191 private void processSetup(@NotNull
final OutputStream out, @NotNull
final String params)
throws IOException {
192 final String[] params2 = params.split(
" ", -1);
193 Assert.assertEquals(0, params2.length%2);
194 final StringBuilder sb =
new StringBuilder(
"setup");
195 for (
int i = 0; i < params2.length; i += 2) {
196 final String key = params2[i];
197 final String value = params2[i+1];
198 if (key.equals(
"map2cmd") || key.equals(
"newmapcmd") || key.equals(
"facecache") || key.equals(
"extendedTextInfos") || key.equals(
"itemcmd") || key.equals(
"spellmon") || key.equals(
"tick") || key.equals(
"num_look_objects") || key.equals(
"mapsize")) {
199 sb.append(
" ").append(key).append(
" ").append(value);
201 sb.append(
" ").append(key).append(
" FALSE");
204 writeString(out, sb.toString());
213 private void processRequestinfo(@NotNull
final OutputStream out, @NotNull
final String params)
throws IOException {
214 if (params.equals(
"exp_table")) {
215 writeBytes(out,
new byte[] {
249 private void processAddme(@NotNull
final OutputStream out)
throws IOException {
250 writeString(out,
"query 0 What is your name?");
251 writeString(out,
"addme_success");
262 private int readByte(@NotNull
final InputStream in)
throws EOFException {
266 }
catch (
final IOException ex) {
267 Assert.fail(ex.getMessage());
268 throw new AssertionError(ex);
271 throw new EOFException(
"EOF");
282 private void writeString(@NotNull
final OutputStream out, @NotNull
final String s)
throws IOException {
283 writeBytes(out, s.getBytes(charset));
292 private void writeBytes(@NotNull
final OutputStream out, @NotNull
final byte[] b)
throws IOException {
293 final int len = b.length;
294 out.write(len/0x100);
301 return server.getLocalPort();
310 private static Socket
acceptClient(@NotNull
final ServerSocket server) {
314 client = server.accept();
315 }
catch (
final IOException ex) {
316 Assert.fail(ex.getMessage());
317 throw new AssertionError(ex);
329 final InputStream in;
331 in = socket.getInputStream();
332 }
catch (
final IOException ex) {
333 Assert.fail(ex.getMessage());
334 throw new AssertionError(ex);
346 final OutputStream out;
348 out = socket.getOutputStream();
349 }
catch (
final IOException ex) {
350 Assert.fail(ex.getMessage());
351 throw new AssertionError(ex);
void setPreferredNumLookObjects(final int preferredNumLookObjects)
Sets the maximum number of objects in the ground view.Must not be called in connected state...
static OutputStream getOutputStream(@NotNull final Socket socket)
Returns the OutputStream of a Socket.
Combines all model classes that are updated.
void waitForCurrentNumLookObjectsValid()
Waits until getCurrentNumLookObjects() is stable.
static InputStream getInputStream(@NotNull final Socket socket)
Returns the InputStream of a Socket.
Regression tests for DefaultCrossfireServerConnection.
static Socket acceptClient(@NotNull final ServerSocket server)
Accepts a single client from a ServerSocket.
int getCurrentNumLookObjects()
Returns the currently negotiated setup value of "num_look_objects".
Default implementation of CrossfireServerConnection.
int startServer()
Starts a dummy Crossfire server.
void stop()
Stops operation.if stopping was interrupted
void testNegotiateNumLookObjects1()
Checks that DefaultCrossfireServerConnection#setPreferredNumLookObjects(int) queues multiple updates...
Semaphore sem
The Semaphore for waiting until character login.
void start()
Starts operation.
void connect(@NotNull final String hostname, final int port)
Attempts to connect the client to a server.the hostname to connect to the port to connect to ...