~isak-karlsson/+junk/syncany-deamonize-gui

« back to all changes in this revision

Viewing changes to syncany/src/org/syncany/api/client/ClientWorker.java

  • Committer: Isak Karlsson
  • Date: 2011-06-20 14:00:07 UTC
  • Revision ID: isak.karlsson@gmail.com-20110620140007-40t2k1uveyw5ka2i
More work on api client(s)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import java.util.logging.Logger;
15
15
import org.syncany.api.Message;
16
16
import org.syncany.api.Actionable;
 
17
import org.syncany.api.ResponseMessage;
17
18
import org.syncany.api.server.AbstractApiWorker;
18
19
import org.syncany.exceptions.ServiceException;
19
20
 
23
24
 */
24
25
public class ClientWorker extends AbstractApiWorker implements Runnable {
25
26
 
 
27
    public static final int RETRY_COUNT = 5;
 
28
 
26
29
    public ClientWorker(Socket clientSocket, Actionable actions) {
27
30
        super(clientSocket, actions);
28
31
        try {
37
40
    protected void init() {
38
41
    }
39
42
 
 
43
    public Message send(Message message, int timeout) throws ServiceException {
 
44
        synchronized (lock) {
 
45
            out.println(message.toJson());
 
46
            return receive(message.getAction(), timeout);
 
47
        }
 
48
    }
 
49
 
40
50
    @Override
41
51
    public Message send(Message message) throws ServiceException {
42
 
        synchronized (clientSocket) {
43
 
            out.println(message.toJson());
44
 
            try {
45
 
                return receive();
46
 
            } catch (IOException ex) {
47
 
                return null;
 
52
        return send(message, 100);
 
53
    }
 
54
 
 
55
    private Message receive(String action, int timeout) throws ServiceException {
 
56
        synchronized (lock) {
 
57
            ResponseMessage message = null;
 
58
            logger.log(Level.INFO, "Try recive action {0}", action);
 
59
            for (int n = 0; n < RETRY_COUNT; n++) {
 
60
                try {
 
61
                    message = receive(ResponseMessage.class);
 
62
                } catch (Exception e) {
 
63
                    logger.severe(e.getMessage());
 
64
                }
 
65
 
 
66
                if (message != null && message.responseTo() != null && message.responseTo().equals(action)) {
 
67
                    logger.log(Level.INFO, "Found {0}.", action);
 
68
                    return message;
 
69
                } else if (message != null) {
 
70
                    doHandle(message);
 
71
                }
 
72
 
 
73
                try {
 
74
                    logger.log(Level.INFO, "Retrying for {0} of 5 times", n + 1);
 
75
                    Thread.sleep(timeout);
 
76
                } catch (InterruptedException ex) {
 
77
                }
48
78
            }
 
79
            logger.info("Response not recieved! Returning null");
 
80
            return null;
49
81
        }
50
82
    }
51
83
 
52
84
    public void setRunning(boolean running) {
53
 
        this.running = running;
 
85
        synchronized (lock) {
 
86
            this.running = running;
 
87
        }
 
88
    }
 
89
 
 
90
    /**
 
91
     * Performe a standard handling of a Message
 
92
     * @param message 
 
93
     */
 
94
    protected synchronized void doHandle(Message message) {
 
95
        synchronized (lock) {
 
96
            if (message != null) {
 
97
                if (!handle(message, this, false)) {
 
98
                    handle(message, getParent(), false);
 
99
                }
 
100
            }
 
101
        }
54
102
    }
55
103
 
56
104
    @Override
57
105
    public void run() {
58
106
        while (isRunning()) {
59
107
            try {
60
 
                synchronized (clientSocket) {
61
 
                    logger.info("Waiting for input..." + clientSocket.isConnected());
 
108
                synchronized (lock) {
 
109
                    logger.info("Waiting for input...");
62
110
                    try {
63
 
                        logger.info("Trying to recivie");
64
111
                        Message message = receive();
65
 
                        logger.info("recived " + message);
66
 
 
67
 
                        if (message != null) {
68
 
                            if (!handle(message, this, false)) {
69
 
                                handle(message, server, false);
70
 
                            }
71
 
                        }
 
112
                        logger.log(Level.INFO, "Recived {0}", message);
 
113
                        doHandle(message);
72
114
                    } catch (SocketTimeoutException e) {
73
 
                        logger.info("Timed out..."); /// swallow
 
115
                        logger.info("Timed out...");
74
116
                    }
75
117
                }
76
 
                if (clientSocket.isInputShutdown() || clientSocket.isOutputShutdown()) {
77
 
                    logger.info("Things are shutdown...");
78
 
                    running = false;
79
 
                }
80
118
            } catch (ServiceException ex) {
81
119
                logger.log(Level.SEVERE, null, ex);
82
120
            } catch (IOException ex) {