Commit ec0d6f86 authored by Sanjna's avatar Sanjna

outlab9

parents
File added
.PHONY: all peer server
all: peer server
peer: peer.cpp
g++ peer.cpp -o peer
server: server.o
g++ server.o -o server
server.o: server.cpp peerinfo.hpp
g++ -c server.cpp
clean:
rm -rf *.o peer server
peer.cpp: - It is a menu driven program for peers which can publish the files to central server which
it wants to share to other peers & and it can search for files through central server get
that file from respective peer.
IMP FUNCTIONS
startPServer() - It will start the server for peer in background to serve incoming request
of fellow peers
connToServer() - It will establish a connection with central server
displayMenu() - It is used to display Menu for intraction
server.cpp: - It is a program for central server which will establish a server for all the peers to connect
and fetch iformation about others.
IMP FUNCTIONS
startServer() - It is used to start and initialise the server
acceptPeer() - Accepts incoming peers request & stores its information
peerinfo.hpp: - It conatins a user defined class which stores all the necessary detials of peer who has
established connectiom with server
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define S_PORT 4500 // Fixed port no. for SERVER
#define BUFFER_SIZE 1024
using namespace std;
int startPServer(int pport, int &psoc); // To start peers background server
int connToServer(int &psocket, string serIP, int port); // Establish connection with diffferent server
int checkNdie(int status); // Check for return values of varous function
int displayMenu();
int mySend(int sSoc, string &input);
char buffer[BUFFER_SIZE]; // Buffer to store sent & recieved data
int main(int argc, char const *argv[])
{
int stat; // Temp variable used to store return value of functions
int peersocket; // Socket in which peers server is listening
int p2sSocket; // Socket for communication with central server
int fileDes; // Descriptor for file
string file_name;
string op;
if( argc != 4)
{
cout<<"Run as: ./peer <Server Port> <Server IP> <Peer Port>\n";
return 1;
}
startPServer(atoi(argv[3]), peersocket);
cout<<"Connecting to Central server...\n";
connToServer(p2sSocket, argv[2], atoi(argv[1])); // Connects to central server
snprintf(buffer, BUFFER_SIZE, "%s", argv[3]); // Sends peer's server port no. to central server
send(p2sSocket, buffer, BUFFER_SIZE, 0);
int process = fork();
if( process > 0)
{
do
{
displayMenu();
mySend(p2sSocket, op);
if( !strncmp(op.c_str(), "1", 1) )
{
cout<<"Publish File : ";
mySend(p2sSocket, file_name); // Publishes the file names to server
perror("Publish");
}
else if( !strncmp(op.c_str(), "2", 1) )
{
cout<<"Search File : ";
mySend(p2sSocket, file_name);
recv(p2sSocket, buffer, BUFFER_SIZE, 0);
if( strncmp(buffer, "NF", 2) )
{
string p2pIP = string(buffer);
cout<<"File is at: "<<p2pIP;
recv(p2sSocket, buffer, BUFFER_SIZE, 0);
string p2pPORT = string(buffer);
cout<<"::"<<p2pPORT<<"\n";
int p2pSocket;
cout<<"Connecting to peer : "<<p2pIP<<":"<<p2pPORT<<"\n";
connToServer(p2pSocket, p2pIP, atoi(p2pPORT.c_str())); // Connects to other peer for download
cout<<"Connection Established...\n";
snprintf(buffer, BUFFER_SIZE, "%s", file_name.c_str());
send(p2pSocket, buffer, BUFFER_SIZE, 0);
snprintf(buffer, BUFFER_SIZE, "p2p-files/%s", file_name.c_str());
fileDes = open(buffer, O_RDWR | O_CREAT | O_APPEND ); // Open the file to write
perror("File open");
int Bsize;
int fsize=0;
cout<<"Downloading "<<file_name<<" ...\n";
while( (Bsize = read(p2pSocket, buffer, BUFFER_SIZE)) > 0 ) // Read from peer & write in file
{
fsize += Bsize;
//perror("Read");
write(fileDes, buffer, Bsize);
}
cout<<"Size = "<<fsize;
close(fileDes);
cout<<" Downloaded successfully "<<"\n";
close(p2pSocket);
}
else
cout<<"File not Found\n";
}
}while( strncmp(op.c_str(), "3", 1) );
}
else
{
while(1)
{
struct sockaddr_in in_peer; // For storing incoming peers address
socklen_t addrlen;
int in_peerFD;
char data[BUFFER_SIZE];
addrlen = sizeof(in_peer);
in_peerFD = accept(peersocket, (struct sockaddr *)&in_peer, &addrlen);
recv(in_peerFD, buffer, BUFFER_SIZE, 0);
snprintf(data, BUFFER_SIZE, "p2p-files/%s", buffer);
fileDes = open(data, O_RDONLY); // Open File From Where to read
// perror("File open");
int Bsize;
while( (Bsize = read(fileDes, buffer, BUFFER_SIZE)) > 0 )
{
write(in_peerFD, buffer, Bsize);
}
close(fileDes);
close(in_peerFD);
}
}
close(peersocket);
close(p2sSocket);
return 0;
}
int startPServer(int pport, int &psoc)
{
cout<<"Starting peer's server...\n";
struct sockaddr_in peer_server;
int stat = -1; // Temp variable used to store return value of functions
psoc = socket(AF_INET, SOCK_STREAM, 0);
/* Checks for succesfull creation of socket */
perror("Peer's Socket");
checkNdie(psoc);
bzero(&peer_server, sizeof(peer_server));
peer_server.sin_family = AF_INET;
peer_server.sin_addr.s_addr = htonl(INADDR_ANY);
peer_server.sin_port = htons(pport);
int optval = 1;
setsockopt(psoc, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
stat = bind(psoc, (struct sockaddr *)&peer_server, sizeof(peer_server));
/* Checks for binding of port & address */
perror("Bind");
checkNdie(stat);
stat = listen(psoc, 5);
/* Checks for listen error */
perror("Listen");
checkNdie(stat);
return 0;
}
int mySend(int sSoc, string &input)
{
getline(cin, input);
snprintf(buffer, BUFFER_SIZE, "%s", input.c_str());
send(sSoc, buffer, BUFFER_SIZE, 0);
return 0;
}
int connToServer(int &psocket, string serIP, int port)
{
int stat;
struct sockaddr_in cen_server;
char serip[50];
strcpy(serip, serIP.c_str());
psocket = socket(AF_INET, SOCK_STREAM, 0);
/* Checks for succesfull creation of socket */
perror("Socket");
checkNdie(psocket);
cen_server.sin_family = AF_INET;
cen_server.sin_addr.s_addr = inet_addr(serip);
cen_server.sin_port = htons(port);
stat = connect(psocket, (struct sockaddr *)&cen_server, sizeof(cen_server));
/* Checks For successfull connection */
perror("Connect");
checkNdie(stat);
}
int displayMenu()
{
cout<<"\n******Peer Main Menu******";
cout<<"\n\t1. Publish";
cout<<"\n\t2. Search & Fetch";
cout<<"\n\t3. Exit";
cout<<"\nSelect an option : ";
return 0;
}
int checkNdie(int status)
{
if( status < 0 )
exit(1);
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class peerInfo
{
private:
int socID; // Socket in which peer is communicating with server
int port; // Port in which peer's server is running
string IP; // IP address of Peer
vector<string> files; // List of File peers have
public:
peerInfo(int socketFD, string ip, int p)
{
socID = socketFD;
IP = ip;
port = p;
}
string getIP(){return IP;}
int getPort(){return port;}
int getsocID(){return socID;}
string getFile(int index){return files[index];}
void addFile(string file) // Add new file to the list
{
files.push_back(file);
}
int searchFile(string sFile) // Search for particular file in peers list of files
{
for (int i = 0; i < files.size(); ++i)
if ( files[i] == sFile )
return 1;
return 0;
}
};
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <arpa/inet.h>
#include "peerinfo.hpp"
#define S_PORT 4500 // Fixed port no. for SERVER
#define BUFFER_SIZE 1024
using namespace std;
int startServer(int &serverSocket, int sPort); // Function used to Initialise and start the server
int acceptPeer(int soc); // Accept new connection and returns FD for that client
int checkNdie(int status); // Check for return values of varous function
vector<peerInfo> peers; // Saves the Informations of all peers which are connected
char buffer[BUFFER_SIZE];
char data[BUFFER_SIZE];
int main(int argc, char const *argv[])
{
fd_set masterFDS;
fd_set readFDS;
int serverSocket=0;
int maxFD;
int stat; // Temp variable used to store return value of functions
if( argc != 2)
{
cout<<"Run as: ./server <Server Port>\n";
return 1;
}
startServer(serverSocket, atoi(argv[1]));
cout<<"Server Socket : "<<serverSocket<<"\n";
FD_ZERO(&masterFDS);
FD_ZERO(&readFDS);
FD_SET(serverSocket,&masterFDS);
maxFD = serverSocket;
while(1)
{
fflush(stdout);
readFDS = masterFDS;
stat = select(maxFD+1, &readFDS, NULL, NULL, NULL);
checkNdie(stat);
for (int i = 0; i <= maxFD; ++i) // Itterate through every fd to check activity
{
if ( FD_ISSET(i, &readFDS) )
{
if ( i == serverSocket) // For new connection if activity is in server's socket
{
int peerFD = acceptPeer(serverSocket); // Accept new Peer connection
if( peerFD > 0) // only if valid FD is returned
{
FD_SET(peerFD, &masterFDS);
if ( peerFD > maxFD )
{
maxFD = peerFD;
}
}
}
else // Handles incoming data from peers
{
recv(i, data, BUFFER_SIZE, 0);
if( !strncmp(data, "1", 1) ) // IF peer want to publish, add file in peer's info
{
recv(i, buffer, BUFFER_SIZE, 0);
for (int j = 0; j < peers.size(); j++)
{
if ( peers[j].getsocID() == i)
{
peers[j].addFile(buffer);
}
}
}
else if( !strncmp(data, "2", 1) ) // IF peer wants to search for a file
{
recv(i, buffer, BUFFER_SIZE, 0);
int found = 0;
for (int j = 0; j < peers.size(); j++)
{
if( peers[j].searchFile( string(buffer) ))
{
found = 1;
snprintf(buffer, BUFFER_SIZE, "%s", peers[j].getIP().c_str());
send(i, buffer, BUFFER_SIZE, 0);
snprintf(buffer, BUFFER_SIZE, "%d", peers[j].getPort());
send(i, buffer, BUFFER_SIZE, 0);
break;
}
}
if ( !found )
{
snprintf(buffer, BUFFER_SIZE, "NF");
send(i, buffer, BUFFER_SIZE, 0);
}
}
else if( !strncmp(data, "3", 1) ) // Show all files of that user
{
for (int j = 0; j < peers.size(); j++)
{
if ( peers[j].getsocID() == i)
{
cout<<"Peer Disconnected : "<<peers[j].getIP()<<"::"<<peers[j].getPort();
cout<<" socket : "<<peers[j].getsocID()<<"\n";
peers.erase( peers.begin()+j );
close(i);
FD_CLR(i, &masterFDS);
}
}
}
// else if( !strncmp(data, "0", 1) ) // Show all files of that user
// {
// for (int j = 0; j < peers.size(); j++)
// {
// if ( peers[j].getsocID() == i)
// {
// cout<<"Files of "<<peers[j].getIP()<<"::"<<peers[j].getPort()<<"\n";
// peers[j].showFiles();
// }
// }
// }
}
}
}
}
close(serverSocket);
return 0;
}
int startServer(int &serverSocket, int sPort)
{
int stat = -1; // Temp variable used to store return value of functions
int serverPort = sPort;
struct sockaddr_in cen_server;
int i = 0;
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
/* Checks for succesfull creation of socket */
perror("Socket");
checkNdie(serverSocket);
bzero(&cen_server, sizeof(cen_server));
cen_server.sin_family = AF_INET;
cen_server.sin_addr.s_addr = htonl(INADDR_ANY);
cen_server.sin_port = htons(serverPort);
int optval = 1;
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
stat = bind(serverSocket, (struct sockaddr *)&cen_server, sizeof(cen_server));
/* Checks for binding of port & address */
perror("Bind");
checkNdie(stat);
stat = listen(serverSocket, 5);
/* Checks for listen error */
perror("Listen");
checkNdie(stat);
return 0;
}
int acceptPeer(int soc)
{
struct sockaddr_in peer;
socklen_t addrlen;
int peerFD;
addrlen = sizeof(peer);
peerFD = accept(soc, (struct sockaddr *)&peer, &addrlen);
if( peerFD == -1 )
perror("Accept");
else
{
recv(peerFD, buffer, BUFFER_SIZE, 0); // Recieves port no. from peer in which it is working
/* Add peer's info in vector of all peers */
peers.push_back(peerInfo(peerFD, inet_ntoa(peer.sin_addr), atoi(buffer)));
cout<<"New peer connected : "<<inet_ntoa(peer.sin_addr)<<"::"<<buffer<<" socket : "<<peerFD<<"\n";
}
return peerFD;
}
int checkNdie(int status)
{
if( status < 0 )
exit(1);
return 0;
}
\ No newline at end of file
CC=g++
CFLAGS=-c -Wall
all: hello
hello: main.o factorial.o hello.o
$(CC) main.o factorial.o hello.o -o hello
main.o: main.cpp
$(CC) $(CFLAGS) main.cpp
factorial.o: factorial.cpp
$(CC) $(CFLAGS) factorial.cpp
hello.o: hello.cpp
$(CC) $(CFLAGS) hello.cpp
clean:
rm -rf *.o hello
#include "functions.h"
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
}
void print_hello();
int factorial(int n);
#include <iostream>
using namespace std;
#include "functions.h"
void print_hello(){
cout << "Hello World!";
}
#include <iostream>
using namespace std;
#include "functions.h"
int main(){
print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}
OBJECTS=main.o hello.o factorial.o
hello: $(OBJECTS)
$(CXX) $(OBJECTS) -o hello
main.o: main.cpp functions.h
$(CXX) -c main.cpp
hello.o: hello.cpp functions.h
$(CXX) -c hello.cpp
factorial.o: $(FACTM)
$(CXX) -c factorial.cpp
clean:
rm -rf *.o hello
#include "functions.h"
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
}
void print_hello();
int factorial(int n);
#include <iostream>
#include "functions.h"
using namespace std;
void print_hello(){
cout << "Hello World!";
}
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}
hellomake: hellomake.o hellofunc.o
gcc $^ -o $@
hellomake.o: hellomake.c hellomake.h
gcc -c $<
hellofunc.o: hellofunc.c hellomake.h
gcc -c $<
clean:
rm -rf *.o hellomake
#include <stdio.h>
#include "hellomake.h"
void myPrintHelloMake(void) {
printf("Hello makefiles!\n");
return;
}
#include "hellomake.h"
int main() {
// call a function in another file
myPrintHelloMake();
return(0);
}
/*
example include file
*/
void myPrintHelloMake(void);
\ No newline at end of file
SUBDIRS = mt_client mt_server
SERVER= ./mt_server
CLIENT= ./mt_client
path = $(shell pwd)
port = 8080
export path port
.PHONY: all clean run_server run_client
all clean:
for dir in $(SUBDIRS); do\
$(MAKE) -C $$dir -f Makefile $@; \
done
run_server:
$(MAKE) -C $(SERVER) run
run_client:
$(MAKE) -C $(CLIENT) run
# server:
# $(MAKE) -C $(SERVER)
# client:
# $(MAKE) -C $(CLIENT)
# clean: $(CLEAN_SERV) $(CLEAN_CL)
# $(CLEAN_SERV):
# $(MAKE) -C $(SERVER) clean
# $(CLEAN_CL):
# $(MAKE) -C $(CLIENT) clean
\ No newline at end of file
CLPATH= ./mt_client/
TLPATH= $(path)
HDEP=cl_task.h cl_utils.h
CFLAGS=-c -g -pthread -Wall
OBJECT = build
NUM_THREADS = 10
HOST_NAME = localhost
PATH_TO_EXEC = $(path)/client.out
all: $(OBJECT) client
$(OBJECT):
if [ ! -d ./$(OBJECT) ];then \
mkdir -p $(OBJECT); \
fi
client: $(TLPATH)/client.out
$(TLPATH)/client.out: $(OBJECT)/mt_client.o $(OBJECT)/cl_task.o $(OBJECT)/cl_utils.o
$(CC) -pthread $^ -o $@
$(OBJECT)/mt_client.o: mt_client.c $(HDEP)
$(CC) $(CFLAGS) $< -o $@
$(OBJECT)/cl_task.o: cl_task.c $(HDEP)
$(CC) $(CFLAGS) $< -o $@
$(OBJECT)/cl_utils.o: cl_utils.c cl_utils.h
$(CC) $(CFLAGS) $< -o $@
run:
$(PATH_TO_EXEC) $(HOST_NAME) $(port) $(NUM_THREADS)
clean:
@rm -r $(PATH_TO_EXEC) $(OBJECT)/*.o $(OBJECT)
SERVPATH=./mt_server/
TLPATH= $(path)
HDEP=sv_task.h sv_utils.h
CFLAGS=-c -g -pthread -Wextra
OBJECT = build
PATH_TO_EXEC = $(path)/server.out
all: $(OBJECT) server
$(OBJECT):
if [ ! -d ./$(OBJECT) ];then \
mkdir -p $(OBJECT); \
fi
server: $(TLPATH)/server.out
$(TLPATH)/server.out: $(OBJECT)/mt_server.o $(OBJECT)/sv_task.o $(OBJECT)/sv_utils.o
$(CC) -pthread $^ -o $@
$(OBJECT)/mt_server.o: mt_server.c $(HDEP)
$(CC) $(CFLAGS) $< -o $@
$(OBJECT)/sv_task.o: sv_task.c $(HDEP)
$(CC) $(CFLAGS) $< -o $@
$(OBJECT)/sv_utils.o: sv_utils.c sv_utils.h
$(CC) $(CFLAGS) $< -o $@
run:
$(PATH_TO_EXEC) $(port)
clean:
@rm -rf $(OBJECT)/*.o $(PATH_TO_EXEC) $(OBJECT)
CSE GitLab username: sanjnamohan
Link to GitLab repository: https://git.cse.iitb.ac.in/sanjnamohan/outLab9
Contributions:
Sanjna Mohan (20305R006): Problem4
Manjusree M P (203059007): Problem3,Problem4
Pooja Gayakwad (203050076): Problem1
Snehlata Yadav (203050075): Problem2
References:
https://www.gnu.org/software/make/manual/make.pdf
https://www.oreilly.com/library/view/managing-projects-with/0596006101/ch06.html
https://www.tutorialspoint.com/makefile/makefile_features.htm
https://www.cmcrossroads.com/article/making-directories-gnu-make
https://www.gnu.org/software/make/manual/html_node/MAKE-Variable.html#MAKE-Variable
https://www.linuxquestions.org/questions/programming-9/makefiles-and-subdirectories-794088/
https://stackoverflow.com/questions/5178125/how-to-place-object-files-in-separate-subdirectory/27794283
https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_16.html#:~:text=Compiling%20for%20debugging&text=This%20debugging%20information%20is%20stored,when%20you%20run%20the%20compiler.
https://stackoverflow.com/questions/12697012/passing-variables-from-include-directive-to-sub-make
https://stackoverflow.com/questions/28890634/how-to-get-a-shell-environment-variable-in-a-makefile
https://tack.ch/gnu/make-3.82/make_43.html
https://gist.github.com/linse/87f3b44d59a9d298309c
http://lackof.org/taggart/hacking/make-example/
https://www.cs.bu.edu/teaching/cpp/writing-makefiles/
https://www.tutorialspoint.com/makefile/makefile_macros.htm
https://stackoverflow.com/questions/3220277/what-do-the-makefile-symbols-and-mean
https://stackoverflow.com/questions/5191436/creating-two-separate-executables-from-a-makefile-g
https://www.tutorialspoint.com/makefile/index.htm
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment