Пример #1
0
        public void UpdateLocation()
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreBlocked(delegate() { UpdateLocation(); });
                return;
            }

            LocationData location = GetLocalLocation();

            byte[] signed = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, location);

            Store_Local(new DataReq(null, Core.UserID, ServiceID, 0, signed));

            // update oldest to newest (update oldest with new address/info before we ping timeout)
            PendingNotifications = new LinkedList <DhtClient>(NotifyUsers.Keys.OrderBy(c => NotifyUsers[c]));
        }
Пример #2
0
        private void CacheFile(SignedData signedHeader, VersionedFileHeader header)
        {
            if (Core.InvokeRequired)
                Debug.Assert(false);

            try
            {
                // check if file exists
                string path = "";
                if (header.FileHash != null)
                {
                    path = GetFilePath(header);
                    if (!File.Exists(path))
                    {
                        Download(signedHeader, header);
                        return;
                    }
                }

                // get file
                OpVersionedFile prevFile = GetFile(header.KeyID);

                if (prevFile != null)
                    if (header.Version < prevFile.Header.Version)
                        return; // dont update with older version

                OpVersionedFile newFile = new OpVersionedFile(header.Key);

                // set new header
                newFile.Header = header;
                newFile.SignedHeader = signedHeader.Encode(Network.Protocol);
                newFile.Unique = !Network.Established;

                FileMap.SafeAdd(header.KeyID, newFile);

                RunSaveHeaders = true;

                if(FileAquired != null)
                    FileAquired.Invoke(newFile);

                // delete old file - do after aquired event so invoked (storage) can perform clean up operation
                if (prevFile != null && prevFile.Header.FileHash != null)
                {
                    string oldPath = GetFilePath(prevFile.Header);
                    if (path != oldPath && File.Exists(oldPath))
                        try { File.Delete(oldPath); }
                        catch { }
                }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error caching data " + ex.Message);
            }
        }
Пример #3
0
        private void Process_UplinkReq(DataReq data, SignedData signed, UplinkRequest request)
        {
            Core.IndexKey(request.KeyID, ref request.Key);
            Core.IndexKey(request.TargetID, ref request.Target);

            if (!Utilities.CheckSignedData(request.Key, signed.Data, signed.Signature))
                return;

            OpTrust requesterTrust = GetTrust(request.KeyID);

            if (requesterTrust != null && requesterTrust.Loaded && requesterTrust.File.Header.Version > request.LinkVersion)
                return;

            // check if target in linkmap, if not add
            OpTrust targetTrust = GetTrust(request.TargetID);

            if (targetTrust == null)
            {
                targetTrust = new OpTrust(new OpVersionedFile(request.Target) );
                TrustMap.SafeAdd(request.TargetID, targetTrust);
            }

            if (targetTrust.Loaded && targetTrust.File.Header.Version > request.TargetVersion)
                return;

            request.Signed = signed.Encode(Network.Protocol); // so we can send it in results / save, later on

            // check for duplicate requests
            OpLink targetLink = targetTrust.GetLink(request.ProjectID);

            if (targetLink != null)
            {
                foreach (UplinkRequest compare in targetLink.Requests)
                    if (Utilities.MemCompare(compare.Signed, request.Signed))
                        return;
            }
            else
            {
                targetTrust.AddProject(request.ProjectID, true);
                targetLink = targetTrust.GetLink(request.ProjectID);
            }

            // add
            targetLink.Requests.Add(request);

            // if target is marked as linked or focused, update link of target and sender
            if (targetTrust.Loaded && (targetTrust.InLocalLinkTree || Core.KeepData.SafeContainsKey(targetTrust.UserID)))
            {
                if (targetTrust.File.Header.Version < request.TargetVersion)
                    Cache.Research(targetTrust.UserID);

                if (requesterTrust == null)
                {
                    requesterTrust = new OpTrust(new OpVersionedFile(request.Key));
                    TrustMap.SafeAdd(request.KeyID, requesterTrust);
                }

                // once new version of requester's link file has been downloaded, interface will be updated
                if (!requesterTrust.Loaded || (requesterTrust.File.Header.Version < request.LinkVersion))
                    Cache.Research(requesterTrust.UserID);
            }

            RunSaveUplinks = true;
        }
