1/*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package thrift
18
19// ProcessorFactory is the default processor factory which returns
20// a singleton instance.
21type ProcessorFactory interface {
22	GetProcessor(trans Transport) Processor
23}
24
25type processorFactory struct {
26	processor Processor
27}
28
29// NewProcessorFactory returns a ProcessorFactory.
30func NewProcessorFactory(p Processor) ProcessorFactory {
31	return &processorFactory{processor: p}
32}
33
34func (p *processorFactory) GetProcessor(trans Transport) Processor {
35	return p.processor
36}
37
38/**
39 * The default processor factory just returns a singleton
40 * instance.
41 */
42type ProcessorFunctionFactory interface {
43	GetProcessorFunction(trans Transport) ProcessorFunction
44}
45
46type processorFunctionFactory struct {
47	processor ProcessorFunction
48}
49
50func NewProcessorFunctionFactory(p ProcessorFunction) ProcessorFunctionFactory {
51	return &processorFunctionFactory{processor: p}
52}
53
54func (p *processorFunctionFactory) GetProcessorFunction(trans Transport) ProcessorFunction {
55	return p.processor
56}
57
58// ProcessorFactoryContext is a ProcessorFactory that supports contexts.
59type ProcessorFactoryContext interface {
60	GetProcessorContext(trans Transport) ProcessorContext
61}
62
63type processorFactoryContext struct {
64	processorContext ProcessorContext
65}
66
67// NewProcessorFactoryContext returns a ProcessorFactoryContext.
68func NewProcessorFactoryContext(p ProcessorContext) ProcessorFactoryContext {
69	return &processorFactoryContext{processorContext: p}
70}
71
72func (p *processorFactoryContext) GetProcessorContext(trans Transport) ProcessorContext {
73	return p.processorContext
74}
75
76// NewProcessorFactoryContextAdapter creates a ProcessorFactoryContext from a regular ProcessorFactory.
77func NewProcessorFactoryContextAdapter(p ProcessorFactory) ProcessorFactoryContext {
78	return &ctxProcessorFactoryAdapter{p}
79}
80
81type ctxProcessorFactoryAdapter struct {
82	ProcessorFactory
83}
84
85func (p ctxProcessorFactoryAdapter) GetProcessorContext(trans Transport) ProcessorContext {
86	return NewProcessorContextAdapter(p.ProcessorFactory.GetProcessor(trans))
87}
88