Example #1
0
        private void Purge()
        {
            SiderisDataSet db = Application[database] as SiderisDataSet;

            SiderisDataSet.PeerDataTable peers = db.Peer;

            string query = String.Format("Expires <= #{0}#",
                                         DateTime.Now);

            SiderisDataSet.PeerRow[] rows = peers.Select(query)
                                            as SiderisDataSet.PeerRow[];

            if (rows.Length == 0)
            {
                return;
            }

            lock (db)
            {
                foreach (SiderisDataSet.PeerRow row in rows)
                {
                    row.Delete();
                }
                peers.AcceptChanges();
            }
        }
Example #2
0
        public bool Unregister(int port)
        {
            Purge();

            SiderisDataSet db = Application[database] as SiderisDataSet;

            SiderisDataSet.PeerDataTable peers = db.Peer;
            SiderisDataSet.PeerRow       peer;

            //
            // Find the peer in the database, then delete it.
            // Shared files are deleted by the foreign key
            // constraint.
            //
            peer = FindPeer(port);
            if (peer != null)
            {
                lock (db)
                {
                    peer.Delete();
                    peers.AcceptChanges();
                }
                return(true);
            }

            //
            // The peer was not in the database anyway.
            //
            return(false);
        }
Example #3
0
        public bool Ping(int port, out DateTime expires)
        {
            Purge();

            SiderisDataSet db = Application[database] as SiderisDataSet;

            SiderisDataSet.PeerDataTable peers = db.Peer;
            SiderisDataSet.PeerRow       peer;

            //
            // Find the peer in the database, then update
            // the expiry time.
            //
            peer = FindPeer(port);
            if (peer != null)
            {
                expires = DateTime.Now + TimeSpan.FromMinutes(15);
                lock (db)
                {
                    peer.Expires = expires;
                    peers.AcceptChanges();
                }
                return(true);
            }

            //
            // The peer was not in the database anyway.
            //
            expires = DateTime.Now;
            return(false);
        }
Example #4
0
        private SiderisDataSet.PeerRow FindPeer(int port)
        {
            SiderisDataSet db = Application[database] as SiderisDataSet;

            SiderisDataSet.PeerDataTable peers = db.Peer;

            string address = GetPeerAddress(port);

            return(peers.FindByAddress(address));
        }
Example #5
0
        public bool Register(string name, ushort port, File[] shares, out DateTime expires)
        {
            Purge();

            SiderisDataSet db = Application[database] as SiderisDataSet;

            SiderisDataSet.PeerDataTable peers = db.Peer;
            SiderisDataSet.FileDataTable files = db.File;
            SiderisDataSet.PeerRow       peer;

            //
            // If this peer was already in the database,
            // remove it. Shared files are removed by the
            // foreign key constraint.
            //
            peer = FindPeer(port);
            if (peer != null)
            {
                lock (db)
                {
                    peer.Delete();
                    peers.AcceptChanges();
                }
            }

            //
            // Compute an expiry date and time for the peer,
            // and add it to the database.
            //
            expires = DateTime.Now + TimeSpan.FromMinutes(15);
            string address = GetPeerAddress(port);

            lock (db)
            {
                peer = peers.AddPeerRow(address, expires, name);

                //
                // Add the files shared by the peer to the database.
                //
                foreach (File file in shares)
                {
                    files.AddFileRow(peer, file.Hash, file.Size,
                                     file.Name);
                }

                db.AcceptChanges();
            }

            SaveData();

            return(true);
        }