Пример #4
0
        private void CacheFile(SignedData signedHeader, VersionedFileHeader header)
        {
            if (Core.InvokeRequired)
            {
                Debug.Assert(false);
            }

            try
            {
                // check if file exists
                string path = "";
                if (header.FileHash != null)
                {
                    path = GetFilePath(header);
                    if (!File.Exists(path))
                    {
                        Download(signedHeader, header);
                        return;
                    }
                }

                // get file
                OpVersionedFile prevFile = GetFile(header.KeyID);

                if (prevFile != null)
                {
                    if (header.Version < prevFile.Header.Version)
                    {
                        return; // dont update with older version
                    }
                }
                OpVersionedFile newFile = new OpVersionedFile(header.Key);


                // set new header
                newFile.Header       = header;
                newFile.SignedHeader = signedHeader.Encode(Network.Protocol);
                newFile.Unique       = !Network.Established;

                FileMap.SafeAdd(header.KeyID, newFile);

                RunSaveHeaders = true;

                if (FileAquired != null)
                {
                    FileAquired.Invoke(newFile);
                }


                // delete old file - do after aquired event so invoked (storage) can perform clean up operation
                if (prevFile != null && prevFile.Header.FileHash != null)
                {
                    string oldPath = GetFilePath(prevFile.Header);
                    if (path != oldPath && File.Exists(oldPath))
                    {
                        try { File.Delete(oldPath); }
                        catch { }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error caching data " + ex.Message);
            }
        }
Пример #5
0
        private void CachePost(SignedData signedHeader, PostHeader header)
        {
            if (Core.InvokeRequired)
                Debug.Assert(false);

            if (header.ParentID == 0 && header.SourceID != header.TargetID)
            {
                Network.UpdateLog("Board", "Post made to board other than source's");
                return;
            }

            if (!File.Exists(GetPostPath(header)))
            {
                DownloadPost(signedHeader, header);
                return;
            }

            // check if current version loaded
            OpPost post = GetPost(header);

            if (post != null && post.Header.Version >= header.Version )
            {
                UpdateGui(post);
                return;
            }

            // put into map
            OpBoard board = GetBoard(header.TargetID);

            if (board == null)
            {
                board = new OpBoard(header.Target);
                BoardMap.SafeAdd(header.TargetID, board);
            }

            PostUID uid = new PostUID(header);

            post = new OpPost();
            post.Header = header;
            post.SignedHeader = signedHeader.Encode(Network.Protocol);
            post.Ident = header.TargetID.GetHashCode() ^ uid.GetHashCode();
            post.Unique = Loading;

            // remove previous version of file, if its different
            OpPost prevPost = board.GetPost(uid);

            if (prevPost != null && GetPostPath(prevPost.Header) != GetPostPath(header))
                try { File.Delete(GetPostPath(prevPost.Header)); }
                catch { }

            board.Posts.SafeAdd(uid,  post);

            // update replies
            if (post.Header.ParentID == 0)
                board.UpdateReplies(post);
            else
            {
                PostUID parentUid = new PostUID(board.UserID, post.Header.ProjectID, post.Header.ParentID);
                OpPost parentPost = board.GetPost(parentUid);

                if (parentPost != null)
                {
                    board.UpdateReplies(parentPost);
                    UpdateGui(post);
                }
            }

            lock (SaveHeaders)
                if (!SaveHeaders.Contains(header.TargetID))
                    SaveHeaders.Add(header.TargetID);

            ushort replies = 0;
            if (SavedReplyCount.SafeTryGetValue(post.Ident, out replies))
            {
                post.Replies = replies;
                SavedReplyCount.SafeRemove(post.Ident);
            }

            UpdateGui(post);

            if (Core.NewsWorthy(header.TargetID, header.ProjectID, true))
                Core.MakeNews(ServiceIDs.Board, "Board updated by " + Core.GetName(header.SourceID), header.SourceID, 0, false);
        }
Пример #6
0
        private void Process_LocationData(DataReq data, SignedData signed, LocationData location)
        {
            Core.IndexKey(location.UserID, ref location.Key);

            Debug.Assert(location.UserID == location.Source.UserID);
            if (location.UserID != location.Source.UserID)
            {
                return;
            }


            ClientInfo client = GetLocationInfo(location.UserID, location.Source.ClientID);

            // check location version
            if (client != null)
            {
                if (location.Version == client.Data.Version)
                {
                    return;
                }

                else if (location.Version < client.Data.Version)
                {
                    if (data != null && data.Source != null)
                    {
                        Network.Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, client.Data.UserID, ServiceID, 0, client.SignedData));
                    }

                    return;
                }
            }

            Core.IndexName(location.UserID, location.Name);

            // notify components of new versions (usually just localsync service signed up for this)
            DhtAddress address = new DhtAddress(location.IP, location.Source);

            foreach (PatchTag tag in location.Tags)
            {
                if (TagReceived.Contains(tag.Service, tag.DataType))
                {
                    TagReceived[tag.Service, tag.DataType].Invoke(address, location.UserID, tag.Tag);
                }
            }


            // add location
            if (client == null)
            {
                // if too many clients, and not us, return
                if (location.UserID != Core.UserID && ActiveClientCount(location.UserID) > MaxClientsperUser)
                {
                    return;
                }

                client = new ClientInfo(location);

                Clients.SafeAdd(client.RoutingID, client);

                // dont need to worry about remote caching old locs indefinitely because if a loc is cached remotely
                // that means the remote is being continuall pinged, or else the loc would expire
                // if we're still interested in loc after a min, it will be pinged locally
            }

            client.Data       = location;
            client.SignedData = signed.Encode(Network.Protocol);

            if (client.Data.UserID == Core.UserID && client.Data.Source.ClientID == Network.Local.ClientID)
            {
                LocalClient = client;
            }


            AddRoutingData(location);

            // only get down here if loc was new version in first place (recently published)
            // with live comm trickle down this prevents highers from being direct ping flooded to find their
            // online status
            client.LastSeen = Core.TimeNow;

            SignalUpdate(client, true);
        }