<commit_before>package core

import (
	"context"
	"sync"

	"v2ray.com/core/common"
	"v2ray.com/core/common/serial"
	"v2ray.com/core/features"
	"v2ray.com/core/features/dns"
	"v2ray.com/core/features/inbound"
	"v2ray.com/core/features/outbound"
	"v2ray.com/core/features/policy"
	"v2ray.com/core/features/routing"
)

// Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
type Server interface {
	common.Runnable
}

// ServerType returns the type of the server.
func ServerType() interface{} {
	return (*Instance)(nil)
}

type resolution struct {
	deps     []interface{}
	callback func([]features.Feature)
}

func getFeature(allFeatures []features.Feature, t interface{}) features.Feature {
	for _, f := range allFeatures {
		if f.Type() == t {
			return f
		}
	}
	return nil
}

func (r *resolution) resolve(allFeatures []features.Feature) bool {
	var fs []features.Feature
	for _, d := range r.deps {
		f := getFeature(allFeatures, d)
		if f == nil {
			return false
		}
		fs = append(fs, f)
	}

	r.callback(fs)
	return true
}

// Instance combines all functionalities in V2Ray.
type Instance struct {
	access             sync.Mutex
	features           []features.Feature
	featureResolutions []resolution
	running            bool
}

// New returns a new V2Ray instance based on given configuration.
// The instance is not started at this point.
// To ensure V2Ray instance works properly, the config must contain one Dispatcher, one InboundHandlerManager and one OutboundHandlerManager. Other features are optional.
func New(config *Config) (*Instance, error) {
	var server = &Instance{}

	if config.Transport != nil {
		features.PrintDeprecatedFeatureWarning("global transport settings")
	}
	if err := config.Transport.Apply(); err != nil {
		return nil, err
	}

	for _, appSettings := range config.App {
		settings, err := appSettings.GetInstance()
		if err != nil {
			return nil, err
		}
		obj, err := CreateObject(server, settings)
		if err != nil {
			return nil, err
		}
		if feature, ok := obj.(features.Feature); ok {
			server.AddFeature(feature)
		}
	}

	if server.GetFeature(dns.ClientType()) == nil {
		server.AddFeature(dns.LocalClient{})
	}

	if server.GetFeature(policy.ManagerType()) == nil {
		server.AddFeature(policy.DefaultManager{})
	}

	if server.GetFeature(routing.RouterType()) == nil {
		server.AddFeature(routing.DefaultRouter{})
	}

	// Add an empty instance at the end, for optional feature requirement.
	server.AddFeature(&Instance{})

	if server.featureResolutions != nil {
		return nil, newError("not all dependency are resolved.")
	}

	if len(config.Inbound) > 0 {
		inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager)
		for _, inboundConfig := range config.Inbound {
			rawHandler, err := CreateObject(server, inboundConfig)
			if err != nil {
				return nil, err
			}
			handler, ok := rawHandler.(inbound.Handler)
			if !ok {
				return nil, newError("not an InboundHandler")
			}
			if err := inboundManager.AddHandler(context.Background(), handler); err != nil {
				return nil, err
			}
		}
	}

	if len(config.Outbound) > 0 {
		outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager)
		for _, outboundConfig := range config.Outbound {
			rawHandler, err := CreateObject(server, outboundConfig)
			if err != nil {
				return nil, err
			}
			handler, ok := rawHandler.(outbound.Handler)
			if !ok {
				return nil, newError("not an OutboundHandler")
			}
			if err := outboundManager.AddHandler(context.Background(), handler); err != nil {
				return nil, err
			}
		}
	}

	return server, nil
}

// Type implements common.HasType.
func (s *Instance) Type() interface{} {
	return ServerType()
}

// Close shutdown the V2Ray instance.
func (s *Instance) Close() error {<commit_msg>refactor new instance function<commit_after>package core

import (
	"context"
	"sync"

	"v2ray.com/core/common"
	"v2ray.com/core/common/serial"
	"v2ray.com/core/features"
	"v2ray.com/core/features/dns"
	"v2ray.com/core/features/inbound"
	"v2ray.com/core/features/outbound"
	"v2ray.com/core/features/policy"
	"v2ray.com/core/features/routing"
)

// Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
type Server interface {
	common.Runnable
}

// ServerType returns the type of the server.
func ServerType() interface{} {
	return (*Instance)(nil)
}

type resolution struct {
	deps     []interface{}
	callback func([]features.Feature)
}

func getFeature(allFeatures []features.Feature, t interface{}) features.Feature {
	for _, f := range allFeatures {
		if f.Type() == t {
			return f
		}
	}
	return nil
}

func (r *resolution) resolve(allFeatures []features.Feature) bool {
	var fs []features.Feature
	for _, d := range r.deps {
		f := getFeature(allFeatures, d)
		if f == nil {
			return false
		}
		fs = append(fs, f)
	}

	r.callback(fs)
	return true
}

// Instance combines all functionalities in V2Ray.
type Instance struct {
	access             sync.Mutex
	features           []features.Feature
	featureResolutions []resolution
	running            bool
}

func addInboundHandlers(server *Instance, configs []*InboundHandlerConfig) error {
	if len(configs) == 0 {
		return nil
	}

	inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager)
	for _, inboundConfig := range configs {
		rawHandler, err := CreateObject(server, inboundConfig)
		if err != nil {
			return err
		}
		handler, ok := rawHandler.(inbound.Handler)
		if !ok {
			return newError("not an InboundHandler")
		}
		if err := inboundManager.AddHandler(context.Background(), handler); err != nil {
			return err
		}
	}

	return nil
}

func addOutboundHandlers(server *Instance, configs []*OutboundHandlerConfig) error {
	if len(configs) == 0 {
		return nil
	}

	outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager)
	for _, outboundConfig := range configs {
		rawHandler, err := CreateObject(server, outboundConfig)
		if err != nil {
			return err
		}
		handler, ok := rawHandler.(outbound.Handler)
		if !ok {
			return newError("not an OutboundHandler")
		}
		if err := outboundManager.AddHandler(context.Background(), handler); err != nil {
			return err
		}
	}

	return nil
}

// New returns a new V2Ray instance based on given configuration.
// The instance is not started at this point.
// To ensure V2Ray instance works properly, the config must contain one Dispatcher, one InboundHandlerManager and one OutboundHandlerManager. Other features are optional.
func New(config *Config) (*Instance, error) {
	var server = &Instance{}

	if config.Transport != nil {
		features.PrintDeprecatedFeatureWarning("global transport settings")
	}
	if err := config.Transport.Apply(); err != nil {
		return nil, err
	}

	for _, appSettings := range config.App {
		settings, err := appSettings.GetInstance()
		if err != nil {
			return nil, err
		}
		obj, err := CreateObject(server, settings)
		if err != nil {
			return nil, err
		}
		if feature, ok := obj.(features.Feature); ok {
			server.AddFeature(feature)
		}
	}

	if server.GetFeature(dns.ClientType()) == nil {
		server.AddFeature(dns.LocalClient{})
	}

	if server.GetFeature(policy.ManagerType()) == nil {
		server.AddFeature(policy.DefaultManager{})
	}

	if server.GetFeature(routing.RouterType()) == nil {
		server.AddFeature(routing.DefaultRouter{})
	}

	// Add an empty instance at the end, for optional feature requirement.
	server.AddFeature(&Instance{})

	if server.featureResolutions != nil {
		return nil, newError("not all dependency are resolved.")
	}

	if err := addInboundHandlers(server, config.Inbound); err != nil {
		return nil, err
	}

	if err := addOutboundHandlers(server, config.Outbound); err != nil {
		return nil, err
	}

	return server, nil
}

// Type implements common.HasType.
func (s *Instance) Type() interface{} {
	return ServerType()
}

// Close shutdown the V2Ray instance.
func (s *Instance) Close() error {
{'fraction_non_alphanumeric': 0.12212589521296645, 'fraction_numerical': 0.003769317753486619, 'mean_word_length': 3.7664670658682633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3445972', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2500, 'n_words': 711}