Skip to main content
summaryrefslogtreecommitdiffstats
blob: 591f8690bc2fecaec0944a7fae99d943e7dba5af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * 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
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.message.io;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Ken J. Aguilar
 */
public class MessageIoManagementService implements IMessageIoManagementService {

   private final HashSet<IMessageIoDriver> drivers = new HashSet<>();

   private final Lock lock = new ReentrantLock();
   private boolean ioStarted = false;

   @Override
   public void install(IMessageIoDriver ioDriver) {
      lock.lock();
      if (!drivers.add(ioDriver)) {
         // driver was already installed
         lock.unlock();
         return;
      }
      if (ioStarted) {
         // make sure we release the lock before entering unknown code
         lock.unlock();
         ioDriver.start();
      } else {
         lock.unlock();
      }
   }

   @Override
   public void startIO() {
      lock.lock();
      ioStarted = true;

      Set<IMessageIoDriver> copiedDrivers = new HashSet<>(drivers);
      lock.unlock();
      for (IMessageIoDriver driver : copiedDrivers) {
         driver.start();
      }

   }

   @Override
   public void stopIO() {
      lock.lock();
      ioStarted = false;
      Set<IMessageIoDriver> copiedDrivers = new HashSet<>(drivers);
      lock.unlock();
      for (IMessageIoDriver driver : copiedDrivers) {
         driver.stop();
      }
   }

   @Override
   public void uninstall(IMessageIoDriver ioDriver) {
      lock.lock();
      boolean changed = drivers.remove(ioDriver);
      lock.unlock();
      if (changed && ioDriver.isStarted()) {
         ioDriver.stop();
      }
   }

}

Back to the top