summaryrefslogtreecommitdiffstats
blob: 5a0059deed60bb9bb954afa4925708b040e16d8e (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
104
105
106
107
108
/*
* Copyright (c) 2014, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2014, Remko Tronçon.
* All rights reserved.
*/

package com.isode.stroke.pubsub;
import com.isode.stroke.client.ClientError;
import com.isode.stroke.client.ClientOptions;
import com.isode.stroke.jid.JID;
import com.isode.stroke.network.JavaNetworkFactories;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.signals.Slot;
import com.isode.stroke.signals.Slot1;
import com.isode.stroke.base.SafeByteArray;

public class Client {
    
    static boolean debugInfo = false;
    static boolean debugInfoXml = false;
    
    public Client(String name, JID jid, SafeByteArray password, JavaNetworkFactories networkFactories, final Slot connectCallback) {
        name_ = name;
        connecting_ = true;
        connected_ = false;
        disconnecting_ = false;
        
        client_ = new com.isode.stroke.client.Client(jid, password, networkFactories);
        
        client_.onConnected.connect(new Slot() {
            public void call() {
                if (debugInfo) {
                    System.out.println("[" + name_ + "] onConnected.");
                }
                connecting_ = false;
                connected_ = true;
                connectCallback.call();
            }
        });
        
        client_.onDisconnected.connect(new Slot1<ClientError>() {
            public void call(ClientError error) {
                if (debugInfo) {
                    System.out.println("[" + name_ + "] onDisconnected.");
                }
                connected_ = false;
            }
        });
        
        client_.onDataRead.connect(new Slot1<SafeByteArray>() {
            public void call(SafeByteArray xml) {
                if (!connecting_ && !disconnecting_) {
                    if (debugInfoXml) {
                        System.out.println("[" + name_ + "] Client.Read:");
                        System.out.println(xml + "\n");
                    }
                }
            }
        });
        
        client_.onDataWritten.connect(new Slot1<SafeByteArray>() {
            public void call(SafeByteArray xml) {
                if (!connecting_ && !disconnecting_) {
                    if (debugInfoXml) {
                        System.out.println("[" + name_ + "] Client.Write:");
                        System.out.println(xml + "\n");
                    }
                }
            }
        });
        
        client_.connect(new ClientOptions());
    }
    
    void disconnect() {
        disconnecting_ = true;
        client_.disconnect();
    }
    
    boolean isConnected() {
        return connected_;
    }
    
    boolean isConnecting() {
        return connecting_;
    }
    
    JID getJID() {
        return client_.getJID();
    }
    
    IQRouter getIQRouter() {
        return client_.getIQRouter();
    }
    
    PubSubManager getPubSubManager() {
        return client_.getPubSubManager();
    }
    
    com.isode.stroke.client.Client client_;
    String name_;
    boolean connected_;
    boolean connecting_;
    boolean disconnecting_;
}