Скачиваний:
5
Добавлен:
01.05.2014
Размер:
2.74 Кб
Скачать
/*************************************************************************
 * OPUS MULTICLASTER EMULATOR v.1.0.																		 *
 * File: ring.cpp   																										 *
 * Date: 15.05.04																												 *
 * Author: Sergey Ivochkin (9351)																				 *
 * E-mail: sivsivsiv@mail.ru																						 *
 * Description: ring test.        																			 *
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "sys/root.h"
#include "message.h"

int main(void) {

	char *FName = "main";
	int	MyProcID;
	int nProcs;
	LinkCB_t *PrevLink, *NextLink;
	int error;

	MyProcID = GET_ROOT()->ProcRoot->MyProcID;
	nProcs=GET_ROOT()->ProcRoot->nProcs;

	if(nProcs<2) {
		PrintProcMessage(MyProcID, "Your network is too small.");
		AbortServer(1);
	}

	//set up links
	if(MyProcID==0) {
		NextLink=MakeLink((MyProcID+1)%nProcs, 1234, &error);
		if(NextLink==NULL) {
			LogError(EC_FATAL, FName, "Failed to make link to next phocessor, error code %d.", error);
			AbortServer(1);
		}

		PrevLink=GetLink((MyProcID-1+nProcs) % nProcs, 1234, &error);
		if(PrevLink==NULL) {
			LogError(EC_FATAL, FName, "Failed to get link from previous processor, error code %d.", error);
			AbortServer(1);
		}
	}
	else {
		PrevLink=GetLink((MyProcID-1+nProcs)%nProcs, 1234, &error);
		if(PrevLink==NULL) {
				LogError(EC_FATAL, FName, "FAiled to get link from previous processor, error code %d.", error);
				AbortServer(1);
		}
		NextLink=MakeLink((MyProcID+1)%nProcs, 1234, &error);
		if(NextLink==NULL) {
				LogError(EC_FATAL, FName, "FAiled to make link to next processor, error code %d.", error);
				AbortServer(1);
		}
	}

	//process message
	{
		if(MyProcID==0) {
			int length;
			char original[1024], result[1024];
			memset(original, 0, 1024);
			memset(result, 0, 1024);

			printf("Input message text : \n");
			scanf("%s", original);
			length=strlen(original);

			//Send
			{
				PrintProcMessage(MyProcID, "Sending message: %s.", original);
				SendLink(NextLink, original, length);
			}

			//Receive
			{
				length = RecvLink(PrevLink, result, 1024);
				PrintProcMessage(MyProcID, "Message received: %s.", result);
			}
			result[length]=(char)0;
			PrintProcMessage(MyProcID, "Received from ring: %s", result);
		}
		else {
			int length;
			char buffer[1024];
			memset(buffer, 0, 1024);

			length=RecvLink(PrevLink, buffer, 1024);
			PrintProcMessage(MyProcID, "Message received: %s.", buffer);
			PrintProcMessage(MyProcID, "Sending message: %s.", buffer);
			SendLink(NextLink, buffer, length);
		}
	}
	exit(0);
}
Соседние файлы в папке Ring_test
  • #
    01.05.20142.74 Кб5ring.cpp
  • #
    01.05.2014918 б4Ring_test.sln
  • #
    01.05.20143.18 Кб4Ring_test.vcproj