Пример #1
0
        public void Process()
        {
            if (client == null)
            {
                return;
            }

            client.Process();

            if (client.IsConnected && DateTime.Now.Subtract(lastRefreshTime) > new TimeSpan(0, 0, 10))
            {
                int addCount = 0;

                foreach (DaemonLogic.CloudObject cloudObject in Objects)
                {
                    if (!InjectedObjects.Contains(cloudObject.CloudObjectId))
                    {
                        InjectOrUpdateObject(cloudObject);

                        addCount++;
                        if (addCount > 10)
                        {
                            break;
                        }
                    }
                }

                List <DaemonLogic.CloudObject> modifiedOjects = (from o in entityContext.CloudObject where o.Bubble.BubbleId == bubble.BubbleId && o.Modified > lastRefreshTime select o).ToList <DaemonLogic.CloudObject>();

                foreach (DaemonLogic.CloudObject cloudObject in modifiedOjects)
                {
                    entityContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, cloudObject);

                    if (cloudObject.Enabled)
                    {
                        DaemonLogic.ObjectType  objectType  = QueryUtil.First <DaemonLogic.ObjectType>(from o in entityContext.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.ObjectType);
                        DaemonLogic.Participant participant = QueryUtil.First <DaemonLogic.Participant>(from o in entityContext.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.Participant);

                        InjectOrUpdateObject(cloudObject);
                    }
                    else
                    {
                        if (Objects.Contains(cloudObject))
                        {
                            EjectObject(cloudObject);
                        }
                    }
                }

                lastRefreshTime = DateTime.Now;
            }
        }
Пример #2
0
        public byte OnCloudParticipantIdentified(CloudBubble bubble, IdentifyRequestMessage message)
        {
            Guid   participantId           = message.ParticipantId;
            String participantIdentifier   = message.ParticipantIdentity;
            String participantIdentityType = message.ParticipantIdentityType;

            if (!participantIdentityType.Equals(IdentifyRequestMessage.OPEN_ID_IDENTITY))
            {
                return(MxpResponseCodes.UNSUPPORTED_OPERATION);
            }

            Participant participant = QueryUtil.First <DaemonLogic.Participant>(
                from p in entityContext.Participant where p.ParticipantId == participantId select p);

            if (participant != null)
            {
                if (participant.OpenIdUrl != participantIdentifier)
                {
                    return(MxpResponseCodes.RESERVED_ID);
                }
                else
                {
                    return(MxpResponseCodes.SUCCESS);
                }
            }

            participant = QueryUtil.First <DaemonLogic.Participant>(
                from p in entityContext.Participant where p.OpenIdUrl == participantIdentifier select p);
            if (participant != null)
            {
                return(MxpResponseCodes.UNAUTHORIZED_OPERATION);
            }

            participant = new Participant
            {
                ParticipantId = participantId,
                OpenIdUrl     = participantIdentifier,
            };

            entityContext.AddToParticipant(participant);
            entityContext.SaveChanges();

            return(MxpResponseCodes.SUCCESS);
        }
Пример #3
0
        /// <summary>
        /// 设置已读
        /// </summary>
        /// <param name="id">站内信ID</param>
        /// <returns></returns>
        public async Task ReadAsync(ObjectId id)
        {
            var msg = await QueryUtil.First <Message>(id).ConfigureAwait(false);

            if (msg == null)
            {
                throw new CmsException(EnumStatusCode.ArgumentOutOfRange, "不存在此站内信");
            }

            if (msg.ReadTime != null)
            {
                //已读的不再更新读取时间
                return;
            }

            msg.ReadTime = DateTime.Now;
            await Repository.Update(msg, "ReadTime").ConfigureAwait(false);

            await SendUnreadCountAsync(msg.ToUser).ConfigureAwait(false);
        }
Пример #4
0
        public DaemonParticipant(
            string serverAddress,
            int serverPort,
            Guid bubbleId,
            string daemonIdentifier,
            string daemonSecret
            )
        {
            this.serverAddress    = serverAddress;
            this.serverPort       = serverPort;
            this.bubbleId         = bubbleId;
            this.DaemonIdentifier = daemonIdentifier;
            this.DaemonSecret     = daemonSecret;
            String programName         = "Cloud Daemon Participant";
            byte   programMajorVersion = 0;
            byte   programMinorVersion = 1;

            bubble = QueryUtil.First <Bubble>((from b in entityContext.Bubble where b.BubbleId == bubbleId select b));

            Objects = (from o in entityContext.CloudObject where o.Bubble.BubbleId == bubble.BubbleId && o.Enabled == true select o).ToList <DaemonLogic.CloudObject>();

            lastRefreshTime = DateTime.Now;

            if (Objects.Count == 0)
            {
                return;
            }

            client = new CloudView(100, programName, programMajorVersion, programMinorVersion);

            client.ServerInteractRequest += OnInteractRequest;

            foreach (DaemonLogic.CloudObject cloudObject in Objects)
            {
                DaemonLogic.ObjectType  objectType  = QueryUtil.First <DaemonLogic.ObjectType>(from o in entityContext.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.ObjectType);
                DaemonLogic.Participant participant = QueryUtil.First <DaemonLogic.Participant>(from o in entityContext.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.Participant);
                IdObjectDictionary.Add(cloudObject.CloudObjectId, cloudObject);
            }
        }
