Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: demo/tucson/chat/ChatAgent.java


1   /*
2    * Chat TuCSoN Demo - Copyright (C) 2001 deis.unibo.it
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  package demo.tucson.chat;
19  
20  import alice.tucson.*;
21  import alice.logictuple.*;
22  import java.util.*;
23  
24  /**
25   * the Chat agent acts as interface between 
26   * the user and the room, in particular sending
27   * messages to room and collecting updates from
28   * the room
29   *
30   * @author Alessandro Ricci
31   *
32   */
33  public class ChatAgent extends alice.tucson.Agent {
34  
35      TupleCentreId  chatId;
36      GUIEvent    event=new GUIEvent();
37      String      name;
38      AgentId   obsAgentId;
39  
40      public ChatAgent(String name,AgentId aid, AgentId obsAgentId,TupleCentreId tid){
41          super(aid);
42          this.name=name;
43          this.obsAgentId=obsAgentId;
44          chatId=tid;
45      }
46  
47      public void run(){
48          try {
49              // ask to enter the chat
50              LogicTuple res=inp(chatId,new LogicTuple("join",
51                              new Value(getId().toString()),
52                              new Value(obsAgentId.toString()),
53                              new Value(name)));
54              if (res==null){
55                  // enter failed
56                  System.err.println("Name "+name+" already used.");
57                  System.exit(-1);
58              }
59              
60              // ok, we are in the chat
61              while (true){
62                  
63                  // wait for user input
64                  synchronized (event){
65                      event.wait();
66                  }
67                  if (event.type==GUIEvent.NEW_MSG){
68                      // send new msg for the user
69                      out(chatId,new LogicTuple("msg",new Value(event.msg)));
70                  } else if (event.type==GUIEvent.LEAVE){
71                      // leaving the chat
72                      break;
73                  }
74              }
75              out(chatId,new LogicTuple("leave"));
76              System.exit(0);
77          } catch (Exception ex){
78              ex.printStackTrace();
79              System.exit(-1);
80          }
81      }
82      
83      /**
84       * method called by the Observer Agent to notify the agent
85       * that the user provided a new message
86       */
87      public void notifyNewMsg(String msg){
88          if (msg!=null && !msg.equals("")){
89              event.msg=msg;
90              event.type=GUIEvent.NEW_MSG;
91              synchronized (event){
92                  event.notify();
93              }
94          }
95      }
96  
97      /**
98       * method called by the GUI to notifdy the agent
99       * that the user want to leave
100      */
101     public void notifyLeave(){
102         event.type=GUIEvent.LEAVE;
103         synchronized (event){
104             event.notify();
105         }
106     }
107 
108     class GUIEvent {
109         static final int NEW_MSG    = 0x01;
110         static final int LEAVE      = 0x02;
111         int type;
112         String msg;
113     }
114 }
115