summaryrefslogtreecommitdiffstats
path: root/vespaclient-core/src/main/java/com/yahoo/feedapi/DummySessionFactory.java
blob: 7b22abc8a763b8c01344d017eca3339e5af67df6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedapi;

import com.yahoo.document.Document;
import com.yahoo.documentapi.VisitorParameters;
import com.yahoo.documentapi.VisitorSession;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.jdisc.Metric;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.ReplyHandler;
import com.yahoo.messagebus.Result;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DummySessionFactory implements SessionFactory {

    public interface ReplyFactory {
        Reply createReply(Message m);
    }

    public final List<Message> messages;
    private boolean autoReply = false;
    private ReplyFactory autoReplyFactory = null;
    private Error autoError;
    private int sessionsCreated = 0;
    OutputStream output = null;

    protected DummySessionFactory() {
        messages = new ArrayList<>();
    }

    public static DummySessionFactory createDefault() {
        return new DummySessionFactory();
    }

    protected DummySessionFactory(boolean autoReply) {
        this.autoReply = autoReply;
        messages = new ArrayList<>();
    }

    protected DummySessionFactory(ReplyFactory autoReplyFactory) {
        this.autoReply = true;
        this.autoReplyFactory = autoReplyFactory;
        messages = new ArrayList<>();
    }

    public static DummySessionFactory createWithAutoReplyFactory(ReplyFactory autoReplyFactory) {
        return new DummySessionFactory(autoReplyFactory);
    }

    protected DummySessionFactory(Error e) {
        autoReply = true;
        this.autoError = e;
        messages = new ArrayList<>();
    }

    public static DummySessionFactory createWithErrorAutoReply(Error e) {
        return new DummySessionFactory(e);
    }

    public static DummySessionFactory createWithAutoReply() {
        return new DummySessionFactory(true);
    }

    public DummySessionFactory(Error e, OutputStream out) {
        messages = null;
        autoReply = true;
        output = out;
    }

    public int sessionsCreated() {
        return sessionsCreated;
    }
    void add(Message m) {
        if (messages != null) {
            messages.add(m);
        }

    }

    @Override
    public SendSession createSendSession(ReplyHandler r, Metric metric) {
        ++sessionsCreated;

        if (output != null) {
            return new DumpDocuments(output, r, this);
        }
        if (autoReply) {
            return new AutoReplySession(r, autoReplyFactory, autoError, this);
        }
        return new DummySession(r, this);
    }

    @Override
    public VisitorSession createVisitorSession(VisitorParameters p) {
        return null;
    }

    public void sendReply(Message m, Error error) {
        MyContext ctxt = (MyContext) m.getContext();

        Reply r = new EmptyReply();
        r.setMessage(m);
        r.setContext(ctxt.oldContext);

        if (error != null) {
            r.addError(error);
        }

        ctxt.handler.handleReply(r);
    }

    private class MyContext {
        MyContext(ReplyHandler handler, Object ctxt) {
            this.handler = handler;
            this.oldContext = ctxt;
        }

        ReplyHandler handler;
        Object oldContext;
    }

    private class AutoReplySession extends SendSession {

        ReplyHandler handler;
        ReplyFactory replyFactory;
        Error e;
        DummySessionFactory owner;

        public AutoReplySession(ReplyHandler handler, ReplyFactory replyFactory,
                                Error e, DummySessionFactory owner) {
            this.handler = handler;
            this.replyFactory = replyFactory;
            this.e = e;
            this.owner = owner;
        }

        protected void handleMessage(Message m) {

        }

        @Override
        protected Result onSend(Message m, boolean blockIfQueueFull) throws InterruptedException {
            owner.add(m);
            handleMessage(m);
            Reply r;
            if (replyFactory == null) {
                r = new EmptyReply();
            } else {
                r = replyFactory.createReply(m);
            }

            m.setTimeReceivedNow();
            r.setMessage(m);
            r.setContext(m.getContext());

            if (e != null) {
                r.addError(e);
            }
            handler.handleReply(r);
            return Result.ACCEPTED;
        }

        @Override
        public void close() {
        }

    }

    private class DumpDocuments extends AutoReplySession {
        final OutputStream out;
        public DumpDocuments(OutputStream out, ReplyHandler r, DummySessionFactory factory) {
            super(r, null, null, factory);
            this.out = out;
        }
        protected void handleMessage(Message m) {
            if (m instanceof PutDocumentMessage) {
                PutDocumentMessage p = (PutDocumentMessage) m;
                Document d = p.getDocumentPut().getDocument();
                d.serialize(out);
            }
        }
    }

    private class DummySession extends SendSession {
        ReplyHandler handler;
        DummySessionFactory owner;

        public DummySession(ReplyHandler handler, DummySessionFactory owner) {
            this.handler = handler;
            this.owner = owner;
        }

        @Override
        protected Result onSend(Message m, boolean blockIfQueueFull) throws InterruptedException {
            m.setContext(new MyContext(handler, m.getContext()));
            owner.add(m);
            return Result.ACCEPTED;
        }

        @Override
        public void close() {
        }
    }

}