e99ec2d335d4091363406484528b26699cddfca8
[bgpcep.git] / framework / src / test / java / org / opendaylight / protocol / framework / SimpleSessionListener.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.framework;
9
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Simple Session Listener that is notified about messages and changes in the session.
19  */
20 public class SimpleSessionListener implements SessionListener {
21         private static final Logger logger = LoggerFactory.getLogger(SimpleSessionListener.class);
22
23         public List<ProtocolMessage> messages = new ArrayList<ProtocolMessage>();
24
25         public boolean up = false;
26
27         public boolean failed = false;
28
29         public void onMessage(ProtocolSession session, ProtocolMessage message) {
30                 logger.debug("Received message: " + message.getClass() + " " + message);
31                 this.messages.add(message);
32         }
33
34         public synchronized void onSessionUp(ProtocolSession session, SimpleSessionPreferences local,
35                         SimpleSessionPreferences remote) {
36                 logger.debug("Session up.");
37                 this.up = true;
38                 this.notifyAll();
39         }
40
41         public synchronized void onConnectionFailed(ProtocolSession session, Exception e) {
42                 logger.debug("Connection Failed: {}", e.getMessage(), e);
43                 this.failed = true;
44                 this.notifyAll();
45                 try {
46                         session.close();
47                 } catch (final IOException ex) {
48                         logger.warn("Session could not be closed.");
49                 }
50         }
51 }