View Javadoc

1   /*
2    * AI Soccer Project - network gaming environment for AI warriors.
3    * Copyright (C) 2001-2004  Marcin Werla, Pawel Widera
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, you can find it here:
17   * http://www.gnu.org/licenses/gpl.html
18   */
19  
20  package aigames.soccer.communication;
21  
22  import snifos.common.UserId;
23  
24  import java.nio.channels.SocketChannel;
25  
26  
27  /***
28   * Data associated with accepted client.
29   * @version $Id: ClientData.java,v 1.4 2004/05/08 21:55:26 mwerla Exp $
30   */
31  public class ClientData {
32      private SocketChannel socket;
33      private StringBuffer buffer;
34      private UserId userId;
35  
36      /***
37       * Deafult constructor.
38       * @param socket socket channel
39       * @param userId UserId object
40       */
41      public ClientData(SocketChannel socket, UserId userId) {
42          this.socket = socket;
43          this.buffer = new StringBuffer();
44          this.userId = userId;
45      }
46  
47      /***
48       * Appends a line to the client message buffer.
49       * @param message string from a socket
50       */
51      public void append(String message) {
52          buffer.append(message);
53      }
54  
55      /***
56       * Getter for client message buffer.
57       * @return message buffer
58       */
59      public StringBuffer getBuffer() {
60          return buffer;
61      }
62  
63      /***
64       * Getter for socket channel.
65       * @return socket channel
66       */
67      public SocketChannel getSocket() {
68          return socket;
69      }
70  
71      /***
72       * Gettero for user id.
73       * @return UserId object
74       */
75      public UserId getUserId() {
76          return userId;
77      }
78  
79      /***
80       * Clear processed part of the message buffer.
81       * @param position last processed byte of the buffer
82       */
83      public void clearBuffer(int position) {
84          String rest = buffer.substring(position);
85          buffer = new StringBuffer(rest);
86      }
87  }