Example #1
0
/*
	0x9000  MESSAGE  (Bi-directional)
    
        When sent from a client, will take that parameter, and pass it (along with the msg ID and 
        name) to the other clients, depending on their FOLLOW or UPDATE modes.

        When sent from server to client, indicates that a message was received at the server, and 
        relayed to the client.  Will also be accompanied by a MSG_ID and a NAME command before the 
        MESSAGE.
        
        The MSG_ID can be used to update the ID of the latest message.  

        If the server is sending multiple messages from the same Name, it will only send Name once, 
        followed by the multiple messages.  Only when messages are being sent from different names, 
        will those entries also be included.
    
*/
void cmdMessage(void *base, risp_length_t length, risp_data_t *data) 
{
	session_t *session = (session_t *) base;
	
	// At every opportunity, we need to make sure that our data is legit.
	assert(base != NULL);
	assert(length >= 0);
	assert(data != NULL);
	
	// the protocol allows a message up to 64k chars in length.  If it is greater than that, then we ignore it.
	assert(length < 65536);
	if (length < 65536) {

		if (checkAuth(session) == true) {

			/// need to relay the message to each client that is connected and in follow mode (other than this 
			/// one, unless it has echo enabled).
			
			maindata_t *maindata = session->maindata;
			assert(maindata);
			
			// store the message in our messages store.
			long long msgID = message_new(maindata, session->data.name, length, (char *) data);
			assert(msgID > 0);

			// when we added the message to the messages array, it copied the message and ensured it was null 
			// termined, so we can now get a pointer to it.
			unsigned char *message = message_get_msg(maindata, msgID);
			unsigned char *name = message_get_name(maindata, msgID);
			
			printf("Session[%d]: Received Message '%s'\n", session->id, message); 

			//   Response for sessions in NOFOLLOW mode.
			//		CMD_LATEST_MSG_ID(integer)
			//
			//   Response for sessions in FOLLOW mode.
			//		CMD_MSG_ID(integer)
			//		CMD_NAME(string)	- If name is supplied.
			//		CMD_MESSAGE(string)

			// the sessions are referenced in an array. 
			assert(maindata);
			assert(maindata->sessions.list);
			assert(maindata->sessions.max > 0);
			for (int i=0; i<maindata->sessions.max; i++) {
				session_t *relay = maindata->sessions.list[i];
				if (relay) {
					if (relay->session_ptr) {
						relay_msg(relay, msgID, name, length, data);
					}
				}
			}
		}
	}
}
Example #2
0
int main() {
  signal(SIGINT, sighandler);

  int socket_id, socket_client;
  
  socket_id = create_server();

  while(1) {
	 
  	printf("<server> listening: %d\n", socket_id);
  	socket_client = accept( socket_id, NULL, NULL );
	sleep(2);
  	printf("<server> connected: %d\n", socket_client );
	
	/* add client to corresponding array */
	int type;  // get type from client
	read(socket_client, &type, sizeof(type));
	if (type == TUTOR_ID) {
		if (num_tutors < MAX_CLIENTS) {
			tutors[num_tutors][0] = socket_client;
		//	tutors[num_tutors][1] = 1;
			num_tutors++;
			printf("<server> added tutor: %d [%d tutors; %d tutees]\n", socket_client, num_tutors, num_tutees);
		}
		else {
			char msg[MSG_SIZE];
			sprintf(msg, "XSorry, too many clients. Come back later.\n");
			write(socket_client, msg, sizeof(msg));
			close(socket_client);
		}
	}
	else if (type == TUTEE_ID) {
		if (num_tutees < MAX_CLIENTS) {
			tutees[num_tutees][0] = socket_client;
			tutees[num_tutees][1] = 1;
			// insert tutee subject requested
			num_tutees++;
			printf("<server> added tutee: %d [%d tutors; %d tutees]\n", socket_client, num_tutors, num_tutees);
		}
		else {
			char msg[MSG_SIZE];
			sprintf(msg, "XSorry, too many clients. Come back later.\n");
			write(socket_client, msg, sizeof(msg));
			close(socket_client);
		}
	}
	else {
		printf("ERROR - type argument\n");
	}

	/* start chat from tutee side */
	if (type == TUTEE_ID) {	
		int tutee_ind = num_tutees-1;
		int tutor_ind = find_tutor_simp(tutee_ind);
		if (tutor_ind != -1) {
			int status;
			printf("<server> paired tutor #%d and tutee #%d\n", tutor_ind, tutee_ind);
			tutors[tutor_ind][1] = 1;  // set tutor to unavailable
			int pid = fork();  // subserver 
    		if (pid == 0) {
				char msg[] = "You have been connected!\n";
				write(tutees[tutee_ind][0], msg, sizeof(msg));
				int run = 1;	
				while(run) {
					int e = relay_msg(tutees[tutee_ind][0], tutors[tutor_ind][0]);
					if (e == -1) {
						run = 0;
					}
					relay_msg(tutors[tutor_ind][0], tutees[tutee_ind][0]);
				}
				close(0);
			}
			/**
		   	else {
				waitpid(pid, &status, 0);  // wait for chat to finish
				printf("<server> closing chat between tutor #%d and tutee #%d\n", tutor_ind, tutee_ind);
				close_chat(tutor_ind, tutee_ind);
			}
			*/
		}
		else {
			char msg[] = "No tutors available at this time. Please come back later!\n";
			write(tutees[tutee_ind][0], msg, sizeof(msg));
			close(tutees[tutee_ind][0]);
		}
	} else {
		// shift array down, adjust
        // close(socket_client);
    }
  }

  return 0;
}