2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import java.io.IOException;
11 import java.net.InetSocketAddress;
12 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.protocol.framework.Dispatcher;
15 import org.opendaylight.protocol.framework.ProtocolServer;
16 import org.opendaylight.protocol.pcep.PCEPConnection;
17 import org.opendaylight.protocol.pcep.PCEPConnectionFactory;
18 import org.opendaylight.protocol.pcep.PCEPDispatcher;
19 import org.opendaylight.protocol.pcep.PCEPSession;
20 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Implementation of PCEPDispatcher.
27 public class PCEPDispatcherImpl implements PCEPDispatcher {
29 private final static Logger logger = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
31 public static final int DEFAULT_MAX_UNKNOWN_MSG = 5;
33 private int maxUnknownMessages = DEFAULT_MAX_UNKNOWN_MSG;
35 private final Dispatcher dispatcher;
37 private final PCEPSessionProposalFactory proposalFactory;
40 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
42 * @throws IOException if some error occurred during opening the selector
44 public PCEPDispatcherImpl(final Dispatcher dispatcher, final PCEPSessionProposalFactory proposalFactory) {
45 this.dispatcher = dispatcher;
46 this.proposalFactory = proposalFactory;
50 public ProtocolServer createServer(final InetSocketAddress address, final PCEPConnectionFactory connectionFactory) throws IOException {
51 connectionFactory.setProposal(this.proposalFactory, address, 0);
52 return this.dispatcher.createServer(address, connectionFactory, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
56 * Create client is used for mock purposes only.
58 * @throws ExecutionException
59 * @throws InterruptedException
62 public PCEPSession createClient(final PCEPConnection connection) throws IOException {
63 PCEPSession session = null;
65 session = (PCEPSession) this.dispatcher.createClient(connection, new PCEPSessionFactoryImpl(this.maxUnknownMessages)).get();
66 } catch (InterruptedException | ExecutionException e) {
67 logger.warn("Client not created. Exception {}.", e.getMessage(), e);
73 public void setMaxUnknownMessages(final int limit) {
74 this.maxUnknownMessages = limit;
77 public int getMaxUnknownMessages() {
78 return this.maxUnknownMessages;
81 public Dispatcher getDispatcher() {
82 return this.dispatcher;