|
|
@@ -7,6 +7,7 @@ import java.io.IOException; |
|
|
import java.io.PrintWriter; |
|
|
import java.io.PrintWriter; |
|
|
import java.time.LocalTime; |
|
|
import java.time.LocalTime; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
|
|
import java.util.Date; |
|
|
import java.util.Enumeration; |
|
|
import java.util.Enumeration; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.Iterator; |
|
|
import java.util.Iterator; |
|
|
@@ -14,6 +15,8 @@ import java.util.Map; |
|
|
import java.util.Properties; |
|
|
import java.util.Properties; |
|
|
import java.util.Vector; |
|
|
import java.util.Vector; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
|
|
import java.util.regex.Matcher; |
|
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext; |
|
|
import javax.servlet.ServletContext; |
|
|
import javax.servlet.ServletException; |
|
|
import javax.servlet.ServletException; |
|
|
@@ -23,7 +26,6 @@ import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger; |
|
|
import org.apache.log4j.Logger; |
|
|
import org.apache.log4j.PropertyConfigurator; |
|
|
import org.apache.log4j.PropertyConfigurator; |
|
|
import org.json.simple.JSONArray; |
|
|
|
|
|
import org.json.simple.JSONObject; |
|
|
import org.json.simple.JSONObject; |
|
|
import org.json.simple.parser.JSONParser; |
|
|
import org.json.simple.parser.JSONParser; |
|
|
|
|
|
|
|
|
@@ -102,8 +104,6 @@ public abstract class CommEngine extends HttpServlet |
|
|
{ |
|
|
{ |
|
|
while (!threadShouldStop) |
|
|
while (!threadShouldStop) |
|
|
{ |
|
|
{ |
|
|
long startTime = System.currentTimeMillis();; |
|
|
|
|
|
|
|
|
|
|
|
String timeOfDay = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")); |
|
|
String timeOfDay = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")); |
|
|
// Check for pause |
|
|
// Check for pause |
|
|
for (Broadcast broadcast : broadcasts.values()) |
|
|
for (Broadcast broadcast : broadcasts.values()) |
|
|
@@ -123,9 +123,8 @@ public abstract class CommEngine extends HttpServlet |
|
|
else if (timeOfDay.equals(daily_resume)) broadcast.resume("clock", null); |
|
|
else if (timeOfDay.equals(daily_resume)) broadcast.resume("clock", null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Wakes up every 1/2 minute to provide minute resolution |
|
|
|
|
|
long currentTime = System.currentTimeMillis(); |
|
|
long currentTime = System.currentTimeMillis(); |
|
|
long sleepTime = startTime + 30*1000 - currentTime; |
|
|
|
|
|
|
|
|
long sleepTime = 60000 + 30000 - currentTime % 60000; |
|
|
if (sleepTime > 0) |
|
|
if (sleepTime > 0) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
@@ -251,11 +250,7 @@ public abstract class CommEngine extends HttpServlet |
|
|
notInService = true; |
|
|
notInService = true; |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Set up periodic purge of stale broadcasts, based on deadBroadcastViewingMinutes |
|
|
|
|
|
String periodStr = config.getProperty("dead_broadcast_viewing_period", Long.toString(DEAD_BROADCAST_VIEWING_PERIOD_DEFAULT)); |
|
|
|
|
|
deadBroadcastViewingMinutes = Long.parseLong(periodStr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String timeOfDay, timeOfDayStr, key; |
|
|
String timeOfDay, timeOfDayStr, key; |
|
|
key = DAILY_RESUME_KEY; |
|
|
key = DAILY_RESUME_KEY; |
|
|
timeOfDayStr = config.getProperty(key , ""); |
|
|
timeOfDayStr = config.getProperty(key , ""); |
|
|
@@ -283,7 +278,18 @@ public abstract class CommEngine extends HttpServlet |
|
|
dailyClock = new DailyClock(); |
|
|
dailyClock = new DailyClock(); |
|
|
dailyClock.start(); |
|
|
dailyClock.start(); |
|
|
|
|
|
|
|
|
initChild(); |
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
// Set up periodic purge of stale broadcasts, based on deadBroadcastViewingMinutes |
|
|
|
|
|
String periodStr = config.getProperty("dead_broadcast_viewing_period", Long.toString(DEAD_BROADCAST_VIEWING_PERIOD_DEFAULT)); |
|
|
|
|
|
deadBroadcastViewingMinutes = Long.parseLong(periodStr); |
|
|
|
|
|
|
|
|
|
|
|
initChild(); |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception e) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ServletException(e.getMessage(), e); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public int getPauseThreshold() |
|
|
public int getPauseThreshold() |
|
|
@@ -463,29 +469,40 @@ public abstract class CommEngine extends HttpServlet |
|
|
out.print(configuration); |
|
|
out.print(configuration); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private JSONObject getConfigJSON() |
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
private JSONObject getConfigJSON() |
|
|
{ |
|
|
{ |
|
|
Map<Object, Object> dataMap = new HashMap<Object, Object>(); |
|
|
|
|
|
|
|
|
JSONObject config = new JSONObject(); |
|
|
|
|
|
|
|
|
// engine configuration |
|
|
// engine configuration |
|
|
Map<Object, Object> EngineMap = new HashMap<Object, Object>(); |
|
|
|
|
|
dataMap.put("engine", EngineMap); |
|
|
|
|
|
EngineMap.put(DAILY_PAUSE_KEY, daily_pause); |
|
|
|
|
|
EngineMap.put(DAILY_RESUME_KEY, daily_resume); |
|
|
|
|
|
|
|
|
JSONObject EngineConfig = new JSONObject(); |
|
|
|
|
|
config.put("engine", EngineConfig); |
|
|
|
|
|
EngineConfig.put(DAILY_PAUSE_KEY, daily_pause); |
|
|
|
|
|
EngineConfig.put(DAILY_RESUME_KEY, daily_resume); |
|
|
|
|
|
|
|
|
// broadcast configuration |
|
|
// broadcast configuration |
|
|
Map<Object, Object> broadcastsMap = new HashMap<Object, Object>(); |
|
|
|
|
|
|
|
|
JSONObject broadcastsConfig = new JSONObject(); |
|
|
synchronized (broadcasts) { |
|
|
synchronized (broadcasts) { |
|
|
for (String broadcastId : broadcasts.keySet()) |
|
|
for (String broadcastId : broadcasts.keySet()) |
|
|
{ |
|
|
{ |
|
|
Broadcast broadcast = broadcasts.get(broadcastId); |
|
|
Broadcast broadcast = broadcasts.get(broadcastId); |
|
|
if (broadcast.getState().isFinal) continue; |
|
|
if (broadcast.getState().isFinal) continue; |
|
|
JSONObject configJSON = broadcast.getConfigJSON(); |
|
|
|
|
|
broadcastsMap.put(broadcastId, configJSON); |
|
|
|
|
|
|
|
|
broadcastsConfig.put(broadcastId, broadcast.getConfigJSON()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (broadcastsMap.size() > 0) dataMap.put("broadcasts", broadcastsMap); |
|
|
|
|
|
return new JSONObject(dataMap); |
|
|
|
|
|
|
|
|
if (broadcastsConfig.size() > 0) config.put("broadcasts", broadcastsConfig); |
|
|
|
|
|
|
|
|
|
|
|
childAddConfig(config); |
|
|
|
|
|
|
|
|
|
|
|
return config; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Derived class may add to configMap |
|
|
|
|
|
* @param configMap |
|
|
|
|
|
*/ |
|
|
|
|
|
protected void childAddConfig(JSONObject config) |
|
|
|
|
|
{ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void cancelBroadcast(HttpServletRequest request, PrintWriter out) |
|
|
private void cancelBroadcast(HttpServletRequest request, PrintWriter out) |
|
|
@@ -538,7 +555,13 @@ public abstract class CommEngine extends HttpServlet |
|
|
protected static String checkTimeOfDay(String timeOfDay) { |
|
|
protected static String checkTimeOfDay(String timeOfDay) { |
|
|
timeOfDay = timeOfDay.trim(); |
|
|
timeOfDay = timeOfDay.trim(); |
|
|
if (timeOfDay.length() == 0) return timeOfDay; |
|
|
if (timeOfDay.length() == 0) return timeOfDay; |
|
|
if (timeOfDay.length() != 5) return null; |
|
|
|
|
|
|
|
|
// pattern hh:mm |
|
|
|
|
|
Pattern pattern = Pattern.compile("^(\\d\\d):[0-5]\\d$"); |
|
|
|
|
|
Matcher matcher = pattern.matcher(timeOfDay); |
|
|
|
|
|
if (!matcher.find()) return null; |
|
|
|
|
|
// Check hour in range |
|
|
|
|
|
String hh = matcher.group(1); |
|
|
|
|
|
if (Integer.parseInt(hh) > 23) return null; |
|
|
return timeOfDay; |
|
|
return timeOfDay; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -548,7 +571,8 @@ public abstract class CommEngine extends HttpServlet |
|
|
* @param out |
|
|
* @param out |
|
|
*/ |
|
|
*/ |
|
|
protected void configure(HttpServletRequest request, PrintWriter out) { |
|
|
protected void configure(HttpServletRequest request, PrintWriter out) { |
|
|
JSONObject currConfig = getConfigJSON(); |
|
|
|
|
|
|
|
|
// save original configuration for roll back in case of error |
|
|
|
|
|
JSONObject origConfigJSON = getConfigJSON(); |
|
|
|
|
|
|
|
|
String jsonString = request.getParameter("data"); |
|
|
String jsonString = request.getParameter("data"); |
|
|
try { |
|
|
try { |
|
|
@@ -558,9 +582,9 @@ public abstract class CommEngine extends HttpServlet |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
myLogger.error(e); |
|
|
myLogger.error(e); |
|
|
out.write("Error - " + e.getMessage()); |
|
|
out.write("Error - " + e.getMessage()); |
|
|
// restore to original confiuration |
|
|
|
|
|
|
|
|
// restore current confiuration |
|
|
try { |
|
|
try { |
|
|
configure(currConfig); |
|
|
|
|
|
|
|
|
configure(origConfigJSON); |
|
|
} catch (Exception e1) { |
|
|
} catch (Exception e1) { |
|
|
out.write("\nInternal error in restoring original configuration: " + e1.getMessage()); |
|
|
out.write("\nInternal error in restoring original configuration: " + e1.getMessage()); |
|
|
} |
|
|
} |
|
|
@@ -601,9 +625,19 @@ public abstract class CommEngine extends HttpServlet |
|
|
broadcast.configure(broadcastConfig); |
|
|
broadcast.configure(broadcastConfig); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
// derived class |
|
|
|
|
|
configureChild(configuration); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
* Derived class updates itself from given configuration. |
|
|
|
|
|
* @param configuration |
|
|
|
|
|
*/ |
|
|
|
|
|
protected void configureChild(JSONObject configuration) |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
* <CallEngine_status> |
|
|
* <CallEngine_status> |
|
|
* status of each broadcast |
|
|
* status of each broadcast |
|
|
* <calls><total>ttt</total><connected>nnn</connected> |
|
|
* <calls><total>ttt</total><connected>nnn</connected> |
|
|
@@ -656,6 +690,7 @@ public abstract class CommEngine extends HttpServlet |
|
|
out.write(broadcast.mkStatusReport() + "\n"); |
|
|
out.write(broadcast.mkStatusReport() + "\n"); |
|
|
} |
|
|
} |
|
|
out.write("<job_summary completed='" + getCompletedJobCount() + "' pending='" + getPendingJobCount() + "' active='" + getActiveJobCount() + "'/>\n"); |
|
|
out.write("<job_summary completed='" + getCompletedJobCount() + "' pending='" + getPendingJobCount() + "' active='" + getActiveJobCount() + "'/>\n"); |
|
|
|
|
|
out.write("<engine_clock><daily_pause>" + daily_pause + "</daily_pause><daily_resume>" + daily_resume + "</daily_resume></engine_clock>\n"); |
|
|
out.write("</" + tag + ">"); |
|
|
out.write("</" + tag + ">"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|