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…