Class: Cosmos::Connections

Inherits:
Object show all
Defined in:
lib/cosmos/tools/cmd_tlm_server/connections.rb

Overview

Abstract base class for Routers and Interfaces. Since Routers are just Interfaces they share a lot of responsibilies which are captured here.

Direct Known Subclasses

Interfaces, Routers

Instance Method Summary collapse

Constructor Details

#initialize(type, cmd_tlm_server_config) ⇒ Connections

Returns a new instance of Connections.

Parameters:

  • cmd_tlm_server_config (CmdTlmServerConfig)

    The configuration which defines all the connections



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 20

def initialize(type, cmd_tlm_server_config)
  @config = cmd_tlm_server_config
  if type == :INTERFACES
    @connections = @config.interfaces
    @keyword = "interface"
  elsif type == :ROUTERS
    @connections = @config.routers
    @keyword = "router"
  else
    raise "Unknown type: #{type}. Must be :INTERFACES or :ROUTERS."
  end
end

Instance Method Details

#allHash<String, Interface>

Returns All the connections.

Returns:



125
126
127
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 125

def all
  @connections
end

#clear_countersObject

Clears the bytes written, bytes read, write count, and read count from each of the connections.



115
116
117
118
119
120
121
122
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 115

def clear_counters
  @connections.each do |connection_name, connection|
    connection.bytes_written = 0
    connection.bytes_read = 0
    connection.write_count = 0
    connection.read_count = 0
  end
end

#connect(connection_name, *params) ⇒ Object

Connect a connection by name

Parameters:

  • connection_name (String)

    Name of the connection

  • params (Array)

    Array of parameters to be passed to the #recreate method. Pass nothing to start the connection for the first time.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 54

def connect(connection_name, *params)
  connection = @connections[connection_name.upcase]
  raise "Unknown #{@keyword}: #{connection_name}" unless connection

  if params.empty?
    start_thread(connection) unless connection.thread
  else
    disconnect(connection_name)
    connection = recreate(connection_name, *params)
    start_thread(connection)
  end
end

#disconnect(connection_name) ⇒ Object

Disconnect a connection by name

Parameters:

  • connection_name (String)

    Name of the connection



80
81
82
83
84
85
86
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 80

def disconnect(connection_name)
  connection = @connections[connection_name.upcase]
  raise "Unknown #{@keyword}: #{connection_name}" unless connection

  stop_thread(connection)
  Logger.info "Disconnected from #{@keyword} #{connection_name.upcase}"
end

#namesArray<String>

Returns The names of all the connections.

Returns:



105
106
107
108
109
110
111
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 105

def names
  names = []
  @connections.each do |connection_name, connection|
    names << connection_name
  end
  return names.sort
end

#recreate(connection_name, *params) ⇒ Object

Recreate an interface with new initialization parameters. Must be implemented by a subclass.

Parameters:

  • connection_name (String)

    Name of the connection

  • params (Array)

    Array of parameters to pass to the connection constructor



73
74
75
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 73

def recreate(connection_name, *params)
  raise "Connections recreate method not implemented"
end

#startObject

Creates a thread for each connection which sends all commands received out on the connection interfaces



35
36
37
38
39
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 35

def start
  @connections.each do |connection_name, connection|
    connect(connection_name) if connection.connect_on_startup
  end
end

#start_raw_logging(connection_name = 'ALL') ⇒ Object

Start raw logging on a connection by name

Parameters:

  • connection_name (String) (defaults to: 'ALL')

    Name of the connection



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 132

def start_raw_logging(connection_name = 'ALL')
  connection_name_upcase = connection_name.upcase
  if connection_name == 'ALL'
    @connections.each do |_, connection|
      connection.start_raw_logging
    end
  else
    connection = @connections[connection_name_upcase]
    raise "Unknown #{@keyword}: #{connection_name}" unless connection
    connection.start_raw_logging
  end
end

#state(connection_name) ⇒ String

Get the state of a connection by name

Returns:

  • (String)

    Either 'CONNECTED', 'ATTEMPTING', or 'DISCONNECTED'.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 91

def state(connection_name)
  connection = @connections[connection_name.upcase]
  raise "Unknown #{@keyword}: #{connection_name}" unless connection

  if connection.connected?
    return 'CONNECTED'
  elsif connection.thread
    return 'ATTEMPTING'
  else
    return 'DISCONNECTED'
  end
end

#stopObject

Stop connections by disconnecting the interface and killing the thread



42
43
44
45
46
47
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 42

def stop
  @connections.each do |connection_name, connection|
    disconnect(connection_name)
    stop_raw_logging(connection_name)
  end
end

#stop_raw_logging(connection_name = 'ALL') ⇒ Object

Stop raw logging on a connection by name

Parameters:

  • connection_name (String) (defaults to: 'ALL')

    Name of the connection



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cosmos/tools/cmd_tlm_server/connections.rb', line 148

def stop_raw_logging(connection_name = 'ALL')
  connection_name_upcase = connection_name.upcase
  if connection_name == 'ALL'
    @connections.each do |_, connection|
      connection.stop_raw_logging
    end
  else
    connection = @connections[connection_name_upcase]
    raise "Unknown #{@keyword}: #{connection_name}" unless connection
    connection.stop_raw_logging
  end
end