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

Quick Search    Search Deep

Source code: demo/tucson/chat/ObserverAgent.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 Observer Agent is responsible to observe (without polling)
26   * the chat room in order to retrieve new message appeated in the chat
27   *
28   * @author Alessandro Ricci
29   *
30   */
31  public class ObserverAgent extends alice.tucson.Agent {
32  
33      TupleCentreId  chatId;
34      GUI gui;
35      Value ownerId;
36  
37      public ObserverAgent(AgentId aid,AgentId ownerId,GUI gui,TupleCentreId tid){
38          super(aid);
39          chatId=tid;
40          this.gui=gui;
41          this.ownerId=new Value(ownerId.toString());
42      }
43  
44      public synchronized void run(){
45          while (true){
46              try {
47                  // get new messages appeared in the chat room
48                  LogicTuple msg=in(chatId,new LogicTuple("msg",ownerId,new Var("Who"),new Var("Content")));
49                  TupleArgument who=msg.getArg(1);
50                  TupleArgument content=msg.getArg(2);
51                  // notify the GUI
52                  gui.notifyNewMsg(who.toString(),content.toString());
53              } catch (Exception ex){
54                  ex.printStackTrace();
55                  System.err.println("TuCSoN node not found.");
56                  System.exit(-1);
57              }
58          }
59      }
60  }
61