
package kr.ac.kaist.ds.groupd.search.impl;

import java.util.ArrayList;

import kr.ac.kaist.ds.groupd.search.SearchProtocol;
import kr.ac.kaist.ds.groupd.search.SearchQuery;
import peersim.config.FastConfig;
import peersim.core.CommonState;
import peersim.core.Linkable;
import peersim.core.Node;
import kr.ac.kaist.ds.groupd.interest.InterestProtocol;
import kr.ac.kaist.ds.groupd.example.token.TokenProtocol;
import kr.ac.kaist.ds.groupd.groupname.GroupNameProtocol;

public class SearchProtocolImpl implements SearchProtocol {

    private static final String PAR_NAME_PROTOCOL = "naming";

    private static final String PAR_INTEREST_GROUP_PROTOCOL = "interestgroup";

    private InterestProtocol interestProtocol;

    private GroupNameProtocol groupNameProtocol;

    private SearchQuery searchQuery;
    
    static ArrayList<Long> checkForPeers;

    private boolean token;

    public SearchProtocolImpl(String name) {
    }

    public boolean hasToken() {
        return token;
    }

    public void resetToken() {
        this.token = false;
    }

    /**
     * Performs the forward search based on the ADGSA algorithm.
     */
    private void performSearch(Node node, int pid) {

        //1~2
        if(checkForPeers.contains(node.getID()))
            return;
        else
            checkForPeers.add(node.getID());
        
        //3~4 token will be change other.
        if (true == token) {

            Linkable linkable = (Linkable)node.getProtocol(FastConfig.getLinkable(pid));

            for (int i = 0; i < linkable.degree(); i++) {
                Node neighbor = linkable.getNeighbor(CommonState.r.nextInt(i));
                TokenProtocol protocol = (TokenProtocol)neighbor.getProtocol(pid);
                protocol.setToken(true);
            }
        }

    }

    /**
     * Performs the backtracking, based on our group naming scheme. backTracking
     * follow previous steps
     */
    private void performBacktracking() {

    }

    /**
     * @see kr.ac.kaist.ds.groupd.search.SearchProtocol#setSearchQuery(kr.ac.kaist.ds.groupd.search.SearchQuery)
     */
    public void setSearchQuery(SearchQuery q) {

    }

    @Override
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void nextCycle(Node node, int protocolID) {
        performSearch(node, protocolID);

    }

}

@@ -829,20 +829,16 @@
 Query;%0D%0A
-    
 %0D%0A    st
@@ -872,24 +872,76 @@
 orPeers;%0D%0A%0D%0A
+    private ArrayList%3CNode%3E celculateForDegrees;%0D%0A%0D%0A
     private 
@@ -1284,24 +1284,47 @@
 t pid) %7B%0D%0A%0D%0A
+        long degee;%0D%0A%0D%0A
         //1~
@@ -1321,16 +1321,17 @@
       //
+ 
 1~2%0D%0A   
@@ -1337,16 +1337,17 @@
       if
+ 
 (checkFo
@@ -1459,24 +1459,16 @@
 ID());%0D%0A
-        
 %0D%0A      
@@ -1471,16 +1471,17 @@
       //
+ 
 3~4 toke
@@ -1745,32 +1745,9 @@
 bor(
-CommonState.r.nextInt(i)
+i
 );%0D%0A
@@ -1888,22 +1888,631 @@
     
-%7D%0D%0A        %7D%0D%0A
+    celculateForDegrees.add(linkable.getNeighbor(i));%0D%0A            %7D%0D%0A%0D%0A            for (int i = 0; i %3C celculateForDegrees.size(); i++) %7B%0D%0A                for (int j = 0; j %3C checkForPeers.size(); j++) %7B%0D%0A                    if (celculateForDegrees.get(i).getID() == checkForPeers.get(j)) %7B%0D%0A                        celculateForDegrees.remove(i);%0D%0A                        continue;%0D%0A                    %7D%0D%0A                %7D%0D%0A            %7D%0D%0A            %0D%0A            //5~6%0D%0A            degee = (Degree(celculateForDegrees));%0D%0A%0D%0A        %7D%0D%0A%0D%0A    %7D%0D%0A    %0D%0A    private long Degree(ArrayList%3CNode%3E S)%0D%0A    %7B%0D%0A        return -1;
 %0D%0A  

implement some code that degree parts of the ADGSA. (Compute the degree of peers by Degree(S);)