/*
 *
 * Copyright 2024 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package channelz

import (
	
	
)

// Server is the channelz representation of a server.
type Server struct {
	Entity
	ID      int64
	RefName string

	ServerMetrics ServerMetrics

	closeCalled   bool
	sockets       map[int64]string
	listenSockets map[int64]string
	cm            *channelMap
}

// ServerMetrics defines a struct containing metrics for servers.
type ServerMetrics struct {
	// The number of incoming calls started on the server.
	CallsStarted atomic.Int64
	// The number of incoming calls that have completed with an OK status.
	CallsSucceeded atomic.Int64
	// The number of incoming calls that have a completed with a non-OK status.
	CallsFailed atomic.Int64
	// The last time a call was started on the server.
	LastCallStartedTimestamp atomic.Int64
}

// NewServerMetricsForTesting returns an initialized ServerMetrics.
func (, , ,  int64) *ServerMetrics {
	 := &ServerMetrics{}
	.CallsStarted.Store()
	.CallsSucceeded.Store()
	.CallsFailed.Store()
	.LastCallStartedTimestamp.Store()
	return 
}

// CopyFrom copies the metrics data from the provided ServerMetrics
// instance into the current instance.
func ( *ServerMetrics) ( *ServerMetrics) {
	.CallsStarted.Store(.CallsStarted.Load())
	.CallsSucceeded.Store(.CallsSucceeded.Load())
	.CallsFailed.Store(.CallsFailed.Load())
	.LastCallStartedTimestamp.Store(.LastCallStartedTimestamp.Load())
}

// ListenSockets returns the listening sockets for s.
func ( *Server) () map[int64]string {
	db.mu.RLock()
	defer db.mu.RUnlock()
	return copyMap(.listenSockets)
}

// String returns a printable description of s.
func ( *Server) () string {
	return fmt.Sprintf("Server #%d", .ID)
}

func ( *Server) () int64 {
	return .ID
}

func ( *Server) ( int64,  entry) {
	switch v := .(type) {
	case *Socket:
		switch .SocketType {
		case SocketTypeNormal:
			.sockets[] = .RefName
		case SocketTypeListen:
			.listenSockets[] = .RefName
		}
	default:
		logger.Errorf("cannot add a child (id = %d) of type %T to a server", , )
	}
}

func ( *Server) ( int64) {
	delete(.sockets, )
	delete(.listenSockets, )
	.deleteSelfIfReady()
}

func ( *Server) () {
	.closeCalled = true
	.deleteSelfIfReady()
}

func ( *Server) () {
	if !.closeCalled || len(.sockets)+len(.listenSockets) != 0 {
		return
	}
	.cm.deleteEntry(.ID)
}

func ( *Server) () int64 {
	return 0
}