From 9b9b883d3727331d1e09d31893f69d5467816ab6 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Mon, 3 Feb 2014 15:58:26 +0100 Subject: [PATCH] Fix of bug in Notificator class Deleting of listner from listenersByStreamName map was corrected try, finally block was added for locking, unlocking of access to map Change-Id: I60ecb9c247af18436f348d4f4827c7f75fdfaeaa Signed-off-by: Jozef Gloncak --- .../sal/streams/listeners/Notificator.java | 33 +++++++++----- .../iml/varioustests/VariousTest.java | 44 ------------------- .../websockets/client/WebSocketClient.java | 26 +++++++++-- 3 files changed, 43 insertions(+), 60 deletions(-) delete mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/listeners/Notificator.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/listeners/Notificator.java index b0140ec83a..d1cb25861a 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/listeners/Notificator.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/listeners/Notificator.java @@ -30,10 +30,13 @@ public class Notificator { public static ListenerAdapter createListener(InstanceIdentifier path, String streamName) { ListenerAdapter listener = new ListenerAdapter(path, streamName); - lock.lock(); - listenersByInstanceIdentifier.put(path, listener); - listenersByStreamName.put(streamName, listener); - lock.unlock(); + try { + lock.lock(); + listenersByInstanceIdentifier.put(path, listener); + listenersByStreamName.put(streamName, listener); + } finally { + lock.unlock(); + } return listener; } @@ -63,10 +66,13 @@ public class Notificator { } catch (Exception e) { } } - lock.lock(); - listenersByStreamName = new ConcurrentHashMap<>(); - listenersByInstanceIdentifier = new ConcurrentHashMap<>(); - lock.unlock(); + try { + lock.lock(); + listenersByStreamName = new ConcurrentHashMap<>(); + listenersByInstanceIdentifier = new ConcurrentHashMap<>(); + } finally { + lock.unlock(); + } } public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) { @@ -81,10 +87,13 @@ public class Notificator { listener.close(); } catch (Exception e) { } - lock.lock(); - listenersByInstanceIdentifier.remove(listener.getPath()); - listenersByStreamName.remove(listener).getStreamName(); - lock.unlock(); + try { + lock.lock(); + listenersByInstanceIdentifier.remove(listener.getPath()); + listenersByStreamName.remove(listener.getStreamName()); + } finally { + lock.unlock(); + } } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java deleted file mode 100644 index cdfed8e494..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.restconf.iml.varioustests; - -import org.junit.Ignore; -import org.junit.Test; -import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.w3c.dom.Document; - - -public class VariousTest { - - @Ignore - @Test - public void test() { - String[] split = "/something:dfsa/s:sda".split("/"); - System.out.println(split.length); - for (String str : split) { - System.out.println(">"+str+"<"); - } - - } - - @Test - public void loadXml() { - TestUtils.readInputToCnSn("/varioustest/xmldata.xml", XmlToCompositeNodeProvider.INSTANCE); -// TestUtils.normalizeCompositeNode(compositeNode, modules, schemaNodePath) - } - - @Test - public void buildXml() { -// Document doc; -// doc.createElementNS(namespaceURI, qualifiedName) - } - - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java index bff63b88ad..845d54ea1f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java @@ -24,6 +24,8 @@ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketVersion; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.net.URI; import org.slf4j.Logger; @@ -81,8 +83,9 @@ public class WebSocketClient { clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6}))); } - public void close() throws InterruptedException { - clientChannel.writeAndFlush(new CloseWebSocketFrame()); + public void close(String reasonText) throws InterruptedException { + CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000,reasonText); + clientChannel.writeAndFlush(closeWebSocketFrame); // WebSocketClientHandler will close the connection when the server // responds to the CloseWebSocketFrame. @@ -95,17 +98,32 @@ public class WebSocketClient { if (args.length > 0) { uri = new URI(args[0]); } else { - uri = new URI("http://192.168.11.1:8181/opendaylight-inventory:nodes"); + uri = new URI("http://192.168.1.101:8181/opendaylight-inventory:nodes"); } IClientMessageCallback messageCallback = new ClientMessageCallback(); WebSocketClient webSocketClient = new WebSocketClient(uri, messageCallback); webSocketClient.connect(); + + while (true) { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String input = br.readLine(); + if (input.equals("q")) { + System.out.print("Would you like to close stream? (Y = yes, empty = yes)\n"); + input = br.readLine(); + if (input.equals("yes") || input.isEmpty()) { + webSocketClient.close("opendaylight-inventory:nodes"); + break; + } + } + } } private static class ClientMessageCallback implements IClientMessageCallback { @Override public void onMessageReceived(Object message) { - logger.info("received message {}", ((TextWebSocketFrame)message).text()); + if (message instanceof TextWebSocketFrame) { + logger.info("received message {}"+ ((TextWebSocketFrame)message).text()); + } } } -- 2.36.6