Package edu.neu.ccs.task.agent

Source Code of edu.neu.ccs.task.agent.AgentServer

package edu.neu.ccs.task.agent;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import webframe.Configuration;
import edu.neu.ccs.task.TaskModel;
import edu.neu.ccs.task.TaskModelSet;
import edu.neu.ccs.task.flash.SpeechServerConfig;
import edu.neu.ccs.task.util.XMLUtil;

/**
* An interface to the dialogue agent.  This provides a server which
* can talk to our ECA clients.
*
* @author Dan Schulman
* @version $Id: AgentServer.java 780 2009-12-01 20:09:12Z schulman $
*/
public class AgentServer {
  private final int port;
  private final AgentFactory factory;
  private final TaskModelSet models;
  protected final String top;
  protected final Map<String, String> topInputs;
  private final SpeechServerConfig speechConfig;
  private final List<OutputFilterFactory> filterFactories;
 
  public AgentServer(Configuration c) throws
      InstantiationException,
      IllegalAccessException,
      ClassNotFoundException,
      InvocationTargetException,
      XMLStreamException,
      IOException {
    port = c.getInt("dtask.port", 6000);
    factory = c.getInstance(AgentFactory.class, "dtask.agent-factory", AgentFactory.DEFAULT);
    models = factory.createModelSet();
    top = c.getString("dtask.top-task", "Top");
   
    topInputs = new HashMap<String, String>();
    for (String name : c.parameters())
      if (name.startsWith("dtask.top-input."))
        topInputs.put(name.substring(16), c.getString(name));
   
    speechConfig = new SpeechServerConfig(c);
    filterFactories = Collections.emptyList();
   
    for (String model : c.getStrings("dtask.models")) {
      TaskModel tm;
      if (XMLUtil.isURI(model))
        tm = models.load(model, null);
      else
        tm = models.load(c.findResource(model), null);
      System.out.println("loaded " + tm.getURI());
    }
  }

  /**
   * start the server
   * @throws Exception
   */
  public void run() throws IOException {
    models.reindex();
    check();
   
    ServerSocket serverSock = new ServerSocket(port);
    System.out.println("ready");
   
    while (true) {
      final Socket socket = serverSock.accept();
      new Thread(new Runnable() {
        public void run() {
          try {
            try {
              System.out.println("Connect: " + socket.getInetAddress());
             
              Agent agent = initAgent();
              AgentSession session = initSession(agent, socket);
              session.run();
            } finally {
              System.out.println("Disconnect: " + socket.getInetAddress());
              socket.close();
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
     
      }).start();
    }
     
  }
 
  public void check() {
    List<String> errs = new ArrayList<String>();
    models.checkAll(errs);
    for (String err : errs)
      System.out.println(err);
  }
 
  private Agent initAgent() {
    Agent agent = factory.createAgent(models);
    agent.getEngine().evalInitScripts();
    return agent;
  }
 
  protected AgentSession initSession(Agent agent, Socket socket)
      throws Exception {
    List<OutputFilter> filters = new ArrayList<OutputFilter>();
    for (OutputFilterFactory factory : filterFactories)
      filters.add(factory.createFilter());
   
    if (filters.isEmpty())
      filters.add(new DefaultOutputFilter());
   
    return new AgentSession(agent, top, topInputs,
        speechConfig,  filters, socket);
  }
 
  public static void main(String[] args) throws Exception {
    AgentMain.serverMain(args);
  }
}
TOP

Related Classes of edu.neu.ccs.task.agent.AgentServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.