Exemplo n.º 1
0
//starting a new file server thread.
void start_server(int port){
    //printf("Port num=%d\n", port);
    int server_fds = -1;
    int myID=-1;
    
    int client_socket_id;
    sockaddr client_addr;
    socklen_t size = sizeof(sockaddr);
    client_info *pci=NULL;
    
    pthread_t thread;
    
    get_serverID_msg msg;
    char recv_data[MAXSOCKETSIZE];
    
    
//First, register with Metadata manager (and retrieve its ServerID (e.g., 3)).
    meta_fds=socket_connect(META_ADDR, META_PORT);
    
    if(meta_fds<0){
        printf("Cannot connect with metadata manager.\n");
        exit(-1);
    }

    msg.event_type=EVENT_GET_SERVID;
    msg.server_port=port;
    
    send(meta_fds,(char *)(&msg), sizeof(msg), 0);
    recv(meta_fds, recv_data, MAXSOCKETSIZE, 0);
    
    if(((socket_msg *)(&recv_data))->event_type==EVENT_ACK_SERVID){
        myID = ((ack_servid_msg *)(&recv_data))->ServerID;
        printf("ServID=%d On line.\n", myID);
    }else{
        printf("Cannot register with metadata manager on ServID. \n");
        close(meta_fds);
        exit(-1);
    }
    

//Then, we listen to client's requests.
    if((server_fds=socket_bind_listen(SERVER_ADDR, port))<0){
        printf("socket_bind_listen() fail. %d \n", server_fds);
        exit(-1);
    }
    
    while(1){
        if((client_socket_id=accept(server_fds, (sockaddr*)(&client_addr), &size))<0){
            printf("accept() error.\n");
        }else{
            pci=(client_info *)malloc(sizeof(client_info));
            pci->client_socket_fds=client_socket_id;
            pci->ServerID=myID;
            memcpy(&(pci->client_addr), &client_addr, size);
            pthread_create(&thread, NULL, (void *(*)(void *))server_process,(void *)pci);
        }
    }
}
Exemplo n.º 2
0
static bool
httpd_output_open(void *data, struct audio_format *audio_format,
		  GError **error)
{
	struct httpd_output *httpd = data;
	bool success;
	GIOChannel *channel;

	g_mutex_lock(httpd->mutex);

	/* create and set up listener socket */

	httpd->fd = socket_bind_listen(PF_INET, SOCK_STREAM, 0,
				       (struct sockaddr *)&httpd->address,
				       httpd->address_size,
				       16, error);
	if (httpd->fd < 0) {
		g_mutex_unlock(httpd->mutex);
		return false;
	}

	channel = g_io_channel_unix_new(httpd->fd);
	httpd->source_id = g_io_add_watch(channel, G_IO_IN,
					  httpd_listen_in_event, httpd);
	g_io_channel_unref(channel);

	/* open the encoder */

	success = httpd_output_encoder_open(httpd, audio_format, error);
	if (!success) {
		g_source_remove(httpd->source_id);
		close(httpd->fd);
		g_mutex_unlock(httpd->mutex);
		return false;
	}

	/* initialize other attributes */

	httpd->clients = NULL;
	httpd->timer = timer_new(audio_format);

	g_mutex_unlock(httpd->mutex);
	return true;
}
Exemplo n.º 3
0
bool
server_socket_open(struct server_socket *ss, GError **error_r)
{
	struct one_socket *good = NULL, *bad = NULL;
	GError *last_error = NULL;

	for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) {
		assert(s->serial > 0);
		assert(good == NULL || s->serial >= good->serial);
		assert(s->fd < 0);

		if (bad != NULL && s->serial != bad->serial) {
			server_socket_close(ss);
			g_propagate_error(error_r, last_error);
			return false;
		}

		GError *error = NULL;
		s->fd = socket_bind_listen(s->address.sa_family, SOCK_STREAM, 0,
					   &s->address, s->address_length, 5,
					   &error);
		if (s->fd < 0) {
			if (good != NULL && good->serial == s->serial) {
				char *address_string = one_socket_to_string(s);
				char *good_string = one_socket_to_string(good);
				g_warning("bind to '%s' failed: %s "
					  "(continuing anyway, because "
					  "binding to '%s' succeeded)",
					  address_string, error->message,
					  good_string);
				g_free(address_string);
				g_free(good_string);
				g_error_free(error);
			} else if (bad == NULL) {
				bad = s;

				char *address_string = one_socket_to_string(s);
				g_propagate_prefixed_error(&last_error, error,
							   "Failed to bind to '%s': ",
							   address_string);
				g_free(address_string);
			} else
				g_error_free(error);
			continue;
		}

		/* allow everybody to connect */

		if (s->path != NULL)
			chmod(s->path, 0666);

		/* register in the GLib main loop */

		GIOChannel *channel = g_io_channel_unix_new(s->fd);
		s->source_id = g_io_add_watch(channel, G_IO_IN,
					      server_socket_in_event, s);
		g_io_channel_unref(channel);

		/* mark this socket as "good", and clear previous
		   errors */

		good = s;

		if (bad != NULL) {
			bad = NULL;
			g_error_free(last_error);
			last_error = NULL;
		}
	}

	if (bad != NULL) {
		server_socket_close(ss);
		g_propagate_error(error_r, last_error);
		return false;
	}

	return true;
}