📄 libcomtcpserverthread.java
字号:
/* $Id: LibComTcpServerThread.java,v 1.10 2005/03/27 20:26:17 agno Exp $*/
/*
* @(#)LibComTcpServerThread.java
*
* The contents of this file are subject to the OAA Community Research
* License Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License
* at http://www.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.com;
import org.apache.log4j.Logger;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.io.IOException;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.icl.IclStr;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.simplefac.StringConnectionId;
import com.sri.oaa2.simplefac.IclTermReceiver;
import com.sri.oaa2.lib.LibOaa;
/**
* LibComTcpServerThread listens for client connections on a server socket.
*/
abstract class LibComTcpServerThread implements Runnable {
static Logger logger = Logger.getLogger(LibComTcpConnection.class.getName());
private LibComTcpConnection conn;
private ServerSocket server;
private LibComClient mClient;
private volatile boolean connected = true;
private int binaryPort = -1;
LibComTcpServerThread(LibComTcpConnection conn, LibComClient mClient, ServerSocket server) {
this.conn = conn;
this.mClient = mClient;
this.server = server;
}
void disconnect() {
connected = false;
}
void handleNewClient(Socket client) {
String mConnectionId = LibComUtils.gensym("direct_client");
// Set up connection info for this connection. First, add the connection
// wbefore the thread listener is started. Then, set the thread listener
// later (see below)
LibComTcpConnection connection = new LibComTcpConnection(mClient, mConnectionId, conn.getCmdLine());
IclTerm clientAddress = new IclStruct("tcp",
new IclStr(client.getInetAddress().getHostAddress()),
new IclInt(client.getPort()));
connection.setClientConnectionData(client, clientAddress, null);
if (conn.serverListener != null) {
conn.serverListener.notifyConnectionEstablished(connection, mConnectionId, clientAddress,
new IclList());
} else {
logger.error("Failed to get serverListener from tcp connection");
}
RealConnection thread_listener = new RealConnection(new StringConnectionId(mConnectionId), client, mClient, connection);
IclTermReceiver termReceiver = getIclTermReceiver(client, thread_listener);
thread_listener.setTermReceiver(termReceiver);
IclList params = new IclList(new IclStruct("oaa_address", conn.getListenAddress()),
new IclStruct("other_type", new IclStr("client")),
new IclStruct("other_language", new IclStr("java")),
new IclStruct("other_version", LibOaa.getLibraryVersion()));
if (binaryPort != -1) {
params.add(new IclStruct("binary", new IclInt(binaryPort)));
}
IclTerm evConnectedTerm = new IclStruct("event",
new IclStruct("ev_connected",
params), new IclList());
// Wait for connect request before sending the connected event.
try {
IclTerm ev_connect = termReceiver.getNextTerm();
} catch (Exception e) {
logger.error("Failed to read ev_connect term", e);
}
logger.info("Sending evConnectedTerm = " + evConnectedTerm);
thread_listener.sendTerm(evConnectedTerm);
new Thread(thread_listener).start();
connection.setClientConnectionData(client, clientAddress, thread_listener);
//REPLY = term(event(ev_connected([oaa_address(addr(tcp('130.107.64.111',3344),6)),other_id(0),other_type(facili
//tator),other_name(root),other_language(prolog),other_version(3.2),other_dialect(quintus),format(binary
//)]),[]))
}
abstract IclTermReceiver getIclTermReceiver(Socket client,
RealConnection thread_listener);
public void run() {
int port = server.getLocalPort();
InetAddress address = server.getInetAddress();
while (connected) {
if (server == null) {
// Try and reconnect the server
try {
logger.info("Restarting tcp server");
server = new ServerSocket(port, LibCom.MAXBACKLOG, address);
} catch (IOException e) {
logger.error("Failed to restart tcp server", e);
try { Thread.sleep(1000L); } catch (InterruptedException ex) {}
}
}
Socket client = null;
try {
// Wait for a client connection
client = server.accept();
} catch (IOException ex) {
// An exception will be thrown when the socket is closed...
if (connected) {
logger.error("An error occurred in the tcp server", ex);
try {
server.close();
server = null;
} catch (IOException e) {
}
} else {
logger.debug("Disconnected server");
}
}
if (client != null) {
// Set up the client connection
logger.info("Received client connection at " +
client.getInetAddress() + ":" + client.getPort());
handleNewClient(client);
}
}
}
public void setBinaryPort(int binaryPort) {
this.binaryPort = binaryPort;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -