Initial framework migration to netty.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImpl.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.pcep.impl;
9
10 import java.io.IOException;
11 import java.net.InetSocketAddress;
12
13 import org.opendaylight.protocol.framework.Dispatcher;
14 import org.opendaylight.protocol.framework.ProtocolServer;
15 import org.opendaylight.protocol.pcep.PCEPConnection;
16 import org.opendaylight.protocol.pcep.PCEPConnectionFactory;
17 import org.opendaylight.protocol.pcep.PCEPDispatcher;
18 import org.opendaylight.protocol.pcep.PCEPSession;
19 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
20
21 /**
22  * Implementation of PCEPDispatcher.
23  */
24 public class PCEPDispatcherImpl implements PCEPDispatcher {
25         public static final int DEFAULT_MAX_UNKNOWN_MSG = 5;
26
27         private int maxUnknownMessages = DEFAULT_MAX_UNKNOWN_MSG;
28
29         private final Dispatcher dispatcher;
30
31         private final PCEPSessionProposalFactory proposalFactory;
32
33         /**
34          * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
35          * 
36          * @throws IOException if some error occurred during opening the selector
37          */
38         public PCEPDispatcherImpl(final Dispatcher dispatcher, final PCEPSessionProposalFactory proposalFactory) {
39                 this.dispatcher = dispatcher;
40                 this.proposalFactory = proposalFactory;
41         }
42
43         @Override
44         public ProtocolServer createServer(final InetSocketAddress address, final PCEPConnectionFactory connectionFactory) throws IOException {
45                 connectionFactory.setProposal(this.proposalFactory, address, 0);
46                 return this.dispatcher.createServer(address, connectionFactory, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
47         }
48
49         /**
50          * Create client is used for mock purposes only.
51          */
52         @Override
53         public PCEPSession createClient(final PCEPConnection connection) throws IOException {
54                 return (PCEPSession) this.dispatcher.createClient(connection, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
55         }
56
57         @Override
58         public void setMaxUnknownMessages(final int limit) {
59                 this.maxUnknownMessages = limit;
60         }
61
62         public int getMaxUnknownMessages() {
63                 return this.maxUnknownMessages;
64         }
65
66         public Dispatcher getDispatcher() {
67                 return this.dispatcher;
68         }
69 }