Пример #5
0
        private void timer_Tick(object state)
        {
            try
            {
                DaemonEntities      entityContext        = new DaemonEntities();
                List <LocalProcess> localProcessEntities = (
                    from lp in entityContext.LocalProcess select lp).ToList <LocalProcess>();

                foreach (LocalProcess localProcessEntity in localProcessEntities)
                {
                    LocalProcessState localProcessState = QueryUtil.First <LocalProcessState>(
                        from lps in entityContext.LocalProcessState where lps.LocalProcess.LocalProcessId == localProcessEntity.LocalProcessId select lps);

                    if (localProcessEntity.Enabled)
                    {
                        if (localProcessState == null)
                        {
                            Process process = Process.Start(Directory.GetParent(this.GetType().Assembly.Location).FullName + "\\DaemonProcess.exe", localProcessEntity.LocalProcessId.ToString() + " --db-log");
                        }
                    }
                    if (localProcessState != null)
                    {
                        if (DateTime.Now - localProcessState.Modified > new TimeSpan(0, 0, 60))
                        {
                            entityContext.DeleteObject(localProcessState);
                            entityContext.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Error in process guard loop: " + e.ToString() + " : " + e.StackTrace);
                Thread.Sleep(1000);
            }
        }
Пример #6
0
        public void Startup()
        {
            lock (this)
            {
                Thread.CurrentThread.Name   = LocalProcessId.ToString() + "-main";
                MxpOptions.ThreadNamePrefix = LocalProcessId.ToString();

                LogUtil.Info("Daemon Process startup...");

                if (service != null)
                {
                    throw new Exception("DaemonProcess already started.");
                }

                // Preparing database entity context.
                entityContext = new DaemonEntities();

                // Loading configuration from database.
                localProcessEntity = QueryUtil.First <LocalProcess>(
                    from lp in entityContext.LocalProcess where lp.LocalProcessId == LocalProcessId select lp);
                participantEntity = QueryUtil.First <DaemonLogic.Participant>(
                    from lp in entityContext.LocalProcess where lp.LocalProcessId == LocalProcessId select lp.Participant);
                remoteProcessEntities = (from rp in entityContext.RemoteProcess where rp.LocalProcess.LocalProcessId == LocalProcessId select rp).ToList <RemoteProcess>();
                bubbleEntities        = (from b in entityContext.Bubble where b.LocalProcess.LocalProcessId == LocalProcessId select b).ToList <Bubble>();

                LogUtil.Info("Loaded local process configuration: " + localProcessEntity.Address + ":" + localProcessEntity.ServerPort + "/" + localProcessEntity.HubPort);

                // Creating service, bubbles and bubble links.
                service = new CloudService(ConfigurationManager.AppSettings["DaemonMemberWeb"],
                                           localProcessEntity.Address,
                                           localProcessEntity.HubPort,
                                           localProcessEntity.ServerPort,
                                           localProcessEntity.Name,
                                           DaemonProcessConstants.ProgramMajorVersion,
                                           DaemonProcessConstants.ProgramMinorVersion);

                foreach (Bubble bubble in bubbleEntities)
                {
                    CloudBubble cloudBubble = new CloudBubble(bubble.BubbleId, bubble.Name, (float)bubble.Range, (float)bubble.PerceptionRange);
                    cloudBubble.ParticipantConnectAuthorize  += OnParticipantConnectAuthorize;
                    cloudBubble.CloudParticipantDisconnected += OnCloudParticipantDisconnected;
                    service.AddBubble(cloudBubble);

                    foreach (RemoteProcess remoteProcess in remoteProcessEntities)
                    {
                        cloudBubble.AddAllowedRemoteHubAddress(remoteProcess.Address);
                    }

                    // Loading bubble link configuration from database.
                    List <DaemonLogic.BubbleLink> bubbleLinkConfigurations = (from b in entityContext.BubbleLink where b.Bubble.BubbleId == bubble.BubbleId select b).ToList <DaemonLogic.BubbleLink>();

                    foreach (DaemonLogic.BubbleLink bubbleLink in bubbleLinkConfigurations)
                    {
                        service.AddBubbleLink(bubble.BubbleId, bubbleLink.RemoteBubbleId, bubbleLink.Address, bubbleLink.Port, (float)-bubbleLink.X, (float)-bubbleLink.Y, (float)-bubbleLink.Z,
                                              true, true);
                    }
                }

                // Saving process state info to database.
                localProcessStateEntity = new LocalProcessState
                {
                    LocalProcessStateId = Guid.NewGuid(),
                    LocalProcess        = localProcessEntity,
                    Participant         = participantEntity,
                    Created             = DateTime.Now,
                    Modified            = DateTime.Now,
                    Cpu = OnGetProcessingTime(),
                    Mem = (System.Diagnostics.Process.GetCurrentProcess().WorkingSet64) / 1024
                };
                entityContext.AddToLocalProcessState(localProcessStateEntity);
                entityContext.SaveChanges();

                service.Startup(false);

                LogUtil.Info("Daemon Process startup done.");
            }
        }
Пример #7
0
        public void OnInteractRequest(InteractRequestMessage interactRequest)
        {
            if (interactRequest.InteractionFragment.InteractionName == "TypeList")
            {
                InteractResponseMessage interactionResponse = (InteractResponseMessage)MessageFactory.Current.ReserveMessage(typeof(InteractResponseMessage));
                interactionResponse.RequestMessageId = interactRequest.MessageId;
                interactionResponse.FailureCode      = MxpResponseCodes.SUCCESS;

                interactionResponse.InteractionFragment.InteractionName     = "TypeList";
                interactionResponse.InteractionFragment.TargetParticipantId = interactRequest.InteractionFragment.SourceParticipantId;
                interactionResponse.InteractionFragment.SourceParticipantId = interactRequest.InteractionFragment.TargetParticipantId;

                OmTypeListResponseExt responseExt = new OmTypeListResponseExt();

                List <DaemonLogic.ObjectType> objectTypes = (from t in entityContext.ObjectType orderby t.Name select t).ToList <DaemonLogic.ObjectType>();
                foreach (DaemonLogic.ObjectType objectType in objectTypes)
                {
                    OmObjectType omObjectType = new OmObjectType();
                    omObjectType.TypeId   = objectType.ObjectTypeId.ToString();
                    omObjectType.TypeName = objectType.Name;
                    responseExt.ObjectType.Add(omObjectType);
                }

                interactionResponse.SetExtension <OmTypeListResponseExt>(responseExt);

                client.SendInteractResponse(interactionResponse);
            }

            if (interactRequest.InteractionFragment.InteractionName == "ObjectInsert")
            {
                OmInsertRequestExt requestExt = interactRequest.GetExtension <OmInsertRequestExt>();

                Guid       typeId     = new Guid(requestExt.TypeId);
                ObjectType objectType = QueryUtil.First <DaemonLogic.ObjectType>(from t in entityContext.ObjectType where t.ObjectTypeId == typeId select t);
                entityContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, objectType);
                Participant participant = QueryUtil.First <DaemonLogic.Participant>(from p in entityContext.Participant where p.ParticipantId == interactRequest.InteractionFragment.SourceParticipantId select p);

                DaemonLogic.CloudObject cloudObject = new DaemonLogic.CloudObject
                {
                    CloudObjectId = Guid.NewGuid(),
                    Participant   = participant,
                    ObjectType    = objectType,
                    Bubble        = bubble,
                    Name          = "New " + objectType.Name,
                    Radius        = objectType.Radius,
                    Mass          = objectType.Mass,
                    ModelUrl      = objectType.ModelUrl,
                    ModelScale    = objectType.ModelScale,
                    X             = requestExt.Location.X,
                    Y             = requestExt.Location.Y,
                    Z             = requestExt.Location.Z,
                    OX            = requestExt.Orientation.X,
                    OY            = requestExt.Orientation.Y,
                    OZ            = requestExt.Orientation.Z,
                    OW            = requestExt.Orientation.W,
                    Created       = DateTime.Now,
                    Modified      = DateTime.Now,
                    Enabled       = true
                };
                entityContext.AddToCloudObject(cloudObject);

                entityContext.SaveChanges();

                InjectOrUpdateObject(cloudObject);

                InteractResponseMessage interactionResponse = (InteractResponseMessage)MessageFactory.Current.ReserveMessage(typeof(InteractResponseMessage));
                interactionResponse.RequestMessageId = interactRequest.MessageId;
                interactionResponse.FailureCode      = MxpResponseCodes.SUCCESS;
                interactionResponse.InteractionFragment.InteractionName     = "ObjectInsert";
                interactionResponse.InteractionFragment.TargetParticipantId = interactRequest.InteractionFragment.SourceParticipantId;
                interactionResponse.InteractionFragment.SourceParticipantId = interactRequest.InteractionFragment.TargetParticipantId;

                OmInsertResponseExt responseExt = new OmInsertResponseExt();
                responseExt.ObjectId = cloudObject.CloudObjectId.ToString();

                interactionResponse.SetExtension <OmInsertResponseExt>(responseExt);

                client.SendInteractResponse(interactionResponse);
            }

            if (interactRequest.InteractionFragment.InteractionName == "ObjectUpdate")
            {
                OmUpdateRequestExt requestExt = interactRequest.GetExtension <OmUpdateRequestExt>();

                Guid objectId = new Guid(requestExt.ObjectId);

                byte failureCode = MxpResponseCodes.SUCCESS;
                if (IdObjectDictionary.ContainsKey(objectId))
                {
                    DaemonLogic.CloudObject cloudObject = IdObjectDictionary[objectId];

                    if (cloudObject.Participant.ParticipantId != interactRequest.InteractionFragment.SourceParticipantId &&
                        bubble.Participant.ParticipantId != interactRequest.InteractionFragment.SourceParticipantId)
                    {
                        failureCode = MxpResponseCodes.UNAUTHORIZED_OPERATION;
                    }
                    else
                    {
                        cloudObject.Name     = requestExt.Name;
                        cloudObject.Radius   = requestExt.Scale;
                        cloudObject.X        = requestExt.Location.X;
                        cloudObject.Y        = requestExt.Location.Y;
                        cloudObject.Z        = requestExt.Location.Z;
                        cloudObject.OX       = requestExt.Orientation.X;
                        cloudObject.OY       = requestExt.Orientation.Y;
                        cloudObject.OZ       = requestExt.Orientation.Z;
                        cloudObject.OW       = requestExt.Orientation.W;
                        cloudObject.Modified = DateTime.Now;

                        entityContext.SaveChanges();
                        InjectOrUpdateObject(cloudObject);
                    }
                }
                else
                {
                    failureCode = MxpResponseCodes.UNKNOWN_ID;
                }

                InteractResponseMessage interactionResponse = (InteractResponseMessage)MessageFactory.Current.ReserveMessage(typeof(InteractResponseMessage));
                interactionResponse.RequestMessageId = interactRequest.MessageId;

                interactionResponse.FailureCode = failureCode;
                interactionResponse.InteractionFragment.InteractionName     = "ObjectUpdate";
                interactionResponse.InteractionFragment.TargetParticipantId = interactRequest.InteractionFragment.SourceParticipantId;
                interactionResponse.InteractionFragment.SourceParticipantId = interactRequest.InteractionFragment.TargetParticipantId;

                client.SendInteractResponse(interactionResponse);
            }

            if (interactRequest.InteractionFragment.InteractionName == "ObjectDelete")
            {
                OmDeleteRequestExt requestExt = interactRequest.GetExtension <OmDeleteRequestExt>();
                Guid objectId = new Guid(requestExt.ObjectId);

                byte failureCode = MxpResponseCodes.SUCCESS;

                if (this.IdObjectDictionary.ContainsKey(objectId))
                {
                    DaemonLogic.CloudObject cloudObject = IdObjectDictionary[objectId];
                    if (cloudObject.Participant.ParticipantId != interactRequest.InteractionFragment.SourceParticipantId &&
                        bubble.Participant.ParticipantId != interactRequest.InteractionFragment.SourceParticipantId)
                    {
                        failureCode = MxpResponseCodes.UNAUTHORIZED_OPERATION;
                    }
                    else
                    {
                        entityContext.DeleteObject(cloudObject);
                        entityContext.SaveChanges();
                        EjectObject(cloudObject);
                    }
                }
                else
                {
                    failureCode = MxpResponseCodes.UNKNOWN_ID;
                }

                InteractResponseMessage interactionResponse = (InteractResponseMessage)MessageFactory.Current.ReserveMessage(typeof(InteractResponseMessage));
                interactionResponse.RequestMessageId = interactRequest.MessageId;

                interactionResponse.FailureCode = failureCode;
                interactionResponse.InteractionFragment.InteractionName     = "ObjectDelete";
                interactionResponse.InteractionFragment.TargetParticipantId = interactRequest.InteractionFragment.SourceParticipantId;
                interactionResponse.InteractionFragment.SourceParticipantId = interactRequest.InteractionFragment.TargetParticipantId;

                client.SendInteractResponse(interactionResponse);
            }
        }