summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.demo/src/org/genivi/trafficinfo/demo/communicationchannel/async2waycommchannel/impl/DummyAsyn2WayCommunicationChannel.java
blob: cc0f9be2071d8c3dd3e92113a489af053ab7022e (plain)
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
80
81
82
83
/**
 * 
 * Copyright (C) 2013 TomTom International B.V.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 */
package org.genivi.trafficinfo.demo.communicationchannel.async2waycommchannel.impl;

import java.util.HashMap;
import java.util.Map;

import org.genivi.trafficinfo.demo.communicationchannel.async2waycommchannel.Async2WayCommunicationChannelClient;
import org.genivi.trafficinfo.demo.communicationchannel.async2waycommchannel.Async2WayCommunicationChannelDataReceptionListener;
import org.genivi.trafficinfo.demo.communicationchannel.async2waycommchannel.Async2WayCommunicationChannelServer;


public class DummyAsyn2WayCommunicationChannel implements Async2WayCommunicationChannelClient, Async2WayCommunicationChannelServer {
  private static DummyAsyn2WayCommunicationChannel broadcastChannel = null;

  private Map<String, Async2WayCommunicationChannelDataReceptionListener> requestListeners =
      new HashMap<String, Async2WayCommunicationChannelDataReceptionListener>();
  private Map<Long, Async2WayCommunicationChannelDataReceptionListener> openRequests =
      new HashMap<Long, Async2WayCommunicationChannelDataReceptionListener>();
  private long nextMessageId = 1l;

  public static DummyAsyn2WayCommunicationChannel getInstance() {
    if (broadcastChannel == null) {
      broadcastChannel = new DummyAsyn2WayCommunicationChannel();
    }

    return broadcastChannel;
  }

  @Override
  public void addRequestListener(String address, Async2WayCommunicationChannelDataReceptionListener requestListener) {
    requestListeners.put(address, requestListener);
  }
  
  public void removeRequestListener(String address) {
    requestListeners.remove(address);
  }
  
  public void removeRequestListener(Async2WayCommunicationChannelDataReceptionListener requestListener) {
    for (String address: requestListeners.keySet()) {
      if (requestListeners.get(address).equals(requestListener)) {
        requestListeners.remove(address);
      }
    }
  }

  @Override
  public long sendRequest(String address, Object data, Async2WayCommunicationChannelDataReceptionListener responseListener) {
    Async2WayCommunicationChannelDataReceptionListener requestListener = getRequestListenerForAddress(address);
    if (requestListener == null) {
      throw new RuntimeException("No request listener for request sent to address: " + address);
    }
    long messageId = generateMessageId();
    openRequests.put(messageId, responseListener);
    requestListener.dataReceived(messageId, data);
    System.out.println("DummyAsyn2WayCommunicationChannel: sending request");
    
    return messageId;
  }


  @Override
  public void sendResponse(long messageId, Object data) {
    System.out.println("DummyAsyn2WayCommunicationChannel: sending response");
    Async2WayCommunicationChannelDataReceptionListener dataReceptionListener = openRequests.remove(messageId);
    dataReceptionListener.dataReceived(messageId, data);
  }

  private long generateMessageId() {
    return nextMessageId++;
  }

  private Async2WayCommunicationChannelDataReceptionListener getRequestListenerForAddress(String address) {
    return requestListeners.get(address);
  }
}