Esempio n. 1
0
		public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
		{
			lock (this)
			{
				//Check tennats here
				if (ValidateHost(stream.To))
				{
					//Create new services
					foreach (var template in templates)
					{
						var service = (IXmppService)Activator.CreateInstance(template.Value);
						service.Jid = new Jid(Stringprep.NamePrep(string.Format("{0}.{1}", template.Key, stream.To.Server).Trim('.')));

						if (serviceManager.GetService(service.Jid) != null)
						{
							continue;
						}

						service.Name = service.Jid.ToString();
						if (!string.IsNullOrEmpty(template.Key))
						{
							service.ParentService = serviceManager.GetService(new Jid(Stringprep.NamePrep(stream.To.Server)));
						}
						service.Configure(new Dictionary<string, string>());
						serviceManager.RegisterService(service);
					}
					//Reroute
					handlerManager.ProcessStreamStart(stream, Uri.CLIENT, xmppStream);
				}
				else
				{
					context.Sender.SendToAndClose(xmppStream, XmppStreamError.HostUnknown);
				}
			}
		}
Esempio n. 2
0
        public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
        {
            lock (this)
            {
                //Check tennats here
                if (ValidateHost(stream.To))
                {
                    //Create new services
                    foreach (var template in templates)
                    {
                        var service = (IXmppService)Activator.CreateInstance(template.Value);
                        service.Jid = new Jid(Stringprep.NamePrep(string.Format("{0}.{1}", template.Key, stream.To.Server).Trim('.')));

                        if (serviceManager.GetService(service.Jid) != null)
                        {
                            continue;
                        }

                        service.Name = service.Jid.ToString();
                        if (!string.IsNullOrEmpty(template.Key))
                        {
                            service.ParentService = serviceManager.GetService(new Jid(Stringprep.NamePrep(stream.To.Server)));
                        }
                        service.Configure(new Dictionary <string, string>());
                        serviceManager.RegisterService(service);
                    }
                    //Reroute
                    handlerManager.ProcessStreamStart(stream, Uri.CLIENT, xmppStream);
                }
                else
                {
                    context.Sender.SendToAndClose(xmppStream, XmppStreamError.HostUnknown);
                }
            }
        }
Esempio n. 3
0
        public byte AddXmppConnection(string connectionId, string userName, byte state, int tenantId)
        {
            try
            {
                _log.DebugFormat("Add Xmpp Connection: connectionId={0}, userName={1}, state={2}, tenantId={3}", connectionId, userName, state, tenantId);
                var jid      = GetJid(userName, tenantId, SignalRHelper.SIGNALR_RESOURCE);
                var listener = (SignalRXmppListener)((XmppGateway)_xmppServer.GetService(typeof(IXmppReceiver))).GetXmppListener("SignalR Listener");
                if (listener.GetXmppConnection(connectionId) != null)
                {
                    RemoveXmppConnection(connectionId, userName, tenantId);
                }
                listener.AddXmppConnection(connectionId, _xmppServer);
                var xmppStream = ((XmppStreamManager)_xmppServer.GetService(typeof(XmppStreamManager))).GetOrCreateNewStream(connectionId);
                xmppStream.Authenticate(userName);
                string domain = CoreContext.TenantManager.GetTenant(tenantId).TenantDomain;
                if (JabberConfiguration.ReplaceDomain && domain.EndsWith(JabberConfiguration.ReplaceFromDomain))
                {
                    int place = domain.LastIndexOf(JabberConfiguration.ReplaceFromDomain);
                    if (place >= 0)
                    {
                        domain = domain.Remove(place, JabberConfiguration.ReplaceFromDomain.Length).Insert(place, JabberConfiguration.ReplaceToDomain);
                    }
                }
                xmppStream.Domain    = domain;
                xmppStream.Connected = true;
                xmppStream.BindResource(SignalRHelper.SIGNALR_RESOURCE);

                var handler = _xmppServer.HandlerManager.HandlerStorage.GetStreamStartHandlers(jid)[0];
                var stream  = new Stream
                {
                    To        = new Jid(jid.Server),
                    Namespace = "http://etherx.jabber.org/streams",
                    Version   = "1.6",
                    Language  = string.Empty
                };

                handler.StreamStartHandle(xmppStream, stream, null);

                var session = new XmppSession(jid, xmppStream)
                {
                    RosterRequested = false,
                    Active          = true,
                    IsSignalRFake   = true
                };

                ((XmppSessionManager)_xmppServer.GetService(typeof(XmppSessionManager))).AddSession(session);

                var presence = new Presence(SignalRHelper.GetShowType(state), String.Empty, SignalRHelper.PRIORITY)
                {
                    From = jid,
                    Type = SignalRHelper.GetPresenceType(state)
                };

                _xmppServer.SessionManager.SetSessionPresence(session, presence);

                var sender   = (IXmppSender)_xmppServer.GetService(typeof(IXmppSender));
                var sessions = _xmppServer.SessionManager.GetSessions().Where(s => s.Id != session.Id).ToArray();
                sender.Broadcast(sessions, session.Presence);

                var offlineMessages = _xmppServer.StorageManager.OfflineStorage.GetOfflineMessages(jid);
                if (offlineMessages.Count > 0)
                {
                    var users = new List <string>();
                    for (int i = 0; i < offlineMessages.Count; i++)
                    {
                        var from = offlineMessages[i].From;
                        var name = from.User != null?from.User.ToLowerInvariant() : string.Empty;

                        if (!users.Contains(name))
                        {
                            users.Add(name);
                        }
                    }
                    signalrServiceClient.SendOfflineMessages(userName, users, tenantId);
                    //_xmppServer.StorageManager.OfflineStorage.RemoveAllOfflineMessages(jid);
                }
            }
            catch (Exception e)
            {
                _log.Error(e);
            }
            return(GetState(tenantId, userName));
        }