aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/test/SimpleProtocol.java
blob: 44f24edd8a80c05b7e5094db937fc5671a7591b1 (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
80
81
82
83
84
85
86
87
88
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.test;

import com.yahoo.component.Version;
import com.yahoo.messagebus.Protocol;
import com.yahoo.messagebus.Routable;
import com.yahoo.messagebus.routing.RoutingPolicy;
import com.yahoo.text.Utf8;
import com.yahoo.text.Utf8String;

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

/**
 * @author Simon Thoresen Hult
 */
public class SimpleProtocol implements Protocol {

    public static final Utf8String NAME = new Utf8String("Simple");
    public static final int MESSAGE = 1;
    public static final int REPLY = 2;
    private final Map<String, PolicyFactory> policies = new HashMap<String, PolicyFactory>();

    @Override
    public String getName() {
        return NAME.toString();
    }

    @Override
    public RoutingPolicy createPolicy(String name, String param) {
        if (policies.containsKey(name)) {
            return policies.get(name).create(param);
        }
        return null;
    }

    @Override
    public Routable decode(Version version, byte[] data) {
        String str = Utf8.toString(data);
        if (str.length() < 1) {
            return null;
        }
        char c = str.charAt(0);
        if (c == 'M') {
            return new SimpleMessage(str.substring(1));
        }
        if (c == 'R') {
            return new SimpleReply(str.substring(1));
        }
        return null;
    }

    @Override
    public byte[] encode(Version version, Routable routable) {
        if (routable.getType() == MESSAGE) {
            return Utf8.toBytes("M" + ((SimpleMessage)routable).getValue());
        } else if (routable.getType() == REPLY) {
            return Utf8.toBytes("R" + ((SimpleReply)routable).getValue());
        } else {
            return null;
        }
    }

    /**
     * Registers a policy factory with this protocol under a given name. Whenever a policy is requested that matches
     * this name, the factory is invoked.
     *
     * @param name    The name of the policy.
     * @param factory The policy factory.
     */
    public void addPolicyFactory(String name, PolicyFactory factory) {
        policies.put(name, factory);
    }

    /**
     * Defines a policy factory interface that tests can use to register arbitrary policies with this protocol.
     */
    public interface PolicyFactory {

        /**
         * Creates a new instance of the routing policy that this factory encapsulates.
         *
         * @param param The param for the policy constructor.
         * @return The routing policy created.
         */
        public RoutingPolicy create(String param);
    }
}