agent.go 4.61 KB
Newer Older
Stefan Dähling's avatar
Stefan Dähling committed
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
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

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.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// defines functions necessary for agent execution like message handling,
// context storing and task execution

package agency

import (
	"log"
	"sync"

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/status"
)

// Agent holds information about an agent and implements functionality for agent execution
type Agent struct {
	mutex    *sync.Mutex
	id       int // unique id of agent
	nodeID   int
	name     string  // Name of agent
	aType    string  // Type of agent
	aSubtype string  // Subtype of agent
66
	custom   string  // custom data
Stefan Dähling's avatar
Stefan Dähling committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	masID    int     // ID of MAS agent is belongs to
	status   int     // Status of agent
	ACL      *ACL    // agent communication
	Logger   *Logger // logger object
	MQTT     *MQTT   // mqtt object
	DF       *DF
	logError *log.Logger
	logInfo  *log.Logger
}

// newAgent creates a new agent
func newAgent(info schemas.AgentInfo, msgIn chan schemas.ACLMessage,
	aclLookup func(int) (*ACL, error), log *loggerClient, logConfig schemas.LogConfig,
	mqtt *mqttClient, logErr *log.Logger, logInf *log.Logger) (ag *Agent) {
	ag = &Agent{
Stefan Dähling's avatar
Stefan Dähling committed
82
		id:       info.ID,
Stefan Dähling's avatar
Stefan Dähling committed
83
84
85
86
		nodeID:   info.Spec.NodeID,
		name:     info.Spec.Name,
		aType:    info.Spec.AType,
		aSubtype: info.Spec.ASubtype,
Stefan Dähling's avatar
Stefan Dähling committed
87
		masID:    info.MASID,
Stefan Dähling's avatar
Stefan Dähling committed
88
89
		custom:   info.Spec.Custom,
		mutex:    &sync.Mutex{},
Stefan Dähling's avatar
Stefan Dähling committed
90
		ACL:      newACL(info.ID, msgIn, aclLookup, logErr, logInf),
Stefan Dähling's avatar
Stefan Dähling committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		logError: logErr,
		logInfo:  logInf,
	}
	// in, out := ag.ACL.getCommDataChannels()
	ag.Logger = newLogger(ag.id, log, logConfig, ag.logError, ag.logInfo)
	ag.MQTT = newMQTT(ag.id, mqtt, ag.Logger, ag.logError, ag.logInfo)
	ag.DF = newDF(ag.masID, ag.id, ag.nodeID, ag.logError, ag.logInfo)
	return
}

// startAgent starts an agent. It requires an agent task to be executed and the ID of the agent
func (agent *Agent) startAgent(task func(*Agent) error) (err error) {
	go task(agent)
	agent.status = status.Running
	agent.logInfo.Println("Started agent ", agent.GetAgentID())
	return
}

// GetAgentID returns the agent ID
func (agent *Agent) GetAgentID() (ret int) {
Stefan Dähling's avatar
Stefan Dähling committed
111
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
112
	ret = agent.id
Stefan Dähling's avatar
Stefan Dähling committed
113
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
114
115
116
117
118
	return
}

// GetAgentType returns the agent type and subtype
func (agent *Agent) GetAgentType() (aType string, aSubtype string) {
Stefan Dähling's avatar
Stefan Dähling committed
119
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
120
121
	aType = agent.aType
	aSubtype = agent.aSubtype
Stefan Dähling's avatar
Stefan Dähling committed
122
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
123
124
125
126
127
	return
}

// GetAgentName returns the agent name
func (agent *Agent) GetAgentName() (ret string) {
Stefan Dähling's avatar
Stefan Dähling committed
128
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
129
	ret = agent.name
Stefan Dähling's avatar
Stefan Dähling committed
130
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
131
132
133
134
	return
}

// GetCustomData returns custom data
135
func (agent *Agent) GetCustomData() (ret string) {
Stefan Dähling's avatar
Stefan Dähling committed
136
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
137
	ret = agent.custom
Stefan Dähling's avatar
Stefan Dähling committed
138
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
139
140
141
142
143
144
145
	return
}

// Terminate terminates the agent
func (agent *Agent) Terminate() {
	agent.logInfo.Println("Terminating agent ", agent.GetAgentID())
	agent.ACL.close()
146
147
	agent.Logger.close()
	agent.MQTT.close()
Stefan Dähling's avatar
Stefan Dähling committed
148
}