String host = InetAddress.getLocalHost().getHostAddress();
int port = PortUtil.findFreePort(host);
HashMap serverConfig = new HashMap();
serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraServerConfig(serverConfig);
connector = new Connector(serverConfig);
StringBuffer buf = new StringBuffer();
buf.append("<?xml version=\"1.0\"?>\n");
buf.append("<config>");
buf.append(" <invoker transport=\"" + getTransport() + "\">");
buf.append(" <attribute name=\"serverBindAddress\">" + host + "</attribute>");
buf.append(" <attribute name=\"serverBindPort\">" + port + "</attribute>");
buf.append(" <attribute name=\"pingFrequency\" isParam=\"true\">" + TEST_PING_FREQUENCY_STRING + "</attribute>");
buf.append(" <attribute name=\"pingWindowFactor\" isParam=\"true\">" + TEST_PING_WINDOW_FACTOR_STRING + "</attribute>");
buf.append(" <attribute name=\"maxRetries\" isParam=\"true\">" + TEST_MAX_RETRIES_STRING + "</attribute>");
buf.append(" <attribute name=\"maxControlConnectionRestarts\" isParam=\"true\">" + TEST_CONTROL_CONNECTION_RESTARTS_STRING + "</attribute>");
buf.append(" </invoker>");
buf.append("</config>");
log.info(buf);
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
connector.setConfiguration(xml.getDocumentElement());
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
InvokerLocator serverLocator = connector.getLocator();
System.out.println("started connector with locator uri of: " + serverLocator);
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
clientConfig.put(Bisocket.IS_CALLBACK_SERVER, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
client.addListener(callbackHandler, new HashMap());
log.info("client added callback handler");
Integer count = new Integer(17);
HashMap metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
// Test server invoker configuration.
// Actually, none of these parameters are used by the server invoker.
// Test callback client invoker configuration.
Set callbackHandlers = invocationHandler.callbackHandlers;
assertEquals(1, callbackHandlers.size());
ServerInvokerCallbackHandler sich = (ServerInvokerCallbackHandler) callbackHandlers.iterator().next();
Client callbackClient = sich.getCallbackClient();
assertTrue(callbackClient.getInvoker() instanceof BisocketClientInvoker);
BisocketClientInvoker callbackClientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
assertEquals(TEST_PING_FREQUENCY, callbackClientInvoker.getPingFrequency());
assertEquals(TEST_MAX_RETRIES, callbackClientInvoker.getMaxRetries());
// Test client invoker configuration.
assertTrue(client.getInvoker() instanceof BisocketClientInvoker);
BisocketClientInvoker clientInvoker = (BisocketClientInvoker) client.getInvoker();
assertEquals(TEST_PING_FREQUENCY, clientInvoker.getPingFrequency());
assertEquals(TEST_MAX_RETRIES, clientInvoker.getMaxRetries());
// Test callback server invoker configuration.
Field field = Client.class.getDeclaredField("callbackConnectors");
field.setAccessible(true);
Map callbackConnectorsMap = (Map) field.get(client);
assertEquals(1, callbackConnectorsMap.size());
Set callbackConnectorsSet = (Set) callbackConnectorsMap.values().iterator().next();
assertEquals(1, callbackConnectorsSet.size());
Connector callbackConnector = (Connector) callbackConnectorsSet.iterator().next();
assertTrue(connector.getServerInvoker() instanceof BisocketServerInvoker);
BisocketServerInvoker callbackServerInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
assertEquals(TEST_PING_FREQUENCY, callbackServerInvoker.getPingFrequency());
assertEquals(TEST_PING_WINDOW_FACTOR, callbackServerInvoker.getPingWindowFactor());
field = BisocketServerInvoker.class.getDeclaredField("pingWindow");
field.setAccessible(true);
int pingWindow = ((Integer) field.get(callbackServerInvoker)).intValue();