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 snifos.application.test;
21  
22  import org.apache.log4j.Logger;
23  
24  import org.custommonkey.xmlunit.Diff;
25  
26  import org.dom4j.Document;
27  import org.dom4j.DocumentHelper;
28  
29  import snifos.application.Application;
30  
31  import snifos.common.UserId;
32  
33  import snifos.common.test.DummyMessages;
34  
35  import snifos.exception.InvalidUserException;
36  
37  import snifos.server.Server;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.Properties;
42  
43  
44  /***
45   * This is simple implementation of Application interface. It's configuration
46   * should contain only one property named "userId" which value should be
47   * integer number. This application accepts user only when user id in user's
48   * communication module can be divided by "userId" without remainder.
49   *
50   * @version $Id: DummyApplication.java,v 1.2 2004/05/08 21:55:30 mwerla Exp $
51   */
52  public class DummyApplication implements Application {
53      private static Logger logger = Logger.getLogger(DummyApplication.class);
54      private Server server;
55      private int moduloFactor = 1;
56      private Collection registeredUsers = new ArrayList();
57  
58      /***
59       * Just dummy constructor - does nothing.
60       */
61      public DummyApplication() {
62          logger.info("Application created " + this);
63      }
64  
65      /***
66       * @see Application#configure(Properties, boolean)
67       */
68      public void configure(Properties configuration, boolean isUnique) {
69          if (configuration.getProperty("userId") != null) {
70              moduloFactor = Integer.parseInt(configuration.getProperty("userId"));
71          }
72  
73          logger.info("Application configured. Modulo factor = " + moduloFactor);
74      }
75  
76      /***
77       * @see Application#setServer(Server)
78       */
79      public void setServer(Server server) {
80          logger.info("Server set in application");
81          this.server = server;
82      }
83  
84      /***
85       * @see Application#unregisterUser(UserId)
86       */
87      public void unregisterUser(UserId userId) {
88          registeredUsers.remove(userId);
89          logger.info("User " + userId + " unregistered from application");
90      }
91  
92      /***
93       * @todo This method should be rewritten in some flexible way...
94       * @see Application#receiveMessage(UserId, Document)
95       */
96      public void receiveMessage(UserId userId, Document message) {
97          logger.info("Message \"" + message.asXML() + "\" from " + userId +
98              " recevied (" + moduloFactor + ")");
99  
100         try {
101             if (registeredUsers.contains(userId)) {
102                 Diff d = new Diff(message.asXML(), DummyMessages.requests[1]);
103                 assert d.identical();
104             } else {
105                 Diff d = new Diff(message.asXML(), DummyMessages.requests[0]);
106                 assert d.identical();
107                 server.sendMessage(userId,
108                     DocumentHelper.parseText(DummyMessages.responses[0]));
109                 server.sendMessage(userId,
110                     DocumentHelper.parseText(DummyMessages.responses[1]));
111                 registeredUsers.add(userId);
112             }
113         } catch (Exception e) {
114             throw new RuntimeException(e);
115         }
116     }
117 
118     /***
119     * Method not implemented.
120     * @see java.lang.Runnable#run()
121     */
122     public void run() {
123         //There is nothing needed here.
124     }
125 
126     /***
127     * This application accepts user only when user id in user's
128      * communication module can be divided by "userId" configuration parameter
129     * without remainder.
130     * @see Application#registerUser(UserId, Document)
131     */
132     public void registerUser(UserId userId, Document message)
133         throws InvalidUserException {
134         if ((userId.getUserIdInModule() % moduloFactor) != 0) {
135             throw new InvalidUserException();
136         }
137     }
138 }