diff --git a/FTP Server/client.py b/FTP Server/client.py new file mode 100644 index 0000000..0b8d05e --- /dev/null +++ b/FTP Server/client.py @@ -0,0 +1,131 @@ +import socket +IP=socket.gethostbyname(socket.gethostname()) +PORT=3030 +FORMAT='utf-8' +HEADER=10 +DISCONNECT_MESSAGE="disconnect" + +ADDR=(IP,PORT) + +client=socket.socket(socket.AF_INET,socket.SOCK_STREAM) + +client.connect(ADDR) +print(client.recv(1024).decode(FORMAT)) +def send(msg): + message=msg.encode(FORMAT) + client.send(message) + +def RETR(client,path): + data = client.recv(1024).decode() + + with open(path,'w') as f: + data = client.recv(1024).decode() + f.write(data) + +def STOR(client,path): + with open(path,'rb') as f: + data = f.read() + client.sendall(data) + +def authorization(): + + user=input("Enter username:") + send(user) + passw=input("Enter password:") + send(passw) + s=client.recv(1024).decode(FORMAT) + if s=="11": + print("Logged in as admin") + return 1 + elif s=="10": + print("Login successful") + return 0 + + elif s=="3": + print("You are a new user do you want to sign up.\nIf yes press 1,else 0") + ch=int(input("1/0:")) + if ch==1: + send("1") + print("Registered as a new user") + else : + send("hfjd") + print("Thank you") + client.close() + + return 0 + elif s=="4": + print("You are banned from the server") + client.close() + + else : + print("Invalid credentials ") + client.close() + + +def LIST(client): + print(client.recv(1024).decode(FORMAT)) + + +def RETR(client,path): + data = client.recv(1024).decode() + + with open(path,'w') as f: + data = client.recv(1024).decode() + f.write(data) + + print("File retrieved succesfully!") + + + + +role=authorization() + +if role==0: + while True: + command = input("Enter a command (LIST, RETR , STOR or QUIT): ").strip() + send(command) + if command == "LIST": + LIST(client) + elif command.startswith("RETR"): + _, filename = command.split(' ', 1) + + RETR(client,filename.strip()) + elif command.startswith("STOR"): + _, filename = command.split(' ', 1) + + STOR(client,filename) + elif command == "QUIT": + print("Disconnecting from the server...") + break + else: + print("Invalid command. Please try again.") +if role == 1: + while True: + command = input("Enter a command (ADDUSER , DELUSER , BAN ,UNBAN ,QUIT): ").strip() + send(command) + if command.startswith("ADDUSER"): + _, username, password = command.split(' ', 2) + print(f"{username} is successfully added to the server") + + + elif command.startswith("DELUSER"): + _, username = command.split(' ', 1) + print(f"{username} is deleted from the server") + + elif command.startswith("BAN"): + _, username = command.split(' ', 1) + print(f"{username} is banned from the server") + + elif command.startswith("UNBAN"): + _, username = command.split(' ', 1) + print(f"{username} is unbanned from the server") + + + elif command == "QUIT": + print("Disconnecting from the server...") + break + else: + print("Invalid command. Please try again.") + + +client.close() diff --git a/FTP Server/server.py b/FTP Server/server.py new file mode 100644 index 0000000..28443fa --- /dev/null +++ b/FTP Server/server.py @@ -0,0 +1,163 @@ +import socket +import threading +import time +import os + +IP = socket.gethostbyname(socket.gethostname()) +PORT = 3030 +FORMAT = 'utf-8' +HEADER = 10 +DISCONNECT_MESSAGE = "disconnect" + +ADDR = (IP, PORT) +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind(ADDR) + +def handle_client(conn, addr, user, pass_user,ban,unban): + print(f"[New connection]: {addr} connected") + connected = True + conn.send("You are successfully connected to the server. Please respond by typing the asked details".encode(FORMAT)) + + while connected: + out = auth(conn, user, pass_user,ban) + if out == 0: + break + + if out==1: + while True: + try: + command = conn.recv(1024).decode(FORMAT) + if command == "LIST": + ls(conn) + elif command.startswith("RETR"): + _, filename = command.split(' ', 1) + + retr(conn,filename.strip()) + elif command.startswith("STOR"): + _, filename = command.split(' ', 1) + store(conn,filename) + elif command == "QUIT": + print(f"Client {addr} disconnected") + conn.close() + connected = False + break + else: + print("Unknown command received.") + break + + except Exception as e: + print(f"Error occurred: {e}") + break + if out==2: + while True: + try: + command = conn.recv(1024).decode(FORMAT) + + if command.startswith("ADDUSER"): + _, username, password = command.split(' ', 2) + adduser(username,password,user,pass_user) + elif command.startswith("DELUSER"): + _, username = command.split(' ', 1) + delete(username,user,pass_user) + elif command.startswith("BAN"): + _, username = command.split(' ', 1) + ban(username,ban) + elif command.startswith("UNBAN"): + _, username = command.split(' ', 1) + unban(username,ban) + elif command == "QUIT": + print(f"Client {addr} disconnected") + conn.close() + connected = False + break + else: + print("Unknown command received.") + except Exception as e: + print(f"Error occurred: {e}") + break + + + +def auth(conn, user, pass_user,ban): + username = conn.recv(1024).decode(FORMAT) + passw = conn.recv(1024).decode(FORMAT) + if (username in user) and (username not in ban): + + if passw == pass_user[user.index(username)]: + if passw=="letmesleep" and username=="admin": + conn.send("11".encode(FORMAT)) + return 2 + else: + conn.send("10".encode(FORMAT)) + return 1 + else: + conn.send("Invalid credentials".encode(FORMAT)) + conn.close() + return 0 + elif ( username not in ban): + conn.send("3".encode(FORMAT)) + res = conn.recv(1024).decode(FORMAT) + if res == "1": + user.append(username) + pass_user.append(passw) + print(user) + print(pass_user) + return 1 + else: + conn.close() + return 0 + else: + conn.send("4".encode(FORMAT)) + +def ls(client): + dirs = os.listdir("./") + client.send("\n".join(dirs).encode()) + +def retr(client,path): + client.send(path.encode()) + time.sleep(0.1) + with open(path,'rb') as f: + data = f.read() + client.sendall(data) + +def store(conn,path): + stream = conn.recv(1024).decode() + + with open(str(path),'w') as f: + f.write(stream) + + conn.send(b"File stored succesfully!") +def ban(username,ban): + ban.append(username) + +def unban(username,ban): + ban.remove(username) + +def delete(username,user,pass_user): + i=user.index(username) + user.remove(username) + pass_user.pop(i) + + +def adduser(username,password,user,pass_user): + user.append(username) + pass_user.append(password) + + + +def start(): + server.listen() + print("Server is listening") + user = ['admin'] + pass_user = ['letmesleep'] + ban=[] + unban=[] + + while True: + conn, addr = server.accept() + thread = threading.Thread(target=handle_client, args=(conn, addr, user, pass_user,ban,unban)) + thread.start() + print(f'[Active clients]: ({threading.active_count() - 1})') + +print("SERVER is starting") +start() diff --git a/OOPS/.gitignore b/OOPS/.gitignore new file mode 100644 index 0000000..f47cb20 --- /dev/null +++ b/OOPS/.gitignore @@ -0,0 +1 @@ +*.out diff --git a/OOPS/main.cpp b/OOPS/main.cpp new file mode 100644 index 0000000..8c3cf31 --- /dev/null +++ b/OOPS/main.cpp @@ -0,0 +1,455 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Date { + int year; + int month; + int day; +}; +typedef struct Date Date; + +struct Transaction { + int acc_no; + string statement; + Date date; +}; + +class BankAccount; +class BankAccountHolder; + +const float SAVINGS_MIN = 100; +const float SAVINGS_TRANS_MAX = 10000; + +int account_no_count = 0; +map bank_accounts; +map account_holders; +vector transactions; + +BankAccount *curr_account; +BankAccountHolder *curr_holder; + +// Terminal Utils +void clrsrc() { cout << "\033[2J\033[1;1H"; } +void press_enter_to_continue() { + cout << "(Press Enter to Continue)" << endl; + cin.clear(); + cin.ignore(std::numeric_limits::max(), '\n'); + cin.get(); +} + +Date get_current_date() { + time_t currentTime = time(nullptr); + + // Convert current time to struct tm + tm *timeinfo = localtime(¤tTime); + + Date curr_date; + // Extract year, month, and day from timeinfo + curr_date.year = timeinfo->tm_year + 1900; // years since 1900 + curr_date.month = timeinfo->tm_mon + 1; // months since January [0, 11] + curr_date.day = timeinfo->tm_mday; // day of the month [1, 31] + return curr_date; +} + +class BankAccount { +public: + int acc_no; + int type; + float interest_rate; + float balance; + Date open_date; + void withdraw(float amount) { + if (amount <= balance) { + balance -= amount; + } else { + throw invalid_argument("Not possible not enough balance"); + } + }; + void deposit(float amount) { balance += amount; }; + void transfer(float amount, int ben_acc_no) { + BankAccount *to_acc = &bank_accounts[ben_acc_no]; + if (amount <= balance) { + balance -= amount; + to_acc->balance += amount; + Transaction trans; + string st = "Ammount " + to_string(amount) + " transferred from " + + to_string(acc_no) + " to " + to_string(ben_acc_no); + trans.statement = st; + trans.date = get_current_date(); + trans.acc_no = acc_no; + transactions.push_back(trans); + } else { + throw invalid_argument("Not possible not enough balance"); + } + }; +}; + +class SavingsAccount : public BankAccount { +public: + float getInterestRate() { return interest_rate; } + +public: + void setInterestRate(float rate) { + if (interest_rate <= 6) { + interest_rate = rate; + } else { + cout << "Interest rate cannot be higher than 6%" << endl; + throw invalid_argument("Interest"); + } + } +}; + +class CheckingAccount : public BankAccount { +public: + void setInterestRate(float interest) { interest_rate = interest; } +}; + +class BankAccountHolder { +public: + string Name; + string Username; + string passwd; + vector bankaccounts; + void changepasswd(string pass) { passwd = pass; } + vector get_accounts() { return bankaccounts; } + +public: + int createAccount(int type, float start_amount, float interest) { + try { + switch (type) { + case 1: + if (start_amount < SAVINGS_MIN) { + throw invalid_argument( + "Opening Savings Account requires at least 100"); + } + SavingsAccount saving_acc; + saving_acc.acc_no = account_no_count + 1; + saving_acc.type = 1; + saving_acc.balance = start_amount; + saving_acc.open_date = get_current_date(); + saving_acc.setInterestRate(interest); + bankaccounts.push_back(saving_acc.acc_no); + bank_accounts[saving_acc.acc_no] = saving_acc; + break; + case 2: + CheckingAccount check_acc; + check_acc.acc_no = account_no_count + 1; + check_acc.type = 2; + check_acc.balance = start_amount; + check_acc.open_date = get_current_date(); + check_acc.setInterestRate(interest); + bankaccounts.push_back(check_acc.acc_no); + bank_accounts[check_acc.acc_no] = check_acc; + break; + } + } catch (...) { + cout << "Cannot create Account" << endl; + press_enter_to_continue(); + } + account_no_count++; + return account_no_count; + } +}; + +class BranchManager { +public: + string username; + string passwd; + void print_holders() { + cout << "Account Name:Account number" << endl; + for (const auto &pair : account_holders) { + for (int acc_no : pair.second.bankaccounts) { + cout << pair.second.Name << ':' << acc_no << endl; + } + } + press_enter_to_continue(); + } + void view_all_statements() { + for (Transaction t : transactions) { + printf("%d/%d/%d: ", t.date.day, t.date.month, t.date.year); + cout << t.statement << endl; + } + press_enter_to_continue(); + } + void fast_forward() { + cout << "Enter time in days" << endl; + int days; + cin >> days; + for (auto it = bank_accounts.begin(); it != bank_accounts.end(); ++it) { + it->second.balance += + (it->second.balance * it->second.interest_rate * days / 365) / 100; + } + cout << "Fast forworded" << endl; + press_enter_to_continue(); + } +}; + +int get_option_bw(int a, int b) { + int opt; + while (true) { + cin >> opt; + if (opt <= b && opt >= a) + break; + printf("Please enter a number between %d and %d\n", a, b); + } + return opt; +} + +int who_you() { + clrsrc(); + cout << "Who are you (Enter the number to select)" << endl; + cout << "1. Bank Account Holder" << endl; + cout << "2. Branch Manager" << endl; + return get_option_bw(1, 2); +} + +void create_new_account_holder() { + string name; + string username; + string passwd; + + try { + clrsrc(); + cout << "Enter your name" << endl; + cin >> name; + cout << "Enter your UserName" << endl; + cin >> username; + cout << "Enter your Password" << endl; + cin >> passwd; + } catch (...) { + throw invalid_argument("Cannot get Account holder info"); + press_enter_to_continue(); + } + try { + BankAccountHolder acc_holder; + acc_holder.Name = name; + acc_holder.passwd = passwd; + acc_holder.Username = username; + account_holders[username] = acc_holder; + } catch (...) { + throw invalid_argument("Cannot add account holder"); + press_enter_to_continue(); + } +} + +void create_account() { + clrsrc(); + cout << "Select Account type" << endl + << "1. Savings Account" << endl + << "2. Checking Account" << endl; + int opt = get_option_bw(1, 2); + try { + cout << "Enter Start Balance" << endl; + float start_bal; + cin >> start_bal; + cout << "Enter Interest Rate" << endl; + float interest; + cin >> interest; + int acc_no = curr_holder->createAccount(opt, start_bal, interest); + cout << "Account Created with acc. no: " << acc_no << endl; + press_enter_to_continue(); + } catch (...) { + throw invalid_argument(""); + } +} +void select_account() { + int acc_no; + try { + cout << "Enter Account no: "; + cin >> acc_no; + curr_account = &bank_accounts[acc_no]; + } catch (...) { + cout << "Cannot Find Bank account" << endl; + press_enter_to_continue(); + } +} +void transfer_money() { + int ben_acc_no; + float amount; + try { + cout << "Enter Account no(Beneficiary): "; + cin >> ben_acc_no; + cout << "Enter Amount to be tranferred: "; + cin >> amount; + } catch (...) { + cout << "Cannot Find Bank account" << endl; + press_enter_to_continue(); + } + try { + curr_account->transfer(amount, ben_acc_no); + } catch (...) { + cout << "Cannot transfer amount" << endl; + press_enter_to_continue(); + } +} +void view_balance() { + int acc_no; + try { + printf("Account Balance is: %f\n", curr_account->balance); + press_enter_to_continue(); + } catch (...) { + cout << "Cannot Find Bank account" << endl; + } +} + +void view_statements() { + for (Transaction t : transactions) { + if (t.acc_no == curr_account->acc_no) { + printf("%d/%d/%d: ", t.date.day, t.date.month, t.date.year); + cout << t.statement << endl; + } + } + press_enter_to_continue(); +} + +void delete_account() { + try { + int acc_no = curr_account->acc_no; + curr_holder->bankaccounts.erase( + std::remove(curr_holder->bankaccounts.begin(), + curr_holder->bankaccounts.end(), acc_no), + curr_holder->bankaccounts.end()); + bank_accounts.erase(acc_no); + cout << "Account Deleted" << endl; + } catch (...) { + cout << "No such Account" << endl; + } + press_enter_to_continue(); +} + +void login_acc_holder() { + string username; + string passwd; + + try { + clrsrc(); + cout << "Enter your UserName" << endl; + cin >> username; + cout << "Enter your Password" << endl; + cin >> passwd; + } catch (...) { + throw invalid_argument("Cannot get Account holder info"); + press_enter_to_continue(); + } + + try { + curr_holder = &account_holders[username]; + if (curr_holder->passwd == passwd) { + cout << "You are logged in" << endl; + } else { + throw invalid_argument("Username or password invalid"); + press_enter_to_continue(); + } + } catch (...) { + throw invalid_argument("Username or password invalid"); + press_enter_to_continue(); + } + + clrsrc(); + cout << "What Operation You want to perform" << endl + << "1. Create Account" << endl + << "2. Transfer Amount" << endl + << "3. View Balance" << endl + << "4. View Statements" << endl + << "5. Delete Account" << endl; + int opt = get_option_bw(1, 5); + switch (opt) { + case 1: + try { + create_account(); + } catch (...) { + cout << "Cannot create account" << endl; + press_enter_to_continue(); + } + break; + case 2: + select_account(); + transfer_money(); + break; + case 3: + select_account(); + view_balance(); + break; + case 4: + select_account(); + view_statements(); + break; + case 5: + select_account(); + delete_account(); + break; + } +} + +int main() { + BranchManager branch_manager; + branch_manager.username = "root"; + branch_manager.passwd = "root"; + bool quit = false; + while (!quit) { + int user_type = who_you(); + if (user_type == 1) { + clrsrc(); + cout << "1. New User" << endl << "2. Existing User" << endl; + int opt = get_option_bw(1, 2); + if (opt == 1) { + try { + create_new_account_holder(); + } catch (...) { + cout << "Cannot new create account holder" << endl; + press_enter_to_continue(); + } + } else { + try { + login_acc_holder(); + } catch (...) { + cout << "Cannot login" << endl; + press_enter_to_continue(); + } + } + } else { + cout << "Enter BranchManager Username" << endl; + string usr; + cin >> usr; + cout << "Enter BranchManager Password" << endl; + string passwd; + cin >> passwd; + if (branch_manager.username == usr && branch_manager.passwd == passwd) { + clrsrc(); + cout << "1. Print statements of all accounts" << endl + << "2. Print Account Holder names" << endl + << "3. Fast Forward" << endl; + int ch = get_option_bw(1, 3); + switch (ch) { + case 1: + branch_manager.view_all_statements(); + break; + case 2: + branch_manager.print_holders(); + break; + case 3: + branch_manager.fast_forward(); + break; + } + + } else { + cout << "Username/Password Invalid" << endl; + } + } + clrsrc(); + cout << "Do you want to quit?" << endl + << "0. No" << endl + << "1. Yes" << endl; + cin >> quit; + } +} diff --git a/images/1.png b/images/1.png new file mode 100644 index 0000000..117e9f6 Binary files /dev/null and b/images/1.png differ diff --git a/images/2.png b/images/2.png new file mode 100644 index 0000000..68fbd43 Binary files /dev/null and b/images/2.png differ diff --git a/images/3.png b/images/3.png new file mode 100644 index 0000000..52a0ebf Binary files /dev/null and b/images/3.png differ diff --git a/images/4.png b/images/4.png new file mode 100644 index 0000000..cc805da Binary files /dev/null and b/images/4.png differ diff --git a/images/5.png b/images/5.png new file mode 100644 index 0000000..eb0bf3d Binary files /dev/null and b/images/5.png differ diff --git a/images/robocup.png b/images/robocup.png new file mode 100644 index 0000000..ce178f0 Binary files /dev/null and b/images/robocup.png differ diff --git a/ros1.md b/ros1.md new file mode 100644 index 0000000..44fb290 --- /dev/null +++ b/ros1.md @@ -0,0 +1,51 @@ +# Setup and building ROS project + +## Ros and Catkin setup +1. Following the setup given in [this](https://github.com/lesaf92/ros_noetic_ubuntu22) repo + +2. Installing mamba + +`curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh" +bash Mambaforge-$(uname)-$(uname -m).sh` + +3. Setup virtual environment with mamba +`mamba create -n ros_env python=3.9 -c conda-forge +mamba activate ros_env +conda config --env --add channels conda-forge +conda config --env --add channels robostack-staging +conda config --env --remove channels defaults +mamba install ros-noetic-desktop-full +mamba install catkin_tools +mamba install rosdep +rosdep init +rosdep update` + + +2. Installing ros-noetic-desktop-full with robostack instead of conda-forge + +3. Creating catkin workspace + +`mkdir -p ~/catkin_ws/src +cd ~/catkin_ws/src +` +4. Building with +`catkin init && catkin build` + +5. Unzip the contents of smb_common in the zip file in the src directory + +6. Sourcing environment with +`source devel/setup.zsh` +on zsh + +7. Running smb_gazebo with roslaunch +`roslaunch smb_gazebo smb_gazebo.launch` + +## RoboCup world running +![alt](images/robocup.png) + +## ScreenShots +![alt](images/1.png) +![alt](images/2.png) +![alt](images/3.png) +![alt](images/4.png) +![alt](images/5.png)