示例#1
0
void uwsgi_send_subscription(char *udp_address, char *key, size_t keysize, uint8_t modifier1, uint8_t modifier2, uint8_t cmd, char *socket_name, char *sign) {

	if (socket_name == NULL && !uwsgi.sockets)
		return;

	if (!socket_name) {
		socket_name = uwsgi.sockets->name;
	}

	struct uwsgi_buffer *ub =  uwsgi_buffer_new(4096);

	// make space for uwsgi header
	ub->pos = 4;

	if (uwsgi_buffer_append_keyval(ub, "key", 3, key, keysize)) goto end;
	if (uwsgi_buffer_append_keyval(ub, "address", 7, socket_name, strlen(socket_name))) goto end;
	if (uwsgi_buffer_append_keynum(ub, "modifier1", 9, modifier1)) goto end;
	if (uwsgi_buffer_append_keynum(ub, "modifier2", 9, modifier2)) goto end;
	if (uwsgi_buffer_append_keynum(ub, "cores", 5, uwsgi.numproc * uwsgi.cores)) goto end;
	if (uwsgi_buffer_append_keynum(ub, "load", 4, uwsgi.shared->load)) goto end;
	if (uwsgi.auto_weight) {
		if (uwsgi_buffer_append_keynum(ub, "weight", 6, uwsgi.numproc * uwsgi.cores )) goto end;
	}
	else {
		if (uwsgi_buffer_append_keynum(ub, "weight", 6, uwsgi.weight )) goto end;
	}

#ifdef UWSGI_SSL
	if (sign) {
		if (uwsgi_buffer_append_keynum(ub, "unix", 4, (uwsgi_now() + (time_t) cmd) )) goto end;

		unsigned int signature_len = 0;
		char *signature = uwsgi_rsa_sign(sign, ub->buf + 4, ub->pos - 4, &signature_len);
		if (signature && signature_len > 0) {
			if (uwsgi_buffer_append_keyval(ub, "sign", 4, signature, signature_len)) {
				free(signature);
				goto end;
			}
			free(signature);
		}
	}
#endif


	send_udp_message(224, cmd, udp_address, ub->buf, ub->pos - 4);
end:
	uwsgi_buffer_destroy(ub);
}
/*
 * process_private_msg(): deal with all private message
 */
void process_private_msg(char* msg, int cid) {
	/* construct private_msg */
	char init_token[TOKEN_LENTH + 1] = {'\0'};char ip[MAXLINE] = {'\0'};
	char tcp_port[MAXLINE] = {'\0'};

	strncpy(init_token, msg, TOKEN_LENTH);
	//sender infomation
	get_connection_info(cid, ip, tcp_port);
	printf("\t got init_token: %s from %s:%s\n", init_token, ip, tcp_port);
	add_init_token(init_token);

	//check number of init-tokens
	int num_tokens = count_init_token();
	int num_peers = count_current_connections();

	if ( num_tokens == num_peers ) { // received enough tokens
		get_peer_token();
		is_peerToken_determined = 1;
		printf("\t peer token determined: %s\n", self_peer_msg.peer_token);
		send_udp_message();
	}
}