Search in sources :

Example 1 with AlertMenu

use of menus.AlertMenu in project DistributedFractalNetwork by Budder21.

the class Layer method initializeFractalRegistry.

//TODO: test error message
/**
	 * This static method must be called at startup of the application if any
	 * Layers are to be used. It looks at the specified directory for all custom
	 * layer files, compiles them, and loads Class representations of them into
	 * the registry so they can be created later. If errors are thrown, it might
	 * be because a jdk is not being used. This method will only work if a jdk
	 * is installed
	 */
public static void initializeFractalRegistry() {
    while (fractalRegistry.size() != 0) fractalRegistry.remove(0);
    fractalRegistry.add(HistogramLayer.class);
    fractalRegistry.add(TriangleAverageLayer.class);
    try {
        File fractalFolder = new File(Constants.CUSTOM_FRACTAL_FILEPATH);
        if (!fractalFolder.exists()) {
            fractalFolder.mkdirs();
            fractalFolder.createNewFile();
        }
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(ClassLoader.getSystemClassLoader(), new Object[] { fractalFolder.toURI().toURL() });
        File[] fractals = fractalFolder.listFiles();
        File classFolder = new File(Constants.CUSTOM_FRACTAL_FILEPATH + "fractal/");
        if (!classFolder.exists()) {
            classFolder.mkdirs();
            classFolder.createNewFile();
        }
        DynamicCompiler.classOutputFolder = Constants.CUSTOM_FRACTAL_FILEPATH;
        for (File f : fractals) {
            if (!f.isDirectory()) {
                String name = f.getName().substring(0, f.getName().indexOf("."));
                File classFile = new File(Constants.CUSTOM_FRACTAL_FILEPATH + "fractal/" + name + ".class");
                if (classFile.exists())
                    classFile.delete();
                DynamicCompiler.name = f.getName().substring(0, f.getName().indexOf("."));
                String classContents = new String(Files.readAllBytes(f.toPath()));
                JavaFileObject file = DynamicCompiler.getJavaFileObject(classContents);
                Iterable<? extends JavaFileObject> files = Arrays.asList(file);
                try {
                    DynamicCompiler.compile(files);
                } catch (NullPointerException e) {
                    AlertMenu alert = new AlertMenu("Unable to compile custom layer types.", "Please try again using a JDK insteaded of a JRE.");
                }
            }
        }
        URL[] urlArray = { classFolder.toURI().toURL() };
        fractals = classFolder.listFiles();
        URLClassLoader classLoader = new URLClassLoader(urlArray);
        for (File f : classFolder.listFiles()) {
            String name = f.getName();
            if (name.indexOf("$") == -1)
                try {
                    Class<? extends Layer> instance = (Class<? extends Layer>) classLoader.loadClass("fractal." + name.substring(0, name.indexOf(".")));
                    fractalRegistry.add(instance);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Method(java.lang.reflect.Method) URL(java.net.URL) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaFileObject(javax.tools.JavaFileObject) URLClassLoader(java.net.URLClassLoader) File(java.io.File) AlertMenu(menus.AlertMenu)

Example 2 with AlertMenu

use of menus.AlertMenu in project DistributedFractalNetwork by Budder21.

the class Layer method registerLayer.

//TODO: test method
/**
	 * Used to register a new layer type
	 * 
	 * @param file
	 *            a file pointing to the layer being registered
	 * @return whether or not the layer was succesfully registered. Returns
	 *         false if the layer was already registered
	 */
public static boolean registerLayer(File file) {
    String name = file.getName().substring(0, file.getName().indexOf("."));
    if (Layer.isValidLayer(name))
        return false;
    File classFile = new File(Constants.CUSTOM_FRACTAL_FILEPATH + "fractal/" + name + ".class");
    if (classFile.exists())
        classFile.delete();
    DynamicCompiler.name = name;
    String classContents;
    try {
        classContents = new String(Files.readAllBytes(file.toPath()));
        JavaFileObject fi = DynamicCompiler.getJavaFileObject(classContents);
        Iterable<? extends JavaFileObject> files = Arrays.asList(fi);
        try {
            DynamicCompiler.compile(files);
        } catch (NullPointerException e) {
            AlertMenu alert = new AlertMenu("Unable to compile custom layer types.", "Please try again using a JDK insteaded of a JRE.");
        }
        fractalRegistry.add(DynamicCompiler.instanceOf());
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) IOException(java.io.IOException) File(java.io.File) AlertMenu(menus.AlertMenu)

Example 3 with AlertMenu

use of menus.AlertMenu in project DistributedFractalNetwork by Budder21.

the class Client method initializeServer.

/**
	 * Connects to the server by broadcasting on a pre-determined port to send the server its ipAddress and
	 * then waiting for the server to connect to it using conventional Sockets. Creates the various listeners
	 * for the SocketWrapper as well.
	 */
private void initializeServer() {
    try {
        DatagramSocket socket = new DatagramSocket();
        socket.setBroadcast(true);
        byte[] message = "JOIN_REQUEST".getBytes();
        DatagramPacket packet = new DatagramPacket(message, message.length, Utils.getBroadcastAddress(), Constants.BROADCAST_PORT);
        socket.send(packet);
        log.newLine("IPAddress broadcast.");
        ServerSocket temp = new ServerSocket(Constants.PORT);
        server = new SocketWrapper(temp.accept(), log);
        temp.close();
        log.newLine("Connected to server at " + server.getInetAdress());
        server.addNoConnectionListener(new NoConnectionListener() {

            public void response(Exception e) {
                log.newLine("Disconnected from server. Shutting down.");
                log.addError(e);
                kill();
            }
        });
        server.addMessageListener(new MessageListener() {

            public void messageRecieved(Object j) {
                if (j instanceof Job)
                    handleJob((Job) j);
                else if (j instanceof RenderManager) {
                    fractal = (RenderManager) j;
                    log.newLine("RenderManager recieved: " + fractal.toString());
                } else if (j instanceof String) {
                    if (j.equals("removing")) {
                        server.sendMessage("removed.");
                        log.blankLine();
                        log.newLine("Removed by server.");
                        System.exit(0);
                    }
                }
            }
        });
        log.newLine("Connected to server " + ipAdress);
        while (fractal == null) {
            try {
                Thread.currentThread().wait(100);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        log.addError(e);
        log.newLine("Server not available. Shutting down.");
        AlertMenu alert = new AlertMenu("Server not Available", "Shutting down. Please try again.");
        System.exit(0);
    }
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) RenderManager(fractal.RenderManager) AlertMenu(menus.AlertMenu)

Aggregations

IOException (java.io.IOException)3 AlertMenu (menus.AlertMenu)3 File (java.io.File)2 JavaFileObject (javax.tools.JavaFileObject)2 RenderManager (fractal.RenderManager)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 ServerSocket (java.net.ServerSocket)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1