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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* libjingle
* Copyright 2004--2013, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TALK_EXAMPLES_CHAT_CHATAPP_H_
#define TALK_EXAMPLES_CHAT_CHATAPP_H_
#include "talk/base/thread.h"
#include "talk/base/scoped_ptr.h"
#include "talk/xmpp/jid.h"
#include "talk/xmpp/xmppclient.h"
namespace buzz {
class XmppClient;
class PresenceOutTask;
class PresenceReceiveTask;
class TextChatSendTask;
class TextChatReceiveTask;
class ConsoleTask;
class PresenceStatus;
}
// This is an example chat app for libjingle, showing how to use xmpp tasks,
// data, callbacks, etc. It has a simple text-based UI for logging in,
// sending and receiving messages, and printing the roster.
class ChatApp: public sigslot::has_slots<> {
public:
// Arguments:
// xmpp_client Points to the XmppClient for the communication channel
// (typically created by the XmppPump object).
// main_thread Wraps the application's main thread. Subsidiary threads
// for the various tasks will be forked off of this.
ChatApp(buzz::XmppClient* xmpp_client, talk_base::Thread* main_thread);
// Shuts down and releases all of the contained tasks/threads
~ChatApp();
// Shuts down the current thread and quits
void Quit();
private:
//
// Initialization
//
// Called explicitly after the connection to the chat server is established.
void OnXmppOpen();
//
// UI Stuff
//
// Prints the app main menu on the console.
// Called when ui_state_ == STATE_BASE.
void PrintMenu();
// Prints a numbered list of the logged-in user's roster on the console.
void PrintRoster();
// Prints a prompt for the user to enter either the index from the
// roster list of the user they wish to chat with, or a fully-qualified
// (user@server.ext) jid.
// Called when when ui_state_ == STATE_PROMPTJID.
void PromptJid();
// Prints a prompt on the console for the user to enter a message to send.
// Called when when ui_state_ == STATE_CHATTING.
void PromptChatMessage();
// Sends our presence state to the chat server (and on to your roster list).
// Arguments:
// state Specifies the presence state to show.
enum PresenceState {online, away};
void BroadcastPresence(PresenceState state);
// Returns the RosterItem associated with the specified index.
// Just a helper to select a roster item from a numbered list in the UI.
bool GetRosterItem(int index, buzz::PresenceStatus* status);
//
// Input Handling
//
// Receives input when ui_state_ == STATE_BASE. Handles choices from the
// main menu.
void HandleBaseInput(const std::string& message);
// Receives input when ui_state_ == STATE_PROMPTJID. Handles selection
// of a JID to chat to.
void HandleJidInput(const std::string& message);
// Receives input when ui_state_ == STATE_CHATTING. Handles text messages.
void HandleChatInput(const std::string& message);
//
// signal/slot Callbacks
//
// Connected to the XmppClient::SignalStateChange slot. Receives
// notifications of state changes of the connection.
void OnStateChange(buzz::XmppEngine::State state);
// Connected to the PresenceReceiveTask::PresenceUpdate slot.
// Receives status messages for the logged-in user's roster (i.e.
// an initial list from the server and people coming/going).
void OnPresenceUpdate(const buzz::PresenceStatus& status);
// Connected to the TextChatReceiveTask::SignalTextChatReceived slot.
// Called when we receive a text chat from someone else.
void OnTextMessage(const buzz::Jid& from, const buzz::Jid& to,
const std::string& message);
// Receives text input from the console task. This is where any input
// from the user comes in.
// Arguments:
// message What the user typed.
void OnConsoleMessage(const std::string &message);
// The XmppClient object associated with this chat application instance.
buzz::XmppClient* xmpp_client_;
// We send presence information through this object.
talk_base::scoped_ptr<buzz::PresenceOutTask> presence_out_task_;
// We receive others presence information through this object.
talk_base::scoped_ptr<buzz::PresenceReceiveTask> presence_receive_task_;
// We send text messages though this object.
talk_base::scoped_ptr<buzz::TextChatSendTask> message_send_task_;
// We receive messages through this object.
talk_base::scoped_ptr<buzz::TextChatReceiveTask> message_received_task_;
// UI gets drawn and receives input through this task.
talk_base::scoped_ptr< buzz::ConsoleTask> console_task_;
// The list of JIDs for the people in the logged-in users roster.
// RosterList roster_list_;
typedef std::map<std::string, buzz::PresenceStatus> RosterList;
RosterList roster_list_;
// The JID of the user currently being chatted with.
buzz::Jid chat_dest_jid_;
// UI State constants
enum UIState { STATE_BASE, STATE_PROMPTJID, STATE_CHATTING };
UIState ui_state_;
};
#endif // TALK_EXAMPLES_CHAT_CHATAPP_H_
|