示例#1
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);
         }
     }
 }
示例#2
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);
         }
     }
 }
示例#3
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);
         }
     }
 }
示例#4
0
 public static ObjectType GetObjectType(Guid objectTypeId)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         ObjectType objectType = QueryUtil.First <ObjectType>((from o in entities.ObjectType where o.ObjectTypeId == objectTypeId select o));
         entities.Detach(objectType);
         return(objectType);
     }
 }
示例#5
0
 public static Bubble GetBubble(Guid bubbleId)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         Bubble bubble = QueryUtil.First <Bubble>((from b in entities.Bubble where b.BubbleId == bubbleId select b));
         entities.Detach(bubble);
         return(bubble);
     }
 }
示例#6
0
 public static Participant GetParticipant(Guid participantId)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         Participant participant = QueryUtil.First <Participant>(from p in entities.Participant where p.ParticipantId == participantId select p);
         entities.Detach(participant);
         return(participant);
     }
 }
示例#7
0
 public static LocalProcess GetLocalProcess(Guid localProcessId)
 {
     using (DaemonEntities entities = new DaemonEntities())
     {
         LocalProcess localProcess = QueryUtil.First <LocalProcess>((from p in entities.LocalProcess where p.LocalProcessId == localProcessId select p));
         entities.Detach(localProcess);
         return(localProcess);
     }
 }
示例#8
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);
     }
 }
示例#9
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);
     }
 }
示例#10
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);
         }
     }
 }
示例#11
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);
         }
     }
 }
示例#12
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);
            }
        }
示例#13
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);
                }
            }
        }
示例#14
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>());
            }
        }
示例#15
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);
         }
     }
 }
示例#16
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);
            }
        }
示例#17
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.");
            }
        }