#include <iostream>
#include <mpi.h>
using namespace std;

int main(int argc, char** argv){
	MPI_Init(&argc, &argv);
	int world_size;
	MPI_Comm_size(MPI_COMM_WORLD, &world_size);
	int world_rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
	if(world_size<2){
		if(world_rank==0){
			cerr<<"ERROR: This program requires at least 2 nodes! Use -np 2"<<endl;
		}
		MPI_Finalize();
		return 1;
	}
	if(world_rank==0){
		int secret_code=998877;
		cout<<"[NODE 0] I am the Boss. I generated the secret code: "<<secret_code<<endl;
		cout<<"[NODE 0] Transmitting code across the network to Node 1..."<<endl;
		MPI_Send(&secret_code, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
		cout<<"[NODE 0] Transmission complete! Boss is signing off."<<endl;
	}
	else if(world_rank==1){
		int received_code;
		cout<<"  ->[NODE 1] I am awake. Waiting for network transmission from Boss..."<<endl;
		MPI_Recv(&received_code, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
		cout<<"  ->[NODE 1] Message received! The secret code is: "<<received_code<<endl;
		cout<<"  ->[NODE 1] Worker is signing off."<<endl;
	}
	MPI_Finalize();
	return 0;
}
