public static void sendResourceInfo(IPEndPoint destination, RingInfo ringInfo, Resource[] resources)
        {
            byte[] message = new byte[Constants.WRITEBUFFSIZE];
            MemoryStream stream;

            try
            {
                User user = User.getInstance();
                BinaryFormatter serializer = new BinaryFormatter();
                stream = new MemoryStream(message);

                NetLib.insertEntropyHeader(serializer, stream);

                serializer.Serialize(stream, Constants.MessageTypes.MSG_QUERYHIT);
                serializer.Serialize(stream, ringInfo.ring.ringID);
                serializer.Serialize(stream, ringInfo.token);
                serializer.Serialize(stream, new Peer(user.node, user.publicUserInfo, ringInfo.IE));
                serializer.Serialize(stream, resources.Length);

                foreach (Resource resource in resources)
                {
                    serializer.Serialize(stream, resource.header);
                }

                NetLib.communicateAsync(destination, message, false);
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }
        public void receiveAndSendHello(RingInfo ringInfo, Peer peer, Neighbor neighbor, 
            BinaryWriter writer)
        {
            if(!peer.node.syncCommunicationPoint.Address.Equals(neighbor.peer.node.syncCommunicationPoint.Address))
                //REVISIT: alert security system
                return;

            neighbor.peer.node.syncCommunicationPoint = peer.node.syncCommunicationPoint;
            neighbor.peer.node.asyncCommunicationPoint = peer.node.asyncCommunicationPoint;
            neighbor.peer.IE = peer.IE;

            byte[] message = new byte[Constants.WRITEBUFFSIZE];
            MemoryStream stream;

            try
            {
                User user = User.getInstance();
                BinaryFormatter serializer = new BinaryFormatter();
                stream = new MemoryStream(message);

                NetLib.insertEntropyHeader(serializer, stream);

                serializer.Serialize(stream, Constants.MessageTypes.MSG_HELLO);
                serializer.Serialize(stream, ringInfo.ring.ringID);
                serializer.Serialize(stream, ringInfo.token);
                serializer.Serialize(stream, new Peer(user.node, user.publicUserInfo, ringInfo.IE));
                writer.Write(message);
                writer.Flush();
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }
 public User(RingInfo[] __ringsInfo, PublicUserInfo __publicUserInfo, PrivateUserInfo __privateUserInfo, Node __node)
 {
     ringsInfo = __ringsInfo;
     publicUserInfo = __publicUserInfo;
     privateUserInfo = __privateUserInfo;
     node = __node;
     loggedIn = false;
 }
 public Download(Peer __peer, RingInfo __ringInfo, ResourceHeader __header, 
     UICallBack __UIHandler)
 {
     peer = __peer;
     ringInfo = __ringInfo;
     header = __header;
     //REVISIT: make sure there is enough room for meta data. Also if very large you want to split up into a list of
     //buffers or circular buffer where the buffer is written to temp file at milestones.
     buffer = new byte[header.size];
     bytesRead = 0;
     UIHandler = __UIHandler;
 }
 public bool Download(Peer peer, RingInfo ringInfo, ResourceHeader resourceHeader, 
     UICallBack UIHndlr)
 {
     Download download = new Download(peer, ringInfo, resourceHeader, UIHndlr);
     int downloadIndex = Add(download);
     if (downloadIndex < 0)
     {
         Queue(download);
         return false;
     }
     Download_Internal(download);
     return true;
 }
        public User(string[] usernames, string[] passwords)
        {
            if(usernames.Length != passwords.Length)
                return;

            ringsInfo = new RingInfo[usernames.Length];
            for(int ringIndx = 0; ringIndx < usernames.Length; ringIndx++)
            {
                ringsInfo[ringIndx] = new RingInfo(null,usernames[ringIndx], passwords[ringIndx]);
            }

            publicUserInfo = new PublicUserInfo();
            privateUserInfo = new PrivateUserInfo();
            loggedIn = false;
        }
 public static RingInfo findRingInfoByID(RingInfo[] ringInfos, uint ringID)
 {
     for(uint ringInfo = 0; ringInfo < ringInfos.Length; ringInfo++)
     {
         if(ringInfos[ringInfo].ring.ringID == ringID)
             return ringInfos[ringInfo];
     }
     return null;
 }
        public void sendQuery(Peer neighbor, RingInfo ringInfo, string[] columns, string[] query, 
            Node sender, int ttl)
        {
            byte[] message = new byte[Constants.WRITEBUFFSIZE];
            MemoryStream stream;

            if (ttl <= 0)
                return;

            try
            {
                BinaryFormatter serializer = new BinaryFormatter();
                stream = new MemoryStream(message);

                NetLib.insertEntropyHeader(serializer, stream);

                if (columns == null)
                    serializer.Serialize(stream, Constants.MessageTypes.MSG_SIMPLEQUERY);
                else
                    serializer.Serialize(stream, Constants.MessageTypes.MSG_COLUMNQUERY);

                serializer.Serialize(stream, ringInfo.token);
                serializer.Serialize(stream, ringInfo.ring.ringID);
                serializer.Serialize(stream, sender.asyncCommunicationPoint);
                serializer.Serialize(stream, ttl);  //TTL
                if (columns == null)
                    serializer.Serialize(stream, Constants.NULL_STRING_ARRAY);
                else
                    serializer.Serialize(stream, columns);
                serializer.Serialize(stream, query);

                NetLib.communicateAsync(neighbor.node.asyncCommunicationPoint, message, false);
            }
            catch (Exception e)
            {
                int x = 2;  //REVISIT: probably need to send info to logger.
            }
        }
        public void showQueryHit(RingInfo ringInfo, Peer hitPeer, ResourceHeader resourceHeader)
        {
            DataRow dr = queryResultsTable.NewRow();

            dr[0] = hitPeer.userInfo.nickname;
            dr[1] = resourceHeader.resourceID;
            dr[2] = resourceHeader.name;
            dr[3] = resourceHeader.size;
            dr[4] = "";
            dr[5] = hitPeer;
            dr[6] = ringInfo;
            dr[7] = resourceHeader;

            this.queryResultsTable.Rows.Add(dr);
        }
        private void showCatalog(RingInfo ringInfo)
        {
            TreeNode ringNode, groupNode, theNode;

            if (ringInfo == null || ringInfo.cataloger == null || ringInfo.cataloger.groups == null)
                return;

            ringNode = resourceTree.Nodes.Add(ringInfo.ring.ringName);

            foreach (ResourceGroup group in ringInfo.cataloger.groups)
            {
                groupNode = ringNode.Nodes.Add(group.groupName);
                if (group is FileGroup)
                {
                    IDictionaryEnumerator enumerator = ((FileGroup)group).resourceList.GetEnumerator();
                    while(enumerator.MoveNext())
                    {
                        Resource resource = (Resource)enumerator.Value;
                        theNode = groupNode.Nodes.Add(((FileInfo)resource.data).FullName);
                        theNode.Tag = resource;
                    }
                }
            }

            // REVISIT: This is temp for debugging, remove please. We should have at least one neighbor
            if(User.getInstance().ringsInfo[0].neighbors != null && User.getInstance().ringsInfo[0].neighbors.Count != 0)
            {
                string temp = commPointLabel.Text;
                commPointLabel.Text = commPointLabel.Text + "," + User.getInstance().ringsInfo[0].ring.ringName + ": (" +
                    ((Neighbor)User.getInstance().ringsInfo[0].neighbors[0]).peer.node.asyncCommunicationPoint.Port + ", " +
                    ((Neighbor)User.getInstance().ringsInfo[0].neighbors[0]).peer.node.syncCommunicationPoint.Port + ")\n";
            }
            commPointLabel.Text = commPointLabel.Text + "IE: " + LibUtil.detokenize(
                LibUtil.InformationEntropyToStringArray(User.getInstance().ringsInfo[0].IE), ' ') + "\n";
        }
 public void RetreiveResource(Peer peer, RingInfo ringInfo, ResourceHeader resourceHeader, 
     UICallBack UIHndlr)
 {
     downloadManager.Download(peer, ringInfo, resourceHeader, UIHndlr);
 }