001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.operations.daemon.messages; 019 020import java.util.logging.Level; 021 022import org.syncany.operations.daemon.EventUserInteractionListener; 023import org.syncany.operations.daemon.messages.api.ManagementRequest; 024import org.syncany.operations.daemon.messages.api.ManagementRequestHandler; 025import org.syncany.operations.daemon.messages.api.Response; 026import org.syncany.operations.init.ConnectOperation; 027import org.syncany.operations.init.ConnectOperationResult; 028 029public class ConnectManagementRequestHandler extends ManagementRequestHandler { 030 public ConnectManagementRequestHandler() { 031 // Nothing 032 } 033 034 @Override 035 public Response handleRequest(final ManagementRequest request) { 036 final ConnectManagementRequest concreteRequest = (ConnectManagementRequest) request; 037 logger.log(Level.SEVERE, "Executing ConnectOperation for folder " + concreteRequest.getOptions().getLocalDir() + " ..."); 038 039 Thread connectThread = new Thread(new Runnable() { 040 @Override 041 public void run() { 042 try { 043 ConnectOperation initOperation = new ConnectOperation(concreteRequest.getOptions(), new EventUserInteractionListener()); 044 ConnectOperationResult operationResult = initOperation.execute(); 045 046 switch (operationResult.getResultCode()) { 047 case OK: 048 eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.OK, operationResult, request.getId())); 049 break; 050 051 case NOK_TEST_FAILED: 052 eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_FAILED_TEST, operationResult, request.getId())); 053 break; 054 055 default: 056 eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_FAILED_UNKNOWN, operationResult, request.getId())); 057 break; 058 } 059 } 060 catch (Exception e) { 061 logger.log(Level.WARNING, "Error adding watch to daemon config.", e); 062 eventBus.post(new ConnectManagementResponse(ConnectManagementResponse.NOK_OPERATION_FAILED, new ConnectOperationResult(), request 063 .getId())); 064 } 065 } 066 }, "ConRq/" + concreteRequest.getOptions().getLocalDir().getName()); 067 068 connectThread.start(); 069 070 return null; 071 } 072}