summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/discovery/SameService.java
blob: 7ca9778c33406b24a5a10ce1a74741af375374c3 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.orbekk.discovery;

import java.net.DatagramPacket;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orbekk.same.ClientApp;
import com.orbekk.same.MasterApp;

public class SameService extends Service {
    final static int PORT = 15066;
    private Logger logger = LoggerFactory.getLogger(getClass());
    private Thread discoveryThread = null;
    
    public final class DiscoveryThread extends Thread {
        Broadcast broadcast;
        
        public DiscoveryThread() {
            broadcast = new Broadcast(SameService.this);
        }
        
        @Override public void run() {
            while (!Thread.interrupted()) {
                byte[] data = new byte[1024];
                DatagramPacket packet = broadcast.receiveBroadcast(PORT);
                String content = new String(packet.getData(), 0, packet.getLength());
                Message message = Message.obtain();
                message.obj = content;
                toastHandler.sendMessage(message);
            }
        }
        
        @Override public void interrupt() {
            super.interrupt();
            broadcast.interrupt();
        }
    }
    
    private Handler toastHandler = new Handler() {
        @Override public void handleMessage(Message message) {
            Toast.makeText(SameService.this,
                    (String)message.obj, Toast.LENGTH_SHORT)
                            .show();
            logger.info("Display toast: {}", (String)message.obj);
        }
    };
    
    private void createNetwork() {
        if (discoveryThread == null) {
            synchronized (this) {
                discoveryThread = new DiscoveryThread();
                discoveryThread.start();
            }
        }
        new Thread() {
            @Override public void run() {
                new MasterApp().run(PORT+1);
            }
        }.start();
    }
    
    private void sendBroadcastDiscovery() {
        byte[] data = "Discover".getBytes();
        new Broadcast(this).sendBroadcast(data, PORT);        
    }
    
    private void joinNetwork() {
        sendBroadcastDiscovery();
        new ClientApp().run(PORT+2, "ClientNetwork", null);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service start: " + intent.getAction(),
                Toast.LENGTH_SHORT).show();
        if (intent.getAction().equals("create")) {
            createNetwork();
        } else if (intent.getAction().equals("join")) {
            joinNetwork();
        }
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
        discoveryThread.interrupt();
    }

}