msgService.setConnectionPort(((ConnectionRequest)msg).getPort());
switch (((ConnectionRequest) msg).getRequestType()){
case ConnectionRequest.REQUESTTYPE_PRODUCER:
TcpConnectInfo tcpInfo = getProducerConnectBroker();
try {
msgService.sendMessage(tcpInfo);
System.out.println("Producer redirected to "+tcpInfo.getHost()+":"+tcpInfo.getPort());
} catch (IOException e) {
e.printStackTrace();
}
break;
case ConnectionRequest.REQUESTTYPE_BROKER:
// put this sub broker in a hashtable
SubBrokerData subBroker = new SubBrokerData(msgService);
this.subBrokers.put(subBroker.getID(), subBroker);
subBroker.getMessageService().addDisconnectHandler(this);
System.out.println("Registered a SubBroker connection: "+msgService.getConnectionInfo().getHost()+":"+msgService.getConnectionInfo().getPort());
break;
case ConnectionRequest.REQUESTTYPE_CONSUMER:
TcpConnectInfo tcpInfo2 = getConsumerConnectBroker();
try {
msgService.sendMessage(tcpInfo2);
System.out.println("Consumer redirected to "+tcpInfo2.getHost()+":"+tcpInfo2.getPort());
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
else if (msg instanceof ModelUpdateMessage){
TcpConnectInfo tcpInfo = ((SerializingMessageService)src).getConnectionInfo();
SubBrokerData subBroker = null;
for (SubBrokerData sub : this.subBrokers.values()){
if (sub.getMessageService().getConnectionInfo().equals(tcpInfo)){
subBroker = sub;
break;
}
}
if (subBroker != null){
// update model of this sub broker
ModelUpdateMessage message = (ModelUpdateMessage)msg;
subBroker.setRegisteredProducers(message.getProducers());
subBroker.setRegisteredConsumers(message.getConsumers());
System.out.println("Recieved and stored update from SubBroker "+tcpInfo.getHost()+":"+tcpInfo.getPort());
}
}
}