Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JumbleNetworkThread.java « net « jumble « morlunk « com « java « main « src - gitlab.com/quite/humla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c891178b808e0420c70a40c8a36fe5708ac5d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2014 Andrew Comminos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.morlunk.jumble.net;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Base class for TCP/UDP protocol implementations.
 * Provides a common threading model (single threaded queue for write)
 * Created by andrew on 25/03/14.
 * @deprecated This shouldn't be needed. Redundant inheritance with limited shared code.
 */
public abstract class JumbleNetworkThread implements Runnable {

    private ExecutorService mExecutor;
    private ExecutorService mSendExecutor;
    private ExecutorService mReceiveExecutor;
    private Handler mMainHandler;
    private boolean mInitialized;

    public JumbleNetworkThread() {
        mMainHandler = new Handler(Looper.getMainLooper());
    }

    protected void startThreads() {
        if (mInitialized) {
            throw new IllegalArgumentException("Threads already initialized.");
        }
        mExecutor = Executors.newSingleThreadExecutor();
        mSendExecutor = Executors.newSingleThreadExecutor();
        mReceiveExecutor = Executors.newSingleThreadExecutor();
        mExecutor.execute(this);
        mInitialized = true;
    }

    protected void stopThreads() {
        if (!mInitialized) {
            throw new IllegalArgumentException("Threads already shutdown.");
        }
        mSendExecutor.shutdown();
        mReceiveExecutor.shutdownNow();
        mExecutor.shutdownNow();
        mSendExecutor = null;
        mReceiveExecutor = null;
        mExecutor = null;
        mInitialized = false;
    }

    protected void executeOnSendThread(Runnable r) {
        mSendExecutor.execute(r);
    }

    protected void executeOnReceiveThread(Runnable r) {
        mSendExecutor.execute(r);
    }

    protected void executeOnMainThread(Runnable r) {
        mMainHandler.post(r);
    }

    protected Handler getMainHandler() {
        return mMainHandler;
    }
}