Ejemplo n.º 1
0
        public void Shutdown()
        {
            if (service != null)
            {
                lock (this)
                {
                    LogUtil.Info("Daemon Process shutdown...");

                    CloudService cloudService = service;

                    try
                    {
                        foreach (DaemonParticipant daemonParticipant in bubbleDaemons.Values)
                        {
                            daemonParticipant.Shutdown();
                        }

                        cloudService.Shutdown();
                    }
                    finally
                    {
                        // Removing process state info from database.
                        if (localProcessStateEntity != null)
                        {
                            entityContext.DeleteObject(localProcessStateEntity);
                            entityContext.SaveChanges();
                        }
                    }

                    service = null;
                    LogUtil.Info("Daemon Process shutdown done.");
                }
            }
        }
Ejemplo n.º 2
0
 public static RemoteProcess AddRemoteProcess(Participant participant, LocalProcess locaProcess)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(locaProcess);
             entities.Attach(participant);
             RemoteProcess remoteProcess = new RemoteProcess
             {
                 RemoteProcessId = Guid.NewGuid(),
                 LocalProcess    = locaProcess,
                 Participant     = participant,
                 Address         = "127.0.0.1",
                 HubPort         = MxpConstants.DefaultHubPort,
                 Trusted         = false
             };
             entities.AddToRemoteProcess(remoteProcess);
             entities.SaveChanges();
             entities.Detach(remoteProcess);
             return(remoteProcess);
         }
         finally
         {
             entities.Detach(locaProcess);
             entities.Detach(participant);
         }
     }
 }
Ejemplo n.º 3
0
 public static ObjectType AddObjectType(Participant participant)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(participant);
             ObjectType objectType = new ObjectType
             {
                 ObjectTypeId = Guid.NewGuid(),
                 Participant  = participant,
                 Name         = "New Object Type",
                 Radius       = 11,
                 Mass         = 10,
                 ModelUrl     = "http://",
                 ModelScale   = 10,
                 Published    = false
             };
             entities.AddToObjectType(objectType);
             entities.SaveChanges();
             entities.Detach(objectType);
             return(objectType);
         }
         finally
         {
             entities.Detach(participant);
         }
     }
 }
Ejemplo n.º 4
0
 public static Bubble AddBuble(Participant participant, LocalProcess localProcess)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(participant);
             entities.Attach(localProcess);
             Bubble bubble = new Bubble
             {
                 BubbleId        = Guid.NewGuid(),
                 Participant     = participant,
                 LocalProcess    = localProcess,
                 Name            = "New Bubble",
                 Range           = 100,
                 PerceptionRange = 150,
                 Published       = false
             };
             entities.AddToBubble(bubble);
             entities.SaveChanges();
             entities.Detach(bubble);
             return(bubble);
         }
         finally
         {
             entities.Detach(participant);
             entities.Detach(localProcess);
         }
     }
 }
Ejemplo n.º 5
0
        public static Participant AttachParticipantProfileToOpenIdIdentity(int userId, string openIdUrl)
        {
            using (DaemonEntities entities = new DaemonEntities())
            {
                OpenIdUser  user        = (from u in entities.OpenIdUser where u.UserId == userId select u).First <OpenIdUser>();
                Participant participant = QueryUtil.First <Participant>(from p in entities.Participant where p.User.UserId == user.UserId select p);

                // If participant is not attached try to find existing participant with openIdUrl
                if (participant == null)
                {
                    participant = QueryUtil.First <Participant>(from p in entities.Participant where p.OpenIdUrl == openIdUrl select p);
                    if (participant != null)
                    {
                        // If participant is not attached to user then attach it.
                        if (participant.User == null)
                        {
                            participant.User = user;
                            entities.SaveChanges();
                        }
                        else
                        {
                            throw new ArgumentException("Participant exists with the same OpenId but already connected to different user.");
                        }
                    }
                }

                // If participant is still null then create new participant and attach to user.
                if (participant == null)
                {
                    participant = new Participant
                    {
                        ParticipantId = new Guid(UUIDGenerator.Current.GenerateNameBasedUUID(new UUID(MxpConstants.MxpNamespaceId.ToString()), openIdUrl).ToString()),
                        OpenIdUrl     = openIdUrl,
                        User          = user
                    };
                    entities.AddToParticipant(participant);
                    entities.SaveChanges();
                }

                entities.Detach(participant);
                return(participant);
            }
        }
Ejemplo n.º 6
0
 public static void ClearLoginSecret(Participant participant)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         entities.Attach(participant);
         participant.LoginSecret        = null;
         participant.LoginSecretExpires = null;
         entities.SaveChanges();
         entities.Detach(participant);
     }
 }
Ejemplo n.º 7
0
 public static void GetLoginSecret(Participant participant)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         entities.Attach(participant);
         participant.LoginSecret        = Guid.NewGuid().ToString("N");
         participant.LoginSecretExpires = DateTime.Now.Add(new TimeSpan(0, 0, 0, 10));
         entities.SaveChanges();
         entities.Detach(participant);
     }
 }
Ejemplo n.º 8
0
        public void TestParticipantDatabase()
        {
            using (DaemonEntities daemonEntities = new DaemonEntities("metadata=res://*/Daemon.csdl|res://*/Daemon.ssdl|res://*/Daemon.msl;provider=System.Data.SqlClient;provider connection string=';Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Directory.GetParent(this.GetType().Assembly.Location).Parent.Parent.Parent.FullName + "\\CloudDaemonWeb\\App_Data\\CloudDaemonWeb.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True'"))
            {
                Guid   participantId = Guid.NewGuid();
                string openIdUrl     = "http://testid.openid.com/";

                Participant newParticipant = new Participant
                {
                    ParticipantId = participantId,
                    OpenIdUrl     = openIdUrl
                };

                daemonEntities.AddToParticipant(newParticipant);
                daemonEntities.SaveChanges();

                IQueryable <Participant> participantQuery =
                    from p in daemonEntities.Participant where p.ParticipantId == participantId select p;

                Assert.AreEqual(1, participantQuery.Count <Participant>());

                IEnumerator <Participant> enumerator = participantQuery.GetEnumerator();
                enumerator.MoveNext();
                Participant loadedParticipant = enumerator.Current;
                enumerator.Dispose();

                Assert.AreEqual(participantId, loadedParticipant.ParticipantId);
                Assert.AreEqual(openIdUrl, loadedParticipant.OpenIdUrl);


                daemonEntities.DeleteObject(loadedParticipant);

                daemonEntities.SaveChanges();

                Assert.AreEqual(0, (from p in daemonEntities.Participant where p.ParticipantId == participantId select p).Count <Participant>());
            }
        }
Ejemplo n.º 9
0
 public static void UpdateObject(CloudObject cloudObject)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(cloudObject);
             ObjectType  objectType  = QueryUtil.First <ObjectType>(from o in entities.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.ObjectType);
             Participant participant = QueryUtil.First <Participant>(from o in entities.CloudObject where o.CloudObjectId == cloudObject.CloudObjectId select o.Participant);
             cloudObject.Modified = DateTime.Now;
             entities.SaveChanges();
         }
         finally
         {
             entities.Detach(cloudObject);
         }
     }
 }
Ejemplo n.º 10
0
 public static CloudObject AddObject(Participant participant, ObjectType objectType, Bubble bubble)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(participant);
             entities.Attach(objectType);
             entities.Attach(bubble);
             CloudObject cloudObject = new 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             = 0,
                 Y             = 0,
                 Z             = 0,
                 OX            = 0,
                 OY            = 0,
                 OZ            = 0,
                 OW            = 0,
                 Created       = DateTime.Now,
                 Modified      = DateTime.Now,
                 Enabled       = false
             };
             entities.AddToCloudObject(cloudObject);
             entities.SaveChanges();
             entities.Detach(cloudObject);
             return(cloudObject);
         }
         finally
         {
             entities.Detach(participant);
             entities.Detach(objectType);
             entities.Detach(bubble);
         }
     }
 }
Ejemplo n.º 11
0
        public static LocalProcess ReserveLocalProcess(Participant participant, string address)
        {
            using (DaemonEntities entities = new DaemonEntities())
            {
                entities.Attach(participant);

                try
                {
                    int serverPort = MxpConstants.DefaultServerPort;
                    if ((from l in entities.LocalProcess select l).Count() > 0)
                    {
                        serverPort = (from l in entities.LocalProcess select l).Max(l => l.ServerPort) + 2;
                    }
                    int hubPort = serverPort + 1;

                    LocalProcess localProcess = new LocalProcess
                    {
                        LocalProcessId = Guid.NewGuid(),
                        Participant    = participant,
                        Name           = "(" + serverPort + "," + hubPort + ")",
                        Address        = address,
                        ServerPort     = serverPort,
                        HubPort        = hubPort,
                        Enabled        = false
                    };

                    entities.AddToLocalProcess(localProcess);
                    entities.SaveChanges();
                    entities.Detach(localProcess);

                    return(localProcess);
                }
                finally
                {
                    entities.Detach(participant);
                }
            }
        }
Ejemplo n.º 12
0
 public static BubbleLink AddBubleLink(Participant participant, Bubble bubble)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         try
         {
             entities.Attach(participant);
             entities.Attach(bubble);
             if (bubble.Participant != participant)
             {
                 throw new UnauthorizedAccessException("You are not owner of this bubble.");
             }
             BubbleLink bubbleLink = new BubbleLink
             {
                 BubbleLinkId   = Guid.NewGuid(),
                 RemoteBubbleId = Guid.Empty,
                 Bubble         = bubble,
                 Name           = "New Bubble Link",
                 Address        = "127.0.0.1",
                 Port           = MxpConstants.DefaultHubPort,
                 X       = 50,
                 Y       = 50,
                 Z       = 0,
                 Enabled = false
             };
             entities.AddToBubbleLink(bubbleLink);
             entities.SaveChanges();
             entities.Detach(bubbleLink);
             return(bubbleLink);
         }
         finally
         {
             entities.Detach(bubble);
             entities.Detach(participant);
         }
     }
 }
Ejemplo n.º 13
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);
            }
        }
Ejemplo n.º 14
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.");
            }
        }
Ejemplo n.º 15
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);
            }
        }