receiveDefinition.mustache 2.09 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
3
4
5
6
7
8
9
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
  {{>handleUri}}
  java.util.function.Consumer<byte[]> consumer = message -> {
    {{> mappingApplication}}
    {{#loggingEnabledForReads}}
    System.out.println("[Receive] " + {{connectParameterName}} + " -> {{tokenName}} = " + {{lastResult}});
    {{/loggingEnabledForReads}}
    set{{tokenName}}({{lastResult}});
  };
10
  ConnectToken connectToken;
René Schöne's avatar
René Schöne committed
11
12
  switch (scheme) {
  {{#usesMqtt}}
13
    case "mqtt":
14
      connectToken = {{mqttHandlerAttribute}}().newConnection(uri, consumer);
15
16
17
18
      if (connectToken == null) {
        return false;
      }
      break;
René Schöne's avatar
René Schöne committed
19
  {{/usesMqtt}}
Sebastian Ebert's avatar
Sebastian Ebert committed
20
  {{#usesJava}}
21
22
23
24
25
26
27
    case "java":
        String uuid = {{javaHandlerAttribute}}().registerCallback(path, consumer);
        connectToken = new ConnectToken(uri, uuid);
        if (uuid == null) {
            return false;
        }
        break;
Sebastian Ebert's avatar
Sebastian Ebert committed
28
  {{/usesJava}}
René Schöne's avatar
René Schöne committed
29
  {{#usesRest}}
30
    case "rest":
31
      connectToken = {{restHandlerAttribute}}().newPUTConnection(uri, input -> {
32
33
34
35
36
37
38
39
40
41
42
        consumer.accept(input.getBytes());
      });
      if (connectToken == null) {
        return false;
      }
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
  }
43
44
  connectTokens.computeIfAbsent(this, astNode -> new java.util.HashMap<java.net.URI, ConnectToken>())
               .put(uri, connectToken);
45
46
47
48
49
50
51
52
53
  return true;
}

public boolean {{parentTypeName}}.{{disconnectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
  {{>handleUri}}
  switch (scheme) {
  {{#usesMqtt}}
    case "mqtt": return {{mqttHandlerAttribute}}().disconnect(connectTokens.get(this).get(uri));
  {{/usesMqtt}}
54
55
56
  {{#usesJava}}
      case "java": return {{javaHandlerAttribute}}().unregisterCallback(uri.getPath(), connectTokens.get(this).get(uri).id);
  {{/usesJava}}
57
58
  {{#usesRest}}
    case "rest": return {{restHandlerAttribute}}().disconnect(connectTokens.get(this).get(uri));
René Schöne's avatar
René Schöne committed
59
60
61
62
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
René Schöne's avatar
René Schöne committed
63
  }
René Schöne's avatar
René Schöne committed
64
}