Пример #1
0
//==============================================================================
//==============================================================================
DatagramSocket::DatagramSocket (const int localPortNumber, const bool canBroadcast)
    : portNumber (0),
      handle (-1),
      connected (true),
      allowBroadcast (canBroadcast),
      serverAddress (nullptr)
{
    SocketHelpers::initSockets();

    handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
    bindToPort (localPortNumber);
}
Communication::Communication()
{
    portno = 3000;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

    while(bindToPort() == false);

//    close(newsockfd);
//    close(sockfd);
}
Пример #3
0
DatagramSocket::DatagramSocket (const String& host, const int portNum,
                                const int h, const int localPortNumber)
    : hostName (host),
      portNumber (portNum),
      handle (h),
      connected (true),
      allowBroadcast (false),
      serverAddress (nullptr)
{
    SocketHelpers::initSockets();

    SocketHelpers::resetSocketOptions (h, true, allowBroadcast);
    bindToPort (localPortNumber);
}
Пример #4
0
//==============================================================================
//==============================================================================
DatagramSocket::DatagramSocket (const int localPortNumber, const bool allowBroadcast_)
    : portNumber (0),
      handle (-1),
      connected (true),
      allowBroadcast (allowBroadcast_),
      serverAddress (0)
{
#if JUCE_WINDOWS
    initWin32Sockets();
#endif

    handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
    bindToPort (localPortNumber);
}
Пример #5
0
DatagramSocket::DatagramSocket (const String& hostName_, const int portNumber_,
                                const int handle_, const int localPortNumber)
    : hostName (hostName_),
      portNumber (portNumber_),
      handle (handle_),
      connected (true),
      allowBroadcast (false),
      serverAddress (0)
{
#if JUCE_WINDOWS
    initWin32Sockets();
#endif

    resetSocketOptions (handle_, true, allowBroadcast);
    bindToPort (localPortNumber);
}
void server::start(int port, int constraint)
{
	
	constraintType = constraint;
	
	// create listener socket
	if(catchSignal(SIGINT, server::handleShutdown) == -1)
		error("Can't set the SIGINT handler");
	if(catchSignal(SIGPIPE, server::handleClientClosed) == -1)
		error("Can't set the SIGPIPE pipe handler");
	if(catchSignal(SIGCHLD, SIG_IGN) == -1)
		cerr << "Can't set the SIGCHLD handler" << endl;

	staticListenerD = openListenerSocket();

	bindToPort(staticListenerD, port);
	if(listen(staticListenerD, 30) < 0)
		error("Can't listen");
	

	struct sockaddr_in client_addr; /* the from address of a client*/
	unsigned int address_size = sizeof(client_addr);

	cout << "Start listen" << endl;
	
	while(1){
		int newsockfd = accept(staticListenerD, (struct sockaddr *) &client_addr, (socklen_t*)&address_size);
		//cout << newsockfd << endl;
		
		// no new socket received
		if (newsockfd < 0){ 
			close(newsockfd);
			continue;
		}
		
		
		char clntIP[INET_ADDRSTRLEN];
		inet_ntop(AF_INET, &(client_addr.sin_addr),clntIP,sizeof(clntIP));
		//cout << "<S_IP>:\t" << clntIP << endl;
		//cout << "<S_Port>:\t" << (int) ntohs(client_addr.sin_port) << endl;
		srcIP = clntIP;
		srcPort = to_string((int) ntohs(client_addr.sin_port));
		
		int childpid;
		if ( (childpid = fork()) < 0) cerr << "server: fork error" << endl;
		else if (childpid == 0) { // child process
			
			// process the request
			// ignore SIGCHLD
			signal(SIGCHLD, NULL);
			
			handleClient(newsockfd);
				
			if(staticListenerD) close(staticListenerD); 
			exit(0);
		}else{// parent process
			// do nothing
			close(newsockfd);
		}
	}
	
}
Пример #7
0
int main(int argc, char **argv) {
	struct sigaction handler;
	handler.sa_sigaction = signalHandler;
	handler.sa_flags = SA_SIGINFO;
	sigaction(SIGINT, &handler, NULL);
	sigaction(SIGPIPE, &handler, NULL);
	sigaction(SIGTERM, &handler, NULL);
	string ip = "localhost";
	int port = 5000;
	string tracker_ip = "127.0.0.1";
	int tracker_port = 7500;
	storage_directory = "/home/deepthought/sandbox/p2pvideo/files";
	char *tip, *dir, *cip;
	int bsize;
	getClientConfig(&tip, &tracker_port, &cip, &clients_port, &streaming_port, &dir, &bsize, &cache_size);
	tracker_ip = tip;
	ip = cip;
	storage_directory = dir;
	if (argc == 5) {
		ip = argv[1];
		port = atoi(argv[2]);
		storage_directory = argv[3];
		streaming_port = atoi(argv[4]);
		cout<<"storage directory is "<<storage_directory<<endl;
		clients_port = port;

		/*
		ip += ":";
		ip += argv[2];
		*/
		cout<<ip<<endl;
	}

	/*
	vector<bool> tmap;
	int tnum = 5;
	for (int i = 0; i < tnum; i++) {
		bool val = (i % 2 == 0);
		tmap.push_back(val);
	}

	BlockMap tblock(tmap);
	//tblock.print();

	int size;
	char *data = tblock.serialize(size);
	BlockMap dblock;
	dblock.deserialize(data, size);
	//dblock.print();

	string filename = "testurl1234567890";
	File tfile(filename, tmap);
	//tfile.print();
	data = tfile.serialize(size);
	File dfile;
	dfile.deserialize(data, size);
	//dfile.print();
	Client tclient(ip, port, "/tmp");
	tclient.addFile(tfile);
	//tclient.print();

	cout<<endl<<endl;

	Client dclient;
	int size2;
	char *data2 = tclient.serialize(size2);
	//dclient.deserialize(data2, size2);
	//dclient.print();

	int sockfd = connectToTracker(tracker_ip, tracker_port);
	if (sockfd != -1) {
		cout<<"Connected to tracker"<<endl;
	}
	//registerWithTracker(sockfd, tclient);

	for (int i = 0; i < tnum; i++) {
		tmap[i] = true;
	}
	int file_idx = tclient.getFileIdxByURL(filename);
	//tclient.updateFile(file_idx, tmap);
	//updateOnTracker(sockfd, tclient);
	*/
	pthread_rwlock_init(&client_mutex, NULL);
	Client tmpc(ip, clients_port, storage_directory);
	c = tmpc;
	c.connectToTracker(tracker_ip, tracker_port);
	c.registerWithTracker();
	c.queryTracker();
	c.client_mutex = &client_mutex;
	pthread_t streamer, queryThread, updateThread, statsThread;
	pthread_create(&streamer, NULL, handleStreaming, NULL);
	pthread_create(&queryThread, NULL, handleQuery, NULL);
	pthread_create(&updateThread, NULL, handleUpdate, NULL);
	pthread_create(&statsThread, NULL, handleStats, NULL);
	clients_sockfd = bindToPort(ip, clients_port);
	if(clients_sockfd == -1)
	{
		cout << "Could not create a socket for incoming client connections" << endl;
		abort();
		exit(1);
	}
	pthread_t client_threads[MAX_CLIENT_THREADS];
	int thread_id = 0;
	while(listen(clients_sockfd, BACKLOG) == 0)
	{
		struct sockaddr_storage incoming;
		socklen_t incoming_sz = sizeof(incoming);
		long new_fd = accept(clients_sockfd, (struct sockaddr *)&incoming, &incoming_sz);
		if (new_fd == -1) 
		{
			cout<<"peer accept failed with error: "<<strerror(errno)<<endl;
			exit(1);
		}
		else
		{
			if(pthread_create(&client_threads[thread_id], NULL, fileTransfer, (void *)new_fd)) 
			{
				cout<<"Thread creating failed"<<endl;
			}
			else
			{
				thread_id++;
			}
		}
	}
	while(1);
	return 0;
}
Пример #8
0
void *
handleStreaming(void *param) {
	int sockfd = bindToPort("127.0.0.1", streaming_port);
	while (listen(sockfd, 10) == 0) {
		/* listen and accept incoming connections */
		struct sockaddr_storage incoming;
		socklen_t incoming_sz = sizeof(incoming);
		long new_fd = accept(sockfd, (struct sockaddr *)&incoming, &incoming_sz);
		if (new_fd == -1) {
			cout<<"accept failed with error: "<<strerror(errno)<<endl;
			exit(1);
		} else {
			//cout<<"got connection: "<<new_fd<<endl;
			char header[1024];
			memset(header, 0, 1024);
			int chunk_size = 1000000;
			int filesize = readFile("test.mp4");
			int roundoff = (filesize / chunk_size) * chunk_size;
			//cout<<"roundoff = "<<roundoff<<endl;
			int range_offset = 0;
			FILE *fp = fopen("test.mp4", "r");
			int offset = 0;
			char crlf[5] = "\r\n\r\n";
			while (1) {
				//int bytes_recv = recv(new_fd, header + offset, 1, 0);
				recvSocketData(new_fd, 1, header + offset);
				//cout<<header[offset];
				if (offset >= 3) {
					if (memcmp(header + offset - 3, crlf, 4) == 0) {
						//cout<<"done!"<<endl;
						//cout<<"header:\n"<<header<<endl;
						int start, end;
						getRangeOffset(header, start, end);
						char *requrl = getFileName(header);
						range_offset = start;
						int num_bytes;
						char *filedata = c.getBlock(requrl, range_offset, 0, num_bytes, filesize);
						//cout<<"block size = "<<num_bytes<<endl;
						char response[1024];
						int end_range = range_offset + num_bytes - 1;
						int header_bytes = sprintf(response, "HTTP/1.1 206 Partial Content\r\n"
							"Content-Type: video/webm\r\nContent-Range: bytes "
							"%d-%d/%d\r\nTransfer-Encoding: chunked\r\n\r\n",
							range_offset, end_range, filesize);
						//cout<<response<<endl;
						//cout<<"num bytes = "<<num_bytes<<endl;
						//cout<<"Bytes sent = "<<send(new_fd, response, header_bytes, 0)<<endl;
						sendSocketData(new_fd, header_bytes, response);
						int size_bytes = sprintf(response, "%x\r\n", num_bytes);
						//cout<<response<<endl;
						//cout<<"size bytes = "<<size_bytes<<" num_bytes = "<<num_bytes<<endl;
						//send(new_fd, response, size_bytes, 0);
						sendSocketData(new_fd, size_bytes, response);
						//cout<<"File bytes sent = "<<send(new_fd, filedata, num_bytes, 0)<<endl;
						sendSocketData(new_fd, num_bytes, filedata);
						size_bytes = sprintf(response, "\r\n0\r\n\r\n");
						//cout<<response<<endl;
						//send(new_fd, response, size_bytes, 0);
						sendSocketData(new_fd, size_bytes, response);
						delete[] filedata;
						offset = 0;
						//int newstart = range_offset + (3 + (rand() % 5)) * num_bytes;
						//int newstart = range_offset + (17) * num_bytes;
						int newstart = range_offset + num_bytes;
						if (newstart < filesize) {
							//cout<<"Prefetching random block with offset "<<newstart<<endl;
							struct pargs args;
							sprintf(args.name, "%s", requrl);
							args.offset = newstart;
							args.bsize = num_bytes;
							args.fsize = filesize;
							//filedata = c.getBlock(requrl, newstart, 0, num_bytes, filesize);
							pthread_t tmpt;
							pthread_create(&tmpt, NULL, prefetcher, (void *)&args);
						}
					}
				} 
				offset++;
			}
		}
	}
}
Пример #9
0
bool DatagramSocket::bindToPort (const int port)
{
    return bindToPort (port, String());
}
Пример #10
0
//==============================================================================
bool StreamingSocket::bindToPort (const int port)
{
    return bindToPort (port, String());
}