Reverse proxies

Did anyone solve reverse proxies question in BrowserStack online round? would be of great if anyone could help…(the challenge is over)

It was weird.

Also wanted help.

1 Like

Can you tell the complete problem?

1 Like

#include<bits/stdc++.h>
using namespace std;
//read domain
string read(string s){
int i=0;
string domain="";
while(s[i]!=’/’){
domain+=s[i];
i++;
}
return domain;
}

//readup
string read_up(string s){
int i=0;
string ip="";
string up="";
while(s[i]!=’/’){
i++;
}
i++;
while(i<s.length() && s[i]!=’/’ && s[i]!=’?’)
{
up+=s[i];
i++;
}
i++;
if(up==“machine_up”){
i+=3;
while(i<s.length())
{
ip+=s[i];
i++;
}
}
return ip;
}

//readdown
string read_down(string s){
int i=0;
string ip="";
string down="";
while(s[i]!=’/’){
i++;
}
i++;
while(i<s.length()&&s[i]!=’/’ && s[i]!=’?’)
{
down+=s[i];
i++;
}
i++;
if(down==“machine_down”){
i+=3;
while(i<s.length())
{
ip+=s[i];
i++;
}
}
return ip;
}
int main(){
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”, stdin);
freopen(“output.txt”,“w”,stdout);
#endif

int n,urlcnt,count,queries;
string url_s,s,p;
map<string,vector<string>>mp;
map<string,queue<string>>mp1;
cin>>n;
for(int i=0;i<n;i++){
cin>>p;
}

cin>>urlcnt;
for(int i=0;i<urlcnt;i++){
 cin>>url_s;
 cin>>count;
 queue<string>q;
 for(int j=0;j<count;j++){
  cin>>s;
  q.push(s);
 }
 mp1[url_s]=q;
}

cin>>queries;
for(int i=0;i<queries;i++){
cin>>s;
//cout<<“query***”<<endl;
//cout<<s<<endl;
string domain=read(s);
string up=read_up(s);
string down=read_down(s);
if(up==""&& down==""){
// cout<<“fine”<<endl;
queueq=mp1[domain];

  string machine=q.front();
  q.pop();
  //cout<<machine<<" "<<s<<endl;
  mp[machine].push_back(s);
  q.push(machine);
  mp1[domain]=q;

}
else if(up!=""){
queueq=mp1[domain];
q.push(up);
string temp=q.front();
q.pop();
q.push(temp);
mp1[domain]=q;
}
else if(down!=""){
queuemodified;
queueq=mp1[domain];
//cout<<“down”<<down<<endl;
while(!q.empty() && q.front()!=down){
//cout<<q.front()<<endl;
modified.push(q.front());
q.pop();
}
q.pop();
while(!q.empty()){
//cout<<q.front()<<endl;
modified.push(q.front());
q.pop();
}
mp1[domain]=modified;
}

}

for(auto it:mp){
cout<<it.first<<endl;
vectorvec=it.second;
for(auto it1:vec)
cout<<it1<<endl;
}

return 0;
}

I don’t understand the problem and solution.
Someone please help !!
@bidibaaz123 Can you help

        import org.apache.kafka.common.protocol.types.Field;
    
    import java.util.*;
    
    class Machine {
        static String lastMachine = "";
        boolean isUp;
        boolean lastReceived;
    
        public Machine() {
            isUp = true;
            lastReceived = false;
        }
    
        public boolean isLastReceived() {
            return lastReceived;
        }
    
        public void setLastReceived(boolean lastReceived) {
            this.lastReceived = lastReceived;
        }
    
        List<String> queries = new ArrayList<>();
    
        public List<String> getQueries() {
            return queries;
        }
    
        public void setQueries(List<String> queries) {
            this.queries = queries;
        }
    
        public boolean isUp() {
            return isUp;
        }
    
        public void setUp(boolean up) {
            isUp = up;
        }
    }
    
    class ReverseProxy {
        Map<String, Machine> machineHashMap = new TreeMap<>();
    
        public Map<String, Machine> getMachineHashMap() {
            return machineHashMap;
        }
    
        public void setMachineHashMap(Map<String, Machine> machineHashMap) {
            this.machineHashMap = machineHashMap;
        }
    }
    
    class TestClass {
        public static void main(String args[]) throws Exception {
    
            Map<String, ReverseProxy> reverseProxyMap = new HashMap<>();
            Scanner s = new Scanner(System.in);
            int n = Integer.parseInt(s.nextLine());
            String machines = s.nextLine();
            int r = Integer.parseInt(s.nextLine());
            for (int i = 0; i < r; i++) {
                String proxyName = s.nextLine();
                int m = Integer.parseInt(s.nextLine());
                String machinesR = s.nextLine();
                ReverseProxy reverseProxy = new ReverseProxy();
                String[] myMachines = machinesR.split(" ");
                for (String myMachine : myMachines) {
                    reverseProxy.getMachineHashMap().put(myMachine, new Machine());
                }
                reverseProxyMap.put(proxyName, reverseProxy);
            }
    //        System.out.println("machine: " + reverseProxyMap);
    
            int q = Integer.parseInt(s.nextLine());
            for (int i = 0; i < q; i++) {
                String query = s.nextLine();
                executeQuery(reverseProxyMap, query);
            }
    
            Set<String> keySet = reverseProxyMap.keySet();
            for (String key : keySet) {
                ReverseProxy reverseProxy = reverseProxyMap.get(key);
                Map<String, Machine> hashMap = reverseProxy.getMachineHashMap();
                Set<String> keySet1 = hashMap.keySet();
                for (String k : keySet1) {
    
                }
            }
    
    
        }
    
        private static void executeQuery(Map<String, ReverseProxy> reverseProxyMap, String query) {
            String[] strings = query.split("/", 2);
            String proxyName = strings[0];
            String remainingQuery = strings[1];
            Map<String, Machine> machineHashMap = reverseProxyMap.get(proxyName).getMachineHashMap();
            if (remainingQuery.contains("machine_down")) {
                String[] split = remainingQuery.split("=");
                String machineName = split[1];
                machineHashMap.get(machineName).setUp(false);
            } else if (remainingQuery.contains("machine_up")) {
                String[] split = remainingQuery.split("=");
                String machineName = split[1];
                machineHashMap.get(machineName).setUp(true);
            } else {
                Set<String> keySet = machineHashMap.keySet();
                for (String key : keySet) {
                    Machine machine = machineHashMap.get(key);
                    if (!Machine.lastMachine.equals(key)) {
                        if (machine.isUp) {
                            machine.getQueries().add(query);
                            Machine.lastMachine = key;
                        }
                    } else {
                        if (machine.isUp) {
                            machine.getQueries().add(query);
                            Machine.lastMachine = key;
                        }
                    }
                }
            }
    
        }
    
    
    }

executeQuery method needs some rework…

Java 8 solution

    import java.util.*;

    class Machine {
        boolean isUp;
        ArrayList<String> commands;

        public Machine() {
            isUp = true;
            commands = new ArrayList<>();
        }
    }

    class Group {
        int last;
        ArrayList<Machine> machines;
        HashMap<String, Machine> map;

        Group() {
            last = -1;
            machines = new ArrayList<>();
        }
    }
    class Network {

        Map<String, Group> groups;

        Network() {
            groups = new HashMap<>();
        }

    }
    class TestClass {
        public static void main(String args[]) throws Exception {

            Network network = new Network();
            Scanner s = new Scanner(System.in);
            int n = Integer.parseInt(s.next());
            LinkedHashMap<String, Machine> machines = new LinkedHashMap<>();
            for (int i = 0; i < n; i++) {
                String name = s.next();
                machines.put(name, new Machine());
            }

            int g = s.nextInt();
            for (int i = 0; i < g; i++) {
                String proxyName = s.next();
                int m = s.nextInt();
                Group newGroup = new Group();
                for (int j = 0; j < m; j++) {
                    String machine = s.next();
                    Machine mc = machines.get(machine);
                    newGroup.machines.add(mc);
                }
                network.groups.put(proxyName, newGroup);
            }


            int q = s.nextInt();
            for (int i = 0; i < q; i++) {
                String query = s.next();
                executeQuery(network, machines, query);
            }


            machines.forEach((key, machine) -> {
                System.out.println(key);
                for (String command : machine.commands) {
                    System.out.println(command);
                }
            });
        }

        private static void executeQuery(Network network,LinkedHashMap<String, Machine> machines, String query) {
            String[] strings = query.split("/", 2);
            if(strings.length<1) {
                return;
            }
            String proxyName = strings[0];
            String remainingQuery = strings[1];
            Group  target = network.groups.get(proxyName);
            if (remainingQuery.contains("machine_down")) {
                String[] split = remainingQuery.split("=");
                String machineName = split[1];
                machines.get(machineName).isUp = false;
            } else if (remainingQuery.contains("machine_up")) {
                String[] split = remainingQuery.split("=");
                String machineName = split[1];
                machines.get(machineName).isUp = true;
            } else {

                int toProcess = (target.last != -1 ? target.last : 0)%target.machines.size();

                for(int i=toProcess; i<target.machines.size();i++) {
                    if(target.machines.get(i).isUp) {
                        target.machines.get(i).commands.add(query);
                        target.last = i+1;
                        return;
                    }
                }

                for(int i=0; i<target.last;i++) {
                    if(target.machines.get(i).isUp) {
                        target.machines.get(i).commands.add(query);
                        target.last = i+1;
                        return;
                    }
                }
            }

        }


    }
#include<bits/stdc++.h>
using namespace std;

string read(string s){
    int i=0;
    string domain="";
    while(s[i]!='/'){
        domain+=s[i];
        i++;
    }
    return domain;
}

string read_up(string s){
    int i=0;
    string ip="";
    string up="";
    while(s[i]!='/'){
        i++;
    }
    i++;
    while(i<s.length() && s[i]!='/' && s[i]!='?')
    {
        up+=s[i];
        i++;
    }
    i++;
    if(up=="machine_up"){
        i+=3;
        while(i<s.length())
        {
            ip+=s[i];
            i++;
        }
    }
    return ip;
}

string read_down(string s){
    int i=0;
    string ip="";
    string down="";
    while(s[i]!='/'){
        i++;
    }
    i++;
    while(i<s.length()&&s[i]!='/' && s[i]!='?')
    {
        down+=s[i];
        i++;
    }
    i++;
    if(down=="machine_down"){
        i+=3;
        while(i<s.length())
        {
            ip+=s[i];
            i++;
        }
    }
    return ip;
}
int main(){


    int n,urlcnt,count,queries;
    string url_s,s,p;
    map<string,vector<string>>mp;
    map<string,queue<string>>mp1;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p;
    }

    cin>>urlcnt;
    for(int i=0;i<urlcnt;i++){
        cin>>url_s;
        cin>>count;
        queue<string>q;
        for(int j=0;j<count;j++){
            cin>>s;
            q.push(s);
        }
        mp1[url_s]=q;
    }
    cin>>queries;
    for(int i=0;i<queries;i++){
        cin>>s;

        string domain=read(s);
        string up=read_up(s);
        string down=read_down(s);
        if(up==""&& down==""){
    
            queue<string> q=mp1[domain];
        
            string machine=q.front();
            q.pop();
    
            mp[machine].push_back(s);
            q.push(machine);
            mp1[domain]=q;
        }
        else if(up!=""){
            queue<string> q=mp1[domain];
            q.push(up);
            string temp=q.front();
            q.pop();
            q.push(temp);
            mp1[domain]=q;
        }
        else if(down!=""){
            queue<string> modified;
            queue<string> q=mp1[domain];
            while(!q.empty() && q.front()!=down){
                modified.push(q.front());
                q.pop();
            }
            q.pop();
            while(!q.empty()){
                modified.push(q.front());
                q.pop();
            }
            mp1[domain]=modified;
        }
    
    }
    
    for(auto it:mp){
        cout<<it.first<<endl;
        vector<string> vec=it.second;
        for(auto it1:vec)
        cout<<it1<<endl;
    }
    
    return 0;
}