]> git.argeo.org Git - gpl/argeo-slc.git/blob - AgentServiceImpl.java
9a292c0de9306d3a4d159f6562956e81a00a94e9
[gpl/argeo-slc.git] / AgentServiceImpl.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.services.impl;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.Executor;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.security.ArgeoSecurityService;
26 import org.argeo.slc.dao.runtime.SlcAgentDescriptorDao;
27 import org.argeo.slc.runtime.SlcAgent;
28 import org.argeo.slc.runtime.SlcAgentDescriptor;
29 import org.argeo.slc.runtime.SlcAgentFactory;
30 import org.argeo.slc.services.AgentService;
31 import org.springframework.beans.factory.DisposableBean;
32 import org.springframework.beans.factory.InitializingBean;
33
34 public class AgentServiceImpl implements AgentService, InitializingBean,
35 DisposableBean {
36 private final static Log log = LogFactory.getLog(AgentServiceImpl.class);
37
38 private final SlcAgentDescriptorDao slcAgentDescriptorDao;
39 private final SlcAgentFactory agentFactory;
40
41 private Executor securityService;
42
43 private Long pingCycle = 20000l;
44
45 private Boolean pingThreadActive = true;
46
47 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao,
48 SlcAgentFactory agentFactory) {
49 this.slcAgentDescriptorDao = slcAgentDescriptorDao;
50 this.agentFactory = agentFactory;
51 }
52
53 public void register(SlcAgentDescriptor slcAgentDescriptor) {
54 if (slcAgentDescriptorDao.getAgentDescriptor(slcAgentDescriptor
55 .getUuid()) == null)
56 slcAgentDescriptorDao.create(slcAgentDescriptor);
57 log.info("Registered agent #" + slcAgentDescriptor.getUuid());
58 }
59
60 public void unregister(SlcAgentDescriptor slcAgentDescriptor) {
61 slcAgentDescriptorDao.delete(slcAgentDescriptor);
62 log.info("Unregistered agent #" + slcAgentDescriptor.getUuid());
63 }
64
65 public void afterPropertiesSet() throws Exception {
66 // if (pingCycle > 0)
67 // new PingThread().start();
68 if (pingCycle > 0) {
69 Thread authenticatedThread = new Thread("SLC Agents Ping") {
70 public void run() {
71 securityService.execute(new AgentsPing());
72 }
73 };
74 authenticatedThread.start();
75
76 }
77 }
78
79 public synchronized void destroy() throws Exception {
80 pingThreadActive = false;
81 notifyAll();
82 }
83
84 public void setPingCycle(Long pingCycle) {
85 this.pingCycle = pingCycle;
86 }
87
88 public void setSecurityService(Executor securityService) {
89 this.securityService = securityService;
90 }
91
92 protected class AgentsPing implements Runnable {
93 public void run() {
94
95 // FIXME: temporary hack so that the ping starts after the server
96 // has been properly started.
97 try {
98 Thread.sleep(10 * 1000);
99 } catch (InterruptedException e1) {
100 // silent
101 }
102
103 log.info("Start pinging agents.");
104 while (pingThreadActive) {
105 List<SlcAgentDescriptor> lst = slcAgentDescriptorDao
106 .listSlcAgentDescriptors();
107 List<String> agentIds = new ArrayList<String>();
108 for (SlcAgentDescriptor ad : lst)
109 agentIds.add(ad.getUuid());
110
111 if (log.isTraceEnabled())
112 log.trace("Ping " + agentIds.size() + " agent(s).");
113 for (String agentId : agentIds) {
114 SlcAgent agent = agentFactory.getAgent(agentId);
115 if (!agent.ping()) {
116 log.info("Agent " + agentId + " did not reply to ping,"
117 + " removing its descriptor...");
118 slcAgentDescriptorDao.delete(agentId);
119 }
120 }
121
122 lst = slcAgentDescriptorDao.listSlcAgentDescriptors();
123 agentIds = new ArrayList<String>();
124 for (SlcAgentDescriptor ad : lst)
125 agentIds.add(ad.getUuid());
126 agentFactory.pingAll(agentIds);
127
128 synchronized (AgentServiceImpl.this) {
129 try {
130 AgentServiceImpl.this.wait(pingCycle);
131 } catch (InterruptedException e) {
132 // silent
133 }
134 }
135 }
136 log.info("Stopped pinging agents.");
137 }
138
139 }
140
141 }