Ako sam dobro razumeo problem u primes.x jedino sto bi trebalo da se promeni jeste da dodas u okviru strukture prime_request jos jednu promenljivu a to je ta promenljiva "pos" i izmenis vrednost funkcije FIND_PRIMES, znaci sve to bi trebalo da izgleda ovako:
Code:
/* Max size of array of primes we can return */
const MAXPRIMES = 1024;
/* Argument: a min-max range */
struct prime_request {
int min;
int max;
int pos;
};
/*OVU STRUKUTURU SAM OSTAVIO JER SE KORISTI U PRIME_SERVER.C
BICE TI JASNO KASNIJE ZASTO JE OSTALA LAKSE JE KADA JE ONA TU MADA NAM
I NIJE NESTO MNOGO POTREBNA */
/* Return value: a variable length array */
struct prime_result {
int array<MAXPRIMES>;
};
/* Define program */
program PRIME_PROG {
version PRIME_VERS {
int FIND_PRIMES(prime_request) = 2; /* proc num */
} = 1; /* vers num */
} = 0x2a3b4c5d; /* prog num */
e sada moramo malo da izmenimo client da uzme u obzir i argument POS i da malo izmenimo ispis, nesto otprilike ovako(komentare cu da pisem velikim slovima da bi bili uocljivi):
Code:
#include "primes.h"
int main(int argc, char *argv[])
{
CLIENT *cl;
prime_request request;
/*OVDE SMO PROMENILI TIP result JER SADA VRACAMO SAMO JEDAN BROJ */
int result;
int i;
/* SADA IMAMO 5 ARGUMENTA A NE 4*/
if (argc != 5) {
/*OVDE SAM UBACIO DA JE PRAVILNA UPOTREBA SA POS NA KRAJU*/
fprintf(stderr, "usage: %s host min max pos\n", argv[0]);
exit(1);
}
/* Connect to the client on the host specified in argv[1] */
cl = clnt_create(argv[1], PRIME_PROG, PRIME_VERS, "tcp");
if (cl == NULL) {
clnt_pcreateerror(argv[1]);
exit(2);
}
/* Form a request: taking the min and max values from the
command line. */
request.min = atoi(argv[2]);
request.max = atoi(argv[3]);
/*OVDE CITAMO I ARGUMENT POS I SMESTAMO GA U STRUKTURU
NARAVNO TI MOZES DA VRSIS I PROVERU DA UNESENI BROJ NIJE SLUCAJNO
NEGATIVAN JA NECU TIME DA SE OPTERECUJEM */
request.pos=atoi(argv[4]);
/* Call the remote procedure */
result = find_primes_1(&request, cl);
/*OVDE SAM STAVIO NULU JER JE RESULT INT A CINI MI SE DA STANDARD NE GARANTUJE
DA JE NULL==0 */
if (result == 0) {
clnt_perror(cl, argv[1]);
exit(3);
}
/* SADA MENJAMO ISPIS JER VISE NE POSTOJI NIZ PROSTIH BROJEVA VEC JE SAMO JEDAN
NARAVNO JA CU ISPIS PO SRPSKI A TI PROMENI KAKO TI ODGOVARA :) */
printf("%d. prosti broj po redu je %d\n", request.pos, result);
/* Clean up */
clnt_destroy(cl);
return 0;
}
to bi trebalo da bude to bar sto se tice klijenta, e sada jos samo da promenimo server :
Code:
#include "primes.h"
int isprime(int);
char *strtime(struct tm *);
/**
* The server procedure is defined as
* ret_type <func name>_<vers num>_svc(req_type, struct svc_req *)
**/
/*OVDE MENJAM TIP KOJI VRACA FUNKCIJA */
int
find_primes_1_svc(prime_request *request, struct svc_req *rqstp)
{
/**
* Reserve stroage for the results. This MUST be static,
* otherwise it will be deallocated from the stack when the
* procedure terminates. By the time the xdr_prime_result
* filter gets to serialise the results, it is no longer valid.
**/
/*CINI MI SE DA VISE NEMA POTREBE DA BUDU STATIC OVI ELEMENTI JER
VRACAMO SAMO INT PO VREDNOSTI ALI JA CU IH OSTAVITI A TI RADI KAKO HOCES */
static prime_result result;
static int prime_array[MAXPRIMES];
int i, count=0;
time_t curr_time;
/* Log requests. */
curr_time = time(NULL);
printf("%s find_primes_1_svc(): range = [%d,%d]\n",
strtime(localtime(&curr_time)), request->min, request->max);
/* Loop over the range limits in the request "packet". */
for (i=request->min; count<MAXPRIMES && i<=request->max; i++)
if (isprime(i))
prime_array[count++] = i;
/**
* Assemble the reply packet. Note that the variable length
* array we are returning is really a struct with an
* element count and a pointer to the first element.
**/
result.array.array_len = count;
result.array.array_val = prime_array;
/*E POSTO JE DO OVOG KORAKA ON PRONASAO SVE PROSTE BROJEVE MI CEMO SAMO
UZETI BROJ KOJI JE NA ONOM MESTU KOJE NAMA TREBA.
DA SAM IZ PRIMES.X IZBRISAO STRUKTURU primes_result MORAO BIH DA MENJAM DOBAR DEO OVOG
KODA A OVAKO SAMO DODAJEM PAR REDA, JESTE DA BI SE ZADATAK BRZE IZVRSAVAO BEZ NJE ALI JE
I OVAKO NADAM SE OK*/
/* SADA TREBA DA PITAMO DA LI JE POS VECE OD BROJA PRONADJENIH BROJEVA */
if (request->pos>result.array.array_len)
return 0; /*ZNACI AKO JESTE VECE ONDA NEMAMO ELEMENT KOJI TRAZIMO I VRACAMO NULU */
else return prime_array[request->pos-1]; /*A AKO NIJE ONDA VRACAMO TAJ ELEMENT
OVDE STAVLJAMO -1 ZA POZICIJU JER INDEXI
KRECU OD 0 */
}
/**
* This is a 'private' support function and is not directly
* accessible via RPC. Returns 1 if n is prime and 0 otherwise.
**/
int isprime(int n)
{
int i;
if (n < 2)
return 0;
for (i=2; i*i<=n; i++)
if ((n % i) == 0)
return 0;
return 1;
}
/**
* Private function to stringify time structure.
**/
char *strtime(struct tm *timeptr)
{
static char tm_str[] = "MMM dd hh:mm:ss";
char *tm_mon;
switch (timeptr->tm_mon) {
case 0: tm_mon = "Jan"; break;
case 1: tm_mon = "Feb"; break;
case 2: tm_mon = "Mar"; break;
case 3: tm_mon = "Apr"; break;
case 4: tm_mon = "May"; break;
case 5: tm_mon = "Jun"; break;
case 6: tm_mon = "Jul"; break;
case 7: tm_mon = "Aug"; break;
case 8: tm_mon = "Sep"; break;
case 9: tm_mon = "Oct"; break;
case 10: tm_mon = "Nov"; break;
case 11: tm_mon = "Dec"; break;
default: return NULL;
}
sprintf(tm_str, "%3s %2d %02d:%02d:%02d", tm_mon, timeptr->tm_mday,
timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
return tm_str;
}
To bi trebalo da bude to, nisam isprobao program pa necu da garantujem da radi, ali ti probaj pa ako nesto negde zeza ostavi poruku, naravno javi se i ako treba neko dodatno objasnjenje oko stvari koje sam menjao.
poz