Dodaj do ulubionych

przepraszam za to

IP: *.imada.sdu.dk 29.10.03, 15:43
umieszczam to tu bo potrzebuje jakiegos miejsca na sieci ktore przetrzyma mi ten kod, przepraszam

#include <stdlib.h>
#include <subnet.h>
#include <stdlib.h>
#include <subnetsupport.h>

#define enable_networklayer 0x10
#define disable_networklayer 0x20
#define networklayer_ready 0x08
#define MAX_SEQ 7
#define NR_BUFS ((MAX_SEQ+1)/2)

char *StationName; /* Global variabel til at overf?re programnavn */
int ThisStation; /* Global variabel der identificerer denne station. */
log_type LogStyle = nolog; /* Hvilken slags log skal systemet f?re */
/* mulige: 'nolog', 'separate' eller 'synchronized' */

char *GetDataFromUser()
{
char temp[100];
char id[10];
char *output;

printf("Input text to send: ");
gets(temp);
printf("Input destination [1-8]: ");
gets(id);
output=(char *)malloc((1+strlen(temp)+1)*sizeof(char));
strncpy(output,id,1);
strcat(output,temp);
return output;
}

void NetworkLayer(void)
{
char *temp;
event_t event;

while(1)
{
Wait(&event,enable_networklayer);// | disable_networklayer);
// switch(event.type)
// {
// case enable_networklayer:
temp=GetDataFromUser();
Signal(networklayer_ready,(void *)temp);
// break;
// case disable_networklayer:
// break;
// }
}
}

void DatalinkLayer()
{
int i=0;
event_t event;
while(1)
{
i++;
if(i%4==0)
{
Signal(enable_networklayer,NULL);
Wait(&event,networklayer_ready);
printf("\nOdebralem: %s",event.msg);
}
Signal(disable_networklayer,NULL);
if(i==10)
Stop();
}
}

int main(int argc, char *argv[])
{
StationName = argv[0];
ThisStation = atoi(argv[1]);

Activate(1, NetworkLayer, "Networklayer");
Activate(1, DatalinkLayer, "DLlayer");

Start();
}
Obserwuj wątek
    • Gość: Conveyor Re: przepraszam za to IP: *.sieci.bianet.com.pl / 195.117.135.* 29.10.03, 16:11
    • Gość: Conveyor Re: przepraszam za to IP: *.sieci.bianet.com.pl / 195.117.135.* 29.10.03, 16:11
      To wyslij sobie na poczte, nie zasmiecaj forum
      • Gość: JV Re: przepraszam za to IP: *.neoplus.adsl.tpnet.pl 29.10.03, 17:31
        Gość portalu: Conveyor napisał(a):
        > To wyslij sobie na poczte, nie zasmiecaj forum

        No wlasnie! Co za debil.... To tak jakby się wysrać w kuchni pod stołem a
        później przeprosić żonę i powiedzieć, że przecież szukałeś miejsca w domu gdzie
        można się wysrać...
        • Gość: woartur Re: przepraszam za to IP: *.79.120.115.adsl.od.worldonline.dk 30.10.03, 11:49
          #include <stdlib.h>
          #include <stdio.h>
          #include "include/subnet.h"
          #include "include/subnetsupport.h"

          #define networklayer_ready 0x08
          #define enable_networklayer 0x10
          #define stop_simulation 0x20

          #define MAX_SEQ 7
          #define NR_BUFS ((MAX_SEQ+1)/2)
          #define MAX_HOST_NO 8 // <=8
          #define TIMER_DELAY 100
          #define ACK_TIMER_DELAY 40


          char *StationName; /* Global variabel til at overf�¸re
          programnavn */
          int ThisStation; /* Global variabel der identificerer denne station.
          */

          log_type LogStyle=nolog;

          typedef enum { FALSE =0, TRUE} boolean;
          typedef enum { DATA, ACK, SEQ, NAK } frame_kind;

          typedef struct
          {
          frame_kind kind;
          int seq;
          int ack;
          char *info;
          } frame;

          typedef struct
          {
          int id;
          int ack_expected;
          int next_frame_to_send;
          char *out_buf[NR_BUFS];
          char *in_buf[NR_BUFS];
          int nbuffered;
          int frame_expected;
          int too_far;
          int oldest_frame;
          boolean no_nak;
          boolean timers[MAX_SEQ]; //FALSE=OFF, TRUE=ON
          boolean arrived[NR_BUFS];
          unsigned int ack_timer_id;
          } host;

          char *GetDataFromUser()
          {
          char temp[100];
          char id[2];
          char *output;
          // char c;

          // printf("\nTo stop simulation press 'q', other to proceed\n");
          // c=getchar();
          // if(c=='q')
          // {
          // Signal(stop_simulation,NULL);
          // Stop();
          // exit(0);
          // }
          printf("Input text to send: ");
          gets(temp);
          printf("Input destination [1-8]: ");
          gets(id);
          output=(char *)malloc((1+strlen(temp)+1)*sizeof(char));
          strncpy(output,id,1);
          strcat(output,temp);

          return output;
          }

          char *itoa(int n)
          {
          char *s=malloc(42);
          if(s)
          sprintf(s,"%d",n);
          return s;
          }

          void InitializeHost(host *h,int a_id) //InitializeHost(&h,2);
          {
          int i=0;
          h->id=a_id;
          h->ack_timer_id=0;
          h->ack_expected=0;
          h->next_frame_to_send=0;
          h->frame_expected=0;
          h->nbuffered=0;
          h->too_far=NR_BUFS;
          for(i=0;i<NR_BUFS;i++)
          {
          h->arrived[i]=FALSE;
          h->timers[i]=FALSE;
          }
          }

          char *makepacket(frame s)
          {
          char *c_kind,*c_ack,*c_seq,*packet,*temp;

          switch(s.kind)
          {
          case DATA:
          c_kind=(char *)malloc((4+1)*sizeof(char));
          strcpy(c_kind,"DATA");
          case NAK:
          c_kind=(char *)malloc((3+1)*sizeof(char));
          strcpy(c_kind,"NAK");
          case ACK:
          c_kind=(char *)malloc((3+1)*sizeof(char));
          strcpy(c_kind,"ACK");
          default:
          c_kind=(char *)malloc((4+1)*sizeof(char));
          strcpy(c_kind,"DATA");
          }

          c_ack=(char *)malloc((strlen(itoa(s.ack))+1)*sizeof(char));
          strcpy(c_kind,itoa(s.ack));
          c_seq=(char *)malloc(((int)strlen(itoa(s.seq))+1)*sizeof(char));
          strcpy(c_kind,itoa(s.seq));

          packet=(char *)malloc((strlen(c_kind)+strlen(c_ack)+strlen(c_seq)+strlen
          (s.info)+1)*sizeof(char));
          strcpy(packet,c_kind);
          strcat(packet,c_seq);
          strcat(packet,c_ack);

          free(c_kind);
          free(c_ack);
          free(c_seq);

          return packet;
          }

          void send_frame(frame_kind fk,int frame_nr, int frame_expected,char *buffer[],
          int destination,host *h)
          {
          frame s;
          int timer_id;
          int helper=(frame_nr%NR_BUFS);
          char *temp;
          s.kind=fk;
          if(fk==DATA)
          s.info=buffer[frame_nr%NR_BUFS];
          s.seq=frame_nr;
          s.ack=(frame_expected+MAX_SEQ)%(MAX_SEQ+1);
          if(fk==NAK)
          h->no_nak=FALSE;
          temp=makepacket(s);
          ToSubnet(ThisStation,destination,temp,strlen(temp));
          if(fk==DATA)
          {
          timer_id=SetTimer(TIMER_DELAY,NULL);
          h->timers[helper]=SetTimer(TIMER_DELAY,NULL);
          }
          StopTimer(h->ack_timer_id,NULL);
          }

          int GiveId(event_t e)
          {
          char n[2];
          n[0]=((char *)(e.msg))[0];
          n[1]=0;
          return atoi(n);
          }

          char *GiveData(event_t e)
          {
          char *data;
          int i=0;
          int l=(strlen((char *)e.msg)-1);
          data=(char *)malloc(l*sizeof(char)+1);
          while(1)
          {
          data[i]=((char *)e.msg)[i+1];
          if(!data[i])
          break;
          i++;
          }
          return data;
          }

          void NetworkLayer(void)
          {
          char *temp;
          event_t event;

          while(1)
          {
          Wait(&event,enable_networklayer);// | disable_networklayer);
          switch(event.type)
          {
          case enable_networklayer:
          //event.msg=(void *)"2bleble";
          temp=GetDataFromUser();
          Signal(networklayer_ready,(void *)temp);
          break;
          //no case disable_networklayer:
          //no break;
          }
          }
          }

          void FromNetworkLayer(event_t ev,host *h)
          {
          char *t=GiveData(ev);
          int i=((h->next_frame_to_send) % NR_BUFS);
          h->out_buf[i]=t;
          }

          void DatalinkLayer()
          {
          event_t event;
          int i,dest;
          char temp[100];
          // host *hst;
          // host *host_table[MAX_HOST_NO];
          // for(i=0;i<MAX_HOST_NO;i++)
          // host_table[i]=NULL;
          Signal(enable_networklayer,temp);

          while(1)
          {
          Wait(&event, networklayer_ready );//| frame_arrival | timeout);

          switch(event.type)
          {
          case networklayer_ready:
          // dest=GiveId(event);
          // printf("%d",dest);
          printf("doszedlem tu");
          /* if(host_table[dest])
          {
          host *h=(host *)malloc(sizeof(host));
          InitializeHost(h,dest);
          host_table[dest]=h;
          }
          hst=host_table[dest];
          hst->nbuffered++;
          FromNetworkLayer(event,hst);
          send_frame(DATA,hst->next_frame_to_send,hst->frame_expected,hst-
          >out_buf,dest,hst);
          (hst->next_frame_to_send)++;
          (hst->nbuffered)++;

          break;
          case frame_arrival:
          break;
          case timeout:
          */ break;
          }

          // if(hst->nbuffered < NR_BUFS)
          Signal(enable_networklayer,NULL);
          }
          // Stop();
          }

          int main(int argc, char *argv[])
          {
          StationName = argv[0];
          ThisStation = atoi(argv[1]);

          Activate(1, NetworkLayer, "Networklayer");
          Activate(1, DatalinkLayer, "Datalinklayer");

          Start();
          }
          • Gość: woartur Re: przepraszam za to IP: *.79.120.115.adsl.od.worldonline.dk 30.10.03, 11:51
            #include "common.h"
            #include "typewriter.h"
            #include "protocol.h"

            /***<< typewriter function >>***
            * This is a simple process that reads input from the user and sends it as it
            is to the protocol handler.
            * For the ease of use some command-line options options have been developed.
            * The difference between stop and exit commands is that the first sends Stop
            signal to network emulator and
            * hence stops the whole simulation. The second command on the other hand only
            exits the typewriter without
            * stopping the simulation. Other typewriters on other consoles/stations may
            still be active.
            * Please note that all commands are case sensitive.
            * Additionally not that fgets has been used instead of gets. The use of gets
            is simpler but does not
            * protect from buffer overflows and hence is dangerous. Use of fgets solves
            this problem.
            */

            void typewriter(void)
            {
            int end = 0; // false
            char temp; // needed for /rcptX command
            int rcpt = ThisStation; // by default sends packets to itself
            char inputbuffer[TYPEWRITERBUFFERSIZE]; // the input buffer for fgets

            printf("Typewriter process started. Please enter text.\n");
            printf("Write /help for help or /exit to stop simulation.\n");
            do {
            printf("> ");
            fgets(inputbuffer, TYPEWRITERBUFFERSIZE, stdin);
            inputbuffer[strlen(inputbuffer)-1] = 0; // truncate the trailing '\n'

            if (inputbuffer[0]=='/') {
            if (!strcmp(inputbuffer, "/exit")) {end = 1; continue;}
            if (!strcmp(inputbuffer, "/stop")) {end = 1; Stop(); continue;}
            if (!strcmp(inputbuffer, "/help")) {
            printf("Help:\n");
            printf(" /exit - exits this instance of typewriter\n");
            printf(" /stop - stops network simulation and exits the
            typewriter\n");
            printf(" /help - shows this message\n");
            printf(" /rcptX - changes the receipient to X [current is %u]\n",
            rcpt);
            continue;
            }
            temp = inputbuffer[5];
            inputbuffer[5]=0; // truncate everything after 5th character
            if (!strcmp(inputbuffer, "/rcpt")) {
            printf("Changing receipient number... ");
            if ((temp<'9') && (temp>'0')) {
            rcpt = temp-'0';
            printf("receipient changed to %hu\n", rcpt);
            } else {
            printf("the provided number is out of scope (1..8).\n");
            }
            continue;
            }
            printf("command not understood!\n");
            } else if (strlen(inputbuffer)ɬ) { // only if the user has actually
            written something
            printf("sending: '%s'\n", inputbuffer);
            sendpacket(rcpt, inputbuffer);
            }
            } while (!end);
            printf("Typewriter closed.\n");
            }
            • Gość: woartur Re: przepraszam za to IP: *.79.120.115.adsl.od.worldonline.dk 30.10.03, 11:51
              #include "common.h"
              #include "protocol.h"

              void protocolhandler(void)
              {
              event_t rcvdevent;
              char textbuffer[200];
              long eventType = EVENT_ANY; // by default, wait for any event
              LOG("Starting protocol handler\n");
              while (1)
              {
              Wait((void*)&rcvdevent, eventType); // wait for an event of specific type
              sprintf(textbuffer, "event: type(%u) ", rcvdevent.type);
              LOG(textbuffer);
              switch (rcvdevent.type)
              {
              case (EVENT_CLIENT2PROTO): // received a packet to transmit it down to
              another station
              {
              int rcpt;
              char *body;
              LOG("[client2proto]");
              body = (char*)rcvdevent.msg; // temporarily
              rcpt = body[0] - '0'; // the first character is the rcpt
              number (ASCII)
              body++; // but the body itself begins from the
              second char

              sprintf(textbuffer, " dest(%u) body(%s)", rcpt, body);
              LOG(textbuffer);
              free(rcvdevent.msg); // no memory leaks, please
              break;
              }
              case (EVENT_PHYS2PROTO): // received a packet from physical layer
              LOG("[phys2proto]");
              break;
              case (EVENT_TIMEOUT):
              LOG("[timeout]");
              break;
              default:
              LOG("[unknown] ignoring");
              }
              LOG("\n");
              }
              }

              void sendpacket(short int rcpt, char *msg)
              {
              char *buff = malloc(strlen(msg) + 2); // first extra for rcpt number, the
              latter is for null terminator
              buff[0] = rcpt+'0';
              strcpy(buff+1, msg);
              Signal(EVENT_CLIENT2PROTO, buff);
              // Please note that the encoded receipient number is 'shifted' from its
              integer value 1..8 to
              // ASCII code corresponding to that number - '1'..'8'. It is done to prevent
              problems resulting
              // from low ASCII codes inside strings. For example 0 in the beginning
              (although receipient should
              // be > 0) would mean a string terminator, although it should not.
              // Additionaly such strings are easier to debug as they can be read directly.
              }
              • Gość: JV Re: przepraszam za to IP: *.neoplus.adsl.tpnet.pl 30.10.03, 12:06
                No to mamy na forum koprofaga...
                • poziomk Re: przepraszam za to 30.10.03, 12:09
                  napisz na "forum o forum" zeby usuneli
Inne wątki na temat:

Nie masz jeszcze konta? Zarejestruj się


Nakarm Pajacyka