}
@Override
public void onConnectConfirm(DdeServerConnectionEvent e) {
DDEClient client = DDEServer.this.addClient(e.getConvHandle(), e.getTopic());
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
EventManager.postEventAndWait(DDEServer.this, "Connected", qq_Params);
_log.debug("New Connection confirmed");
}
@Override
public void onDisconnect(DdeServerConnectionEvent e) {
if (DDEServer.this.clients.containsKey(e.getConvHandle())){
DDEClient client = DDEServer.this.clients.get(e.getConvHandle());
synchronized (DDEServer.this.clients) {
DDEServer.this.clients.remove(e.getConvHandle());
}
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
EventManager.postEventAndWait(DDEServer.this, "Disconnected", qq_Params);
_log.debug("Disconnected");
}
return;
}
});
} catch (TooManyListenersException e) {
_log.error(e);
}
//Register transaction event listener
try {
this.addDdeServerTransactionEventListener(new com.neva.DdeServerTransactionEventAdaptor() {
@Override
public void onPoke(DdeServerTransactionEvent e)
throws DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
String value = new String(e.getDdeData());
value = value.trim();
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
qq_Params.put( "putItemID", new ParameterHolder(getRespID()));
qq_Params.put( "text", new ParameterHolder(new TextData(value)));
qq_Params.put( "item", new ParameterHolder(new TextData(e.getItem())));
_log.debug("PutItemRequest: " + e.getItem() + " : " + value);
EventManager.postEventAndWait(DDEServer.this, "PutItemRequest", qq_Params);
ItemResponse response;
synchronized (DDEServer.this.responseData){
response = DDEServer.this.responseData.get(e.getConvHandle());
DDEServer.this.responseData.remove(e.getConvHandle());
}
if (response == null){
_log.debug("Processing: putItemResponse() with nothing");
e.setRequestedData(EMPTY);
} else {
_log.debug("Processing: putItemResponse() with " + response);
e.setRequestedData(new byte[] {(byte)response.getReturnCode()});
}
return;
}
@Override
public void onRequest(com.neva.DdeServerTransactionEvent e)
throws com.neva.DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
qq_Params.put( "item", new ParameterHolder(new TextData(e.getItem())));
_log.debug("Posting: GetItemRequest for " + e.getItem());
EventManager.postEventAndWait(DDEServer.this, "GetItemRequest", qq_Params);
ItemResponse response;
synchronized (DDEServer.this.responseData){
response = DDEServer.this.responseData.get(e.getConvHandle());
DDEServer.this.responseData.remove(e.getConvHandle());
}
if (response == null){
_log.debug("Processing: getItemResponse() with nothing");
e.setRequestedData(EMPTY);
} else {
_log.debug("Processing: getItemResponse() with " + response);
e.setRequestedData(response.getTextAsCString());
}
return;
}
@Override
public void onExecute(DdeServerTransactionEvent e)
throws DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
int ID = getCmdID();
String command = new String(e.getDdeData());
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
qq_Params.put( "command", new ParameterHolder(new TextData(command)));
qq_Params.put( "commandID", new ParameterHolder(ID));
_log.debug("CommandRequest : " + command);
EventManager.postEventAndWait(DDEServer.this, "CommandRequest", qq_Params);
CommandResponse response;
synchronized (DDEServer.this.commandResponseData){
response = DDEServer.this.commandResponseData.get(e.getConvHandle()+":"+ID);
DDEServer.this.commandResponseData.remove(e.getConvHandle()+":"+ID);
}
if (response == null){
_log.debug("Processing: CommandResponse() with nothing");
e.setRequestedData(EMPTY);
} else {
_log.debug("Processing: CommandResponse() with " + response);
e.setRequestedData(new byte[] {(byte)response.getReturnCode(), 0});
}
return;
}
//======================================
// linked item methods
//======================================
/**
* onAdvReq is called once for each client when postAdvise is called (from the UpdateItem method).
* We need to update the passed event with the data to be returned to the particular client.
* @throws DdeServerTransactionRejectedException
*/
@Override
public void onAdvReq(DdeServerTransactionEvent e) throws DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
String item = e.getItem();
// Most of this code is obsolete. The use of the LinkedItemRequest is Forte is odd, and never
// seems to get posted. Hence, this method will only ever be called from the postAdvise method
// which should ensure that there is valid linked response data set up. So it really will just
// call e.setRequestedData().
LinkedItem response;
synchronized (DDEServer.this.linkedResponseData){
response = DDEServer.this.linkedResponseData.get( item );
}
if (response == null) {
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "client", new ParameterHolder(client));
qq_Params.put( "item", new ParameterHolder(new TextData(item)));
qq_Params.put( "isHot", new ParameterHolder(true));
_log.debug("LinkedItemRequest : " + item);
EventManager.postEventAndWait(DDEServer.this, "LinkedItemRequest", qq_Params);
synchronized (DDEServer.this.linkedResponseData){
response = DDEServer.this.linkedResponseData.get( item );
if (response == null){
_log.debug("Processing: request UpdateItem() with nothing");
e.setRequestedData(EMPTY);
} else {
_log.debug("Processing: request UpdateItem() with " + response);
e.setRequestedData(response.getTextAsCString());
}
// Clear out the linked response
linkedResponseData.remove(item);
}
}
else {
e.setRequestedData(response.getTextAsCString());
}
return;
}
@Override
public void onAdvStart(DdeServerTransactionEvent e)
throws DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
String item = e.getItem();
String topic = e.getTopic();
addTopicToItemMap(item, topic);
synchronized (DDEServer.this.linkedResponseData){
LinkedItem li = DDEServer.this.linkedResponseData.get( item );
if ( li == null){
li = new LinkedItem(item, "");
DDEServer.this.linkedResponseData.put(item, li);
}
li.addClient(client);
}
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "item", new ParameterHolder(new TextData(e.getItem())));
_log.debug("LinkStartRequest : " + e.getItem());
EventManager.postEventAndWait(DDEServer.this, "LinkStartRequest", qq_Params);
synchronized (DDEServer.this.linkedResponseData){
LinkedItem lir = DDEServer.this.linkedResponseData.get(item);
_log.debug("Processing: start UpdateItem() with " + lir);
e.setRequestedData(lir.getTextAsCString());
}
return;
}
@Override
public void onAdvStop(DdeServerTransactionEvent e)
throws DdeServerTransactionRejectedException {
DDEClient client = getClient(e.getConvHandle());
String item = e.getItem();
String topic = e.getTopic();
removeTopicFromItemMap(item, topic);
synchronized (DDEServer.this.linkedResponseData){
LinkedItem lir = DDEServer.this.linkedResponseData.get(item);
if (lir != null){
lir.removeClient(client);
}
}
Hashtable<String, ParameterHolder> qq_Params = new Hashtable<String, ParameterHolder>();
qq_Params.put( "item", new ParameterHolder(new TextData(e.getItem())));
_log.debug("LinkEndRequest : " + e.getItem());
EventManager.postEventAndWait(DDEServer.this, "LinkEndRequest", qq_Params);
return;
}
});