Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
szabozee
committed
Jul 22, 2017
1 parent
0b4d29d
commit 8497de1
Showing
55 changed files
with
1,885 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/sh | ||
CP="../java-examples/lib/*:classes" | ||
SP=src/ | ||
|
||
/bin/rm -f ../xtrabytes-dicom.jar | ||
/bin/rm -rf classes | ||
/bin/mkdir -p classes/ | ||
|
||
javac -encoding utf8 -sourcepath "${SP}" -classpath "${CP}" -d classes/ src/*.java || exit 1 | ||
|
||
echo "XtraBYtes DICOM java library compiled successfully" | ||
|
||
jar cf ../java-examples/lib/xtrabytes-dicom.jar -C classes . || exit 1 | ||
/bin/rm -rf classes | ||
|
||
echo "XtraBYtes DICOM java library generated successfully" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package XtraBYtes; | ||
|
||
import XtraBYtes.XtraBYtesDservlet; | ||
import XtraBYtes.XtraBYtesDsignals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.JSONArray; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.handler.AbstractHandler; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.server.handler.ResourceHandler; | ||
import org.eclipse.jetty.server.handler.DefaultHandler; | ||
import org.eclipse.jetty.server.handler.HandlerList; | ||
import org.eclipse.jetty.servlet.ServletHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class XtraBYtesDICOM extends XtraBYtesDsignals { | ||
|
||
public String ViewPageOverviewContent() { return ""; } | ||
|
||
private static long HeartbeatTimestamp = 0; | ||
|
||
public static void UpdateHeartbeatTimestamp(long timestamp) { | ||
HeartbeatTimestamp = timestamp; | ||
} | ||
|
||
private String AcceptedMSG = "Module accepted"; | ||
private String AcceptedMSG2 = "STOP FLOOD!"; | ||
|
||
private JSONObject ModuleConnectionRequest = new JSONObject(); | ||
|
||
private Server DICOMserver; | ||
|
||
// 0 = Invalid 1 = Connected 2 = Disconnected | ||
private int ModuleStatus=0; | ||
|
||
private boolean ModuleWatchdogStarted = false; | ||
|
||
private ScheduledExecutorService CronThreads = Executors.newScheduledThreadPool(1); | ||
|
||
private final Runnable ModuleWatchdog = new Runnable() { | ||
public void run() { | ||
if (( ModuleStatus == 1 ) && ((System.currentTimeMillis()-HeartbeatTimestamp)>2000)) { | ||
ModuleStatus = 2; | ||
System.out.print("Connection lost. Reconnecting."); | ||
reconnect(); | ||
} | ||
} | ||
}; | ||
|
||
public boolean connect(String ModuleName, String ModuleID, int ModulePort, String[] Signals){ | ||
|
||
if (!ModuleWatchdogStarted) { | ||
CronThreads.scheduleWithFixedDelay(ModuleWatchdog, 0, 1, TimeUnit.SECONDS); | ||
ModuleWatchdogStarted = true; | ||
} | ||
|
||
ModuleConnectionRequest.put("DICOM","Module-Registration"); | ||
ModuleConnectionRequest.put("module-name",ModuleName); | ||
ModuleConnectionRequest.put("module-id",ModuleID); | ||
ModuleConnectionRequest.put("dicom-port",ModulePort); | ||
JSONArray signal_wrapper = new JSONArray(); | ||
for (String signal: Signals) { | ||
signal_wrapper.add(signal); | ||
} | ||
ModuleConnectionRequest.put("signals",signal_wrapper); | ||
|
||
while (true) { | ||
XtraBYtesDservlet listenner = new XtraBYtesDservlet(); | ||
JSONObject response = listenner.SendJsonQuery(ModuleConnectionRequest); | ||
if ((AcceptedMSG.equals((String)response.get("DICOM"))) || (AcceptedMSG2.equals((String)response.get("DICOM")))) { | ||
System.out.println("OK"); | ||
DICOMserver = new Server(); | ||
ServerConnector connector; | ||
connector = new ServerConnector(DICOMserver); | ||
|
||
connector.setPort(ModulePort); | ||
connector.setHost("127.0.0.1"); | ||
connector.setIdleTimeout(15000); | ||
connector.setReuseAddress(true); | ||
DICOMserver.addConnector(connector); | ||
|
||
HandlerList DICOMHandlers = new HandlerList(); | ||
|
||
ServletContextHandler DICOMHandler = new ServletContextHandler(); | ||
|
||
DICOMHandler.addServlet(XtraBYtesDservlet.class, "/dicom"); | ||
|
||
DICOMHandlers.addHandler(DICOMHandler); | ||
|
||
DICOMHandlers.addHandler(new DefaultHandler()); | ||
|
||
DICOMserver.setHandler(DICOMHandlers); | ||
DICOMserver.setStopAtShutdown(true); | ||
|
||
try { | ||
HeartbeatTimestamp = System.currentTimeMillis(); | ||
DICOMserver.start(); | ||
System.out.println(ModuleName + " DICOM server started at " + ModulePort + " port."); | ||
ModuleStatus = 1; | ||
return true; | ||
} catch (Exception e) { | ||
System.out.println("Failed to start DICOM server."); | ||
return false; | ||
} | ||
|
||
} // if | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException ex) { } | ||
System.out.print("."); | ||
} | ||
} | ||
|
||
private void reconnect() { | ||
while (true) { | ||
XtraBYtesDservlet listenner = new XtraBYtesDservlet(); | ||
JSONObject response = listenner.SendJsonQuery(ModuleConnectionRequest); | ||
if ((AcceptedMSG.equals((String)response.get("DICOM"))) || (AcceptedMSG2.equals((String)response.get("DICOM")))) { | ||
System.out.println("Reconnected."); | ||
ModuleStatus = 1; | ||
HeartbeatTimestamp = System.currentTimeMillis(); | ||
return; | ||
} | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException ex) { } | ||
System.out.print("."); | ||
} | ||
} | ||
|
||
public void startDICOM(String ModuleName, String ModuleID, int ModulePort, String[] Signals){ | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
shutdown(); | ||
} | ||
})); | ||
|
||
try { | ||
System.out.println("*** Starting XtraBYtes module."); | ||
if (!connect( ModuleName, ModuleID, ModulePort, Signals )) throw new Exception("Module registration failed."); | ||
System.out.println("*** XtraBYtes module started successfully."); | ||
} catch (Exception e) { | ||
System.out.println("*** ERROR: "+e.toString()); | ||
System.out.println("*** XtraBYtes module start FAILED."); | ||
} | ||
|
||
} | ||
|
||
public void shutdown() { | ||
CronThreads.shutdown(); | ||
System.out.println("*** XtraBYtes module stopped."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package XtraBYtes; | ||
|
||
import XtraBYtes.XtraBYtesDICOM; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.parser.JSONParser; | ||
import org.json.simple.parser.ParseException; | ||
|
||
import java.io.*; | ||
import java.io.IOException; | ||
|
||
import javax.servlet.http.*; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
|
||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.client.api.ContentResponse; | ||
import org.eclipse.jetty.client.util.StringContentProvider; | ||
|
||
import java.net.*; | ||
import java.net.InetSocketAddress; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.MalformedURLException; | ||
|
||
|
||
public class XtraBYtesDservlet extends HttpServlet { | ||
|
||
public void doGet(HttpServletRequest request, HttpServletResponse response) | ||
|
||
throws ServletException, IOException { | ||
response.setContentType("text/html"); | ||
PrintWriter ServletOutputStream = response.getWriter(); | ||
ServletOutputStream.println("<h1>" + "The specified method is not allowed." + "</h1>"); | ||
} | ||
|
||
public void doPost(HttpServletRequest request, HttpServletResponse response) | ||
|
||
throws ServletException, IOException { | ||
JSONObject JsonRequest = new JSONObject(); | ||
try { | ||
JsonRequest = (JSONObject)new JSONParser().parse(request.getReader()); | ||
|
||
} catch (ParseException e) { e.printStackTrace(); } | ||
|
||
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private"); | ||
response.setHeader("Pragma", "no-cache"); | ||
response.setDateHeader("Expires", 0); | ||
response.setContentType("text/plain; charset=UTF-8"); | ||
|
||
JSONObject JsonResponse = new JSONObject(); | ||
|
||
switch ( (String) JsonRequest.get("DICOM") ) { | ||
case "Heartbeat-Echo-Request": { | ||
JsonResponse = DICOM_HeartbeatReq(JsonRequest); | ||
} break; | ||
case "view-page-overview-content": { | ||
JsonResponse = DICOM_ViewPageOverviewContent(JsonRequest); | ||
} break; | ||
|
||
default: { | ||
JsonResponse.put("error","Bad requestType."); | ||
} break; | ||
} | ||
|
||
response.setContentType("text"); | ||
PrintWriter ServletOutputStream = response.getWriter(); | ||
|
||
ServletOutputStream.print(JsonResponse.toString()); | ||
} | ||
|
||
public synchronized JSONObject SendJsonQuery(JSONObject request) { | ||
|
||
JSONObject JSONresponse = new JSONObject(); | ||
|
||
try { | ||
HttpClient client = new HttpClient(); | ||
client.setBindAddress(new InetSocketAddress( InetAddress.getByName("127.0.0.1") , 0 )); | ||
client.start(); | ||
ContentResponse response = client.POST("http://127.0.0.1:3344/dicom") | ||
.content(new StringContentProvider(request.toString()) , "application/json; charset=UTF-8") | ||
.send(); | ||
client.stop(); | ||
if ( response.getStatus() == HttpURLConnection.HTTP_OK ) { | ||
try { | ||
JSONresponse = (JSONObject)new JSONParser().parse(response.getContentAsString()); | ||
} catch (Exception e) { | ||
JSONresponse.put("Error","Failed parsing returned JSON object."); | ||
} | ||
} | ||
} catch (Exception e) { | ||
JSONresponse.put("Error","Communication error."); | ||
} | ||
|
||
return JSONresponse; | ||
} | ||
|
||
|
||
private JSONObject DICOM_HeartbeatReq( JSONObject JsonRequest ) { | ||
|
||
JSONObject response = new JSONObject(); | ||
|
||
try { | ||
response.put("DICOM", "Heartbeat-Echo-Reply"); | ||
XtraBYtesDICOM.UpdateHeartbeatTimestamp(System.currentTimeMillis()); | ||
} catch (Exception e) { | ||
response.put("error",1); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
|
||
public JSONObject DICOM_ViewPageOverviewContent( JSONObject JsonRequest ) { | ||
|
||
JSONObject response = new JSONObject(); | ||
|
||
try { | ||
response.put("DICOM", "view-page-overview-content"); | ||
response.put("view-page-overview-content", XtraBYtesDICOM.getSignalHandler().ViewPageOverviewContent()); | ||
} catch (Exception e) { | ||
response.put("error",1); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package XtraBYtes; | ||
|
||
import XtraBYtes.XtraBYtesDICOM; | ||
|
||
import org.json.simple.JSONObject; | ||
|
||
public abstract class XtraBYtesDsignals { | ||
|
||
protected static XtraBYtesDsignals SignalHandler; | ||
|
||
public static XtraBYtesDsignals getSignalHandler() { | ||
return SignalHandler; | ||
} | ||
|
||
public void setSignalHandler(XtraBYtesDsignals sh) { | ||
SignalHandler = sh; | ||
} | ||
|
||
public abstract String ViewPageOverviewContent(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/sh | ||
CP="../lib/*:classes" | ||
SP=src/ | ||
|
||
/bin/rm -f example1.jar | ||
/bin/rm -rf classes | ||
/bin/mkdir -p classes/ | ||
|
||
javac -encoding utf8 -sourcepath "${SP}" -classpath "${CP}" -d classes/ src/*.java || exit 1 | ||
|
||
echo "XtraBYtes example1 module compiled." | ||
|
||
jar cf example1.jar -C classes . || exit 1 | ||
/bin/rm -rf classes | ||
|
||
echo "The xtrabytes-example1.jar generated successfully." |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@ECHO OFF | ||
IF EXIST java ( | ||
start "XTRABYTES EXAMPLE1" java -cp example1.jar;lib\* XtraBYtes.Example1 | ||
) ELSE ( | ||
ECHO Java software not found on your system. Please go to http://java.com/en/ to download a copy of Java. | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
java -cp example1.jar:../lib/* XtraBYtes.Example1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package XtraBYtes; | ||
|
||
import XtraBYtes.XtraBYtesDsignals; | ||
import XtraBYtes.XtraBYtesDICOM; | ||
|
||
public class Example1 { | ||
|
||
public static class XBYsignals extends XtraBYtesDsignals { | ||
|
||
public String ViewPageOverviewContent() { | ||
return "Hello XtraBYtes ;-)"; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
XtraBYtesDICOM module = new XtraBYtesDICOM(); | ||
module.setSignalHandler((XtraBYtesDsignals)new XBYsignals()); | ||
module.startDICOM( | ||
"Example1 Module", // Module name | ||
"example1", // Module ID | ||
10001, // Module DICOM port | ||
new String[]{ // Signals | ||
"view-page-overview-content" | ||
} | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.