Package br.com.visualmidia.update

Source Code of br.com.visualmidia.update.UpdateStack

package br.com.visualmidia.update;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import br.com.visualmidia.business.FileDescriptor;
import br.com.visualmidia.business.FileDescriptorMD5;
import br.com.visualmidia.business.Update;
import br.com.visualmidia.core.Constants;
import br.com.visualmidia.core.server.Communicate;

public class UpdateStack {

    private Socket socket;

    public UpdateStack(Socket socket) {
        this.socket = socket;
        initCommunication();
    }

  private void initCommunication() {
        Communicate communicate = new Communicate(socket);
       
        String buildId = communicate.sendAndReceive("300, Qual sua vers�o?");
       
        List<Update> updateToBeMadeInTheCliente = getUpdateList(buildId);
        if(updateToBeMadeInTheCliente.size() > 0) {
            String response = communicate.sendAndReceive("302, Voc� precisa de atualiza��o");
            if(response.equals("305")) {
                for (Update update : updateToBeMadeInTheCliente) {
                    List<File> files = update.getFiles();
                    for (int i = 0; i < files.size(); i++) {
                        File file = files.get(i);
                        if(socket.isClosed()) {
                            return;
                        }
                        String fileRemotePath = getPath(update.getBuildId(), file.getPath(), file.getName());
                               
                        communicate.send("303, Vou te enviar um arquivo");
                        communicate.sendObject(new FileDescriptor(fileRemotePath, file.getName(), file.length(), Long.parseLong(update.getBuildId())));
                       
                        if(communicate.receive().equals("309")) {
                          communicate.sendFile(file);
                            if(communicate.receive().equals("308")){
                                i--;
                            }
                        }
                    }
                }
                communicate.send("304");
            } else if(response.equals("306")) {
                communicate.sendObject(updateToBeMadeInTheCliente);
            }
        } else {
            communicate.send("301, Voc� est� atualizado!");
        }
       
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
    }

    private List<Update> getUpdateList(String clientBuildId) {
      List<Update> updateToBeMadeInTheCliente = new ArrayList<Update>();
        File directory = new File(Constants.FILES_PATH);
        if(directory.exists() && directory.isDirectory()) {
          String[] directoryList = directory.list();
          for (String buildId : directoryList) {
        if(Long.parseLong(buildId) > Long.parseLong(clientBuildId)) {
          Update update = new Update(buildId, "", "");
          update.updateFiles();
          updateToBeMadeInTheCliente.add(update);
        }
      }
        }
       
        return updateToBeMadeInTheCliente;
    }

    private String getPath(String buildId, String path, String fileName) {
        String replace = path.replace(Constants.FILES_PATH + buildId + Constants.FILE_SEPARATOR, "");
        return replace.replace(fileName, "");
    }

    private static boolean isGdOpen() {
      File file = new File(Constants.CURRENT_DIR + ".gd");
    if(file.exists()) {
      return true;
    }
        return false;
    }
}
TOP

Related Classes of br.com.visualmidia.update.UpdateStack

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.