summaryrefslogtreecommitdiff
path: root/cs/demo/Glacier2/chat/ChatWindow.xaml.cs
blob: a41ae55e7df3e0fd5d4a97e0ba6a7be8dbf10f3e (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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// **********************************************************************
//
// Copyright(c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using Demo;

namespace Glacier2.chat.client
{
    public class LoginData
    {
        public LoginData()
        {
            routerHost = "127.0.0.1";
            userName = "test";
            password = "";
        }

        public String routerHost;
        public String userName;
        public String password;
    }

    class Util
    {
        static public void
        locateOnScreen(System.Windows.Window window)
        {
            window.Left = (System.Windows.SystemParameters.PrimaryScreenWidth - window.Width) / 2;
            window.Top = (System.Windows.SystemParameters.PrimaryScreenHeight - window.Height) / 2;
        }

        static public void
        centerWindow(System.Windows.Window w1, System.Windows.Window w)
        {
            w1.Top = w.Top + ((w.Height - w1.Height) / 2);
            w1.Left = w.Left + ((w.Width - w1.Width) / 2);
        }
    }

    /// <summary>
    /// Interaction logic for ChatWindow.xaml
    /// </summary>
    public partial class ChatWindow : Window, Glacier2.SessionCallback
    {
        private class ChatCallbackI : Demo.ChatCallbackDisp_
        {
            public ChatCallbackI(ChatWindow window)
            {
                _window = window;
            }

            public override void
            message(string data, Ice.Current current)
            {
                _window.appendMessage(data + Environment.NewLine);
            }

            private ChatWindow _window;
        }

        public ChatWindow()
        {
            Ice.InitializationData initData = new Ice.InitializationData();

            initData.properties = Ice.Util.createProperties();
            initData.properties.load("config.client");

            // Dispatch servant calls and AMI callbacks with this windows Dispatcher.
            initData.dispatcher = delegate(System.Action action, Ice.Connection connection)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
            };

            _factory = new SessionFactoryHelper(initData, this);
            InitializeComponent();
            Util.locateOnScreen(this);
        }

        private void
        login(object sender, ExecutedRoutedEventArgs args)
        {
            doLogin();
        }
        
        public void doLogin()
        {
            LoginDialog loginDialog = new LoginDialog(_loginData);
            Util.centerWindow(loginDialog, this);
            if(loginDialog.ShowModal())
            {
                status.Content = "Connecting";
                _factory.setRouterHost(_loginData.routerHost);
                _factory.setRouterIdentity(new Ice.Identity("router", "DemoGlacier2"));
                _session = _factory.connect(_loginData.userName, _loginData.password);

                _cancelDialog = new CancelDialog();
                Util.centerWindow(_cancelDialog, this);
                if(_cancelDialog.ShowModal())
                {
                    destroySession();
                }
            }
        }

        private void
        logout(object sender, ExecutedRoutedEventArgs args)
        {
            txtMessages.Text = "";
            status.Content = "Logging out";
            destroySession();
        }

        private void
        exit(object sender, ExecutedRoutedEventArgs args)
        {
            Close();
        }

        private void
        windowClosed(object sender, EventArgs e)
        {
            lock(this)
            {
                destroySession();
            }
            App.Current.Shutdown(0);
        }

        private void
        isLogoutEnabled(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = _session != null && _session.isConnected();
        }

        private void
        isLoginEnabled(object sender,CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = _session == null || !_session.isConnected();
        }

        //
        // Event handler attached to txtChatImputLine onKeyDown.
        // If the key is the Enter key, it sends the message asynchronously
        // and cleans the input line; otherwise, it does nothing.
        //
        private void
        sendMessage(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                string message = input.Text.Trim();
                if(message.Length > 0)
                {
                    _chat.begin_say(message).whenCompleted(delegate(Ice.Exception ex)
                                             {
                                                 appendMessage("<system-message> - " + ex.ToString() + 
                                                               Environment.NewLine);
                                             });
                }
                input.Text = "";
            }
        }

        private void
        scrollDown(object sender, SizeChangedEventArgs e)
        {
            txtMessages.ScrollToEnd();
        }

        private void
        closeCancelDialog()
        {
            if(_cancelDialog != null)
            {
                _cancelDialog.Close();
                _cancelDialog = null;
            }
        }

        public void
        appendMessage(string message)
        {
            txtMessages.AppendText(message);
            txtMessages.ScrollToEnd();
        }

        private void
        destroySession()
        {
            if(_session != null)
            {
                _session.destroy();
                _session = null;
            }
        }

        private LoginData _loginData = new LoginData();
        private CancelDialog _cancelDialog = new CancelDialog();
        private Glacier2.SessionFactoryHelper _factory;
        private Glacier2.SessionHelper _session;
        private Demo.ChatSessionPrx _chat;

        #region Callback Members

        public void connectFailed(SessionHelper session, Exception ex)
        {
            // If the session has been reassigned avoid the
            // spurious callback.
            if(session != _session)
            {
                return;
            }

            closeCancelDialog();
            status.Content = ex.GetType();
        }

        public void connected(SessionHelper session)
        {
            // If the session has been reassigned avoid the
            // spurious callback.
            if(session != _session)
            {
                return;
            }

            Ice.Object servant = new ChatCallbackI(this);

            Demo.ChatCallbackPrx callback = Demo.ChatCallbackPrxHelper.uncheckedCast(_session.addWithUUID(servant));
            _chat = Demo.ChatSessionPrxHelper.uncheckedCast(_session.session());
            _chat.begin_setCallback(callback).whenCompleted(delegate()
                                              {
                                                  closeCancelDialog();
                                                  input.IsEnabled = true;
                                                  status.Content = "Connected with " + _loginData.routerHost;
                                              },
                                              delegate(Ice.Exception ex)
                                              {
                                                  if(_session != null)
                                                  {
                                                      _session.destroy();
                                                  }
                                              });
        }

        public void createdCommunicator(SessionHelper session)
        {
        }

        public void disconnected(SessionHelper session)
        {
            // If the session has been reassigned avoid the
            // spurious callback.
            if(session != _session)
            {
                return;
            }
            closeCancelDialog();
            _session = null;
            _chat = null;
            input.IsEnabled = false;
            status.Content = "Not connected";
        }

        #endregion
    }
}