Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ControlServices comply with autostart server settings #705

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletions dicoogle/src/main/java/pt/ua/dicoogle/server/ControlServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public class ControlServices {
private static ControlServices instance = null;
// Services vars
private DicomStorage storage = null;
private boolean webServicesRunning = false;
private boolean webServerRunning = false;
private QueryRetrieve retrieve = null;

private DicoogleWeb webServices;

private ControlServices() {
startInicialServices();
autoStartServices();
}

public static synchronized ControlServices getInstance() {
Expand All @@ -60,30 +59,26 @@ public static synchronized ControlServices getInstance() {
}


/* Strats the inicial services based on ServerSettingsManager */
private void startInicialServices() {
/* Starts the initial services based on server settings. */
private void autoStartServices() {
ServerSettings settings = ServerSettingsManager.getSettings();

try {
/* if (settings.isP2P())
{
startP2P();
}*/
ServerSettings.DicomServices dicomSettings = settings.getDicomServicesSettings();

if (!this.storageIsRunning()) {
if (!this.storageIsRunning() && dicomSettings.getStorageSettings().isAutostart()) {
try {
startStorage();
} catch (IOException ex) {
logger.error("Failed to start the DICOM storage service", ex);
}
}

if (!this.queryRetrieveIsRunning()) {
startQueryRetrieve();
}

if (!this.webServerIsRunning()) {
startWebServer();
}
if (!this.queryRetrieveIsRunning() && dicomSettings.getQueryRetrieveSettings().isAutostart()) {
startQueryRetrieve();
}

} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
if (!this.webServerIsRunning() && settings.getWebServerSettings().isAutostart()) {
startWebServer();
}
}

Expand All @@ -104,38 +99,33 @@ public boolean stopAllServices() {
}

/**
*
* @return 0 - if everything is fine and the service was started
* -1 - if the server's storage path is not defined
* -2 - service is already running
*
* @throws IOException
* @return `true` if everything is fine and the service was started,
* `false` if the service was already running
* @throws IOException if the storage service could not start
*/
public int startStorage() throws IOException {
public boolean startStorage() throws IOException {
if (storage == null) {
ServerSettings settings = ServerSettingsManager.getSettings();


SOPList list = SOPList.getInstance();
list.readFromSettings(settings);

int i;

List l = list.getKeys();
List<String> l = list.getKeys();
String[] keys = new String[l.size()];

int i;
for (i = 0; i < l.size(); i++) {
keys[i] = (String) l.get(i);
keys[i] = l.get(i);
}
storage = new DicomStorage(keys, list);
storage.start();

logger.info("Starting DICOM Storage SCP");

return 0;
return true;
}

return -2;
return false;
}

public void stopStorage() {
Expand Down Expand Up @@ -175,15 +165,13 @@ public boolean webServerIsRunning() {
return webServerRunning;
}

// TODO: Review those below!
public void startWebServer() {
logger.info("Starting WebServer");

try {
if (webServices == null) {
webServices = new DicoogleWeb(ServerSettingsManager.getSettings().getWebServerSettings().getPort());
webServerRunning = true;
webServicesRunning = true;
logger.info("Starting Dicoogle Web");
}
} catch (Exception ex) {
Expand All @@ -197,7 +185,6 @@ public void stopWebServer() {

if (webServices != null) {
try {
webServicesRunning = false;
webServerRunning = false;

webServices.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,32 +160,45 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

// update running
if (updateRunning) {
switch (mType) {
case STORAGE:
if (running) {
controlServices.startStorage();
obj.element("running", true);
} else {
controlServices.stopStorage();
obj.element("running", false);
}
break;

case QUERY:
if (running) {
controlServices.startQueryRetrieve();
obj.element("running", true);

} else {
controlServices.stopQueryRetrieve();
obj.element("running", false);
}
break;

default:
break;
try {
switch (mType) {
case STORAGE:
if (running) {
boolean out = controlServices.startStorage();
if (!out) {
resp.addHeader("Warning", "Service was already running");
obj.element("warning", "Service was already running");
}
obj.element("running", true);
} else {
controlServices.stopStorage();
obj.element("running", false);
}
break;

case QUERY:
if (running) {
controlServices.startQueryRetrieve();
obj.element("running", true);

} else {
controlServices.stopQueryRetrieve();
obj.element("running", false);
}
break;

default:
break;
}
} catch (Exception ex) {
obj.element("success", false);
obj.element("error", ex.getMessage());
reply(resp, 500, obj);
return;
}
}

// finalize
ServerSettingsManager.saveSettings();
obj.element("success", true);
reply(resp, 200, obj);
Expand Down
Loading