com.skynetcomputing.connection.ConnectionMgr.java Source code

Java tutorial

Introduction

Here is the source code for com.skynetcomputing.connection.ConnectionMgr.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skynetcomputing.connection;

import com.skynetcomputing.api.jobs.TaskMessage;
import com.skynetcomputing.skynetclient.WorkManager;
import com.skynetcomputing.skynetclient.persistence.PersistenceManager;
import com.skynetcomputing.utils.MiscUtils;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author sebas
 */
public class ConnectionMgr implements IConnectionMgr {

    private final ClientConnection conn;
    private final WorkManager workMgr;

    public ConnectionMgr(WorkManager workMgr) throws IOException {
        conn = new ClientConnection(this);
        this.workMgr = workMgr;
    }

    public void start(InetSocketAddress serverAddress) {
        conn.start(serverAddress);
    }

    protected void onConnected() {
        try {
            conn.sendMessage(TaskMessage.WORKER_READY);
        } catch (IOException ex) {
            Logger.getLogger(ConnectionMgr.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isSendingFiles() {
        return conn.isSendingFiles();
    }

    @Override
    public void sendFile(File aFile) {
        try {
            conn.sendFile(aFile);
        } catch (Exception ex) {
            Logger.getLogger(ClientConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param isOK
     * @throws IOException
     */
    @Override
    public void notifyJarOKNOK(boolean isOK) throws IOException {
        TaskMessage msg = isOK ? TaskMessage.WORKER_JAR_OK : TaskMessage.WORKER_JAR_NOK;
        conn.sendMessage(msg);
    }

    /**
     *
     * @throws IOException
     */
    @Override
    public void notifyTaskStarting() throws IOException {
        conn.sendMessage(TaskMessage.TASK_STARTING);
    }

    /**
     *
     * @throws IOException
     */
    @Override
    public void notifyTaskCompleted() throws IOException {
        conn.sendMessage(TaskMessage.TASK_SUCCESS);
        conn.sendMessage(TaskMessage.WORKER_READY);
    }

    /**
     * Save the received file in the job directory.
     *
     * @param aFile File that was send by the server.
     * @param extension Extension of the file.
     * @return True if saved.
     * @throws IOException
     */
    public boolean saveFileLocal(File aFile) throws IOException {
        Logger.getLogger(ConnectionMgr.class.getName()).log(Level.INFO, "File received: {0}", aFile.getName());
        String extension = FilenameUtils.getExtension(aFile.getAbsolutePath());
        switch (extension) {
        case "jar":
            workMgr.onJarReceived(aFile);
            return true;
        case "task":
            workMgr.onTaskReceived(aFile);
            return true;
        case "data":
            workMgr.onDataReceived(aFile);
            return true;
        default:
            return false;
        }
    }
}