Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------------------------
- // Robert Worland: 08011047 Luke Gee: 09018018
- // Date: 03/10
- // 10 char messaging system
- //--------------------------------------------------------------------------------------
- package worksheet1;
- public class JavaLan {
- private static int pendindex = 0;
- private static SerialPortHandler sio;
- private static Term terminal;
- private static final int LOGIN = 0,
- ADDRPENDING = 1,
- MENU = 2,
- GETADDR = 3,
- INPUTMESS = 4;
- private static int kindex;
- private static final int PACKETSIZE = 16;
- private static char myaddr = 0;
- private static char kbdpacket[] = new char[PACKETSIZE];
- private static char rxpacket[] = new char[PACKETSIZE];
- private static char txpacket[] = new char[PACKETSIZE];
- private static final int PENDTABLE_SIZE = 26;
- private static final int DELAY = 10000;
- private static final int WAITING = 0,
- RECEIVING = 1,
- ARRIVED = 2,
- DECODING = 3;
- private static int rxindex = WAITING;
- private static PendtableRecord[] pendtable = new PendtableRecord[PENDTABLE_SIZE];
- private static final int SOM = 0,
- DEST = 1,
- SRC = 2,
- TYPE = 3,
- CHKSM = 14,
- EOM = 15;
- private static int keycount, inum;
- public static char[] clearpacket(char[] packet) {
- char[] p = new char[PACKETSIZE];
- for (int i = 0; i < packet.length; i++) {
- packet[i] = ' ';
- p[i] = ' ';
- }
- p[SOM] = packet[SOM] = '{'; // sets start char to {
- p[EOM] = packet[EOM] = '}'; // sets end to }
- return p;
- }
- public static void setchsum(char[] packet) {
- packet[CHKSM] = 'S';
- // int chksm = 0, i;
- // for (i = 0; i < 16; i++) chksm += kbdpacket[i]; {
- //
- // kbdpacket[CHKSM] = (char) ~(chksm % 128);
- // kbdpacket[CHKSM] |= 0x80;
- // }
- }//checksum set END
- //--------------------------------------------------------------------------------------
- // Start of main method
- // Sets up COM port handler and Terminal
- // Loops userinput, transmitter and reciever until system exit
- //--------------------------------------------------------------------------------------
- public static void main(String[] args) {
- sio = new SerialPortHandler(SerialPortHandler.COM1);
- terminal = new Term();
- welcome("Rob and Luke");
- for (int i = 0; i < PENDTABLE_SIZE; i++) {
- pendtable[i] = new PendtableRecord();
- }
- while (true) { // Stops output window looping
- userInput();
- transmitter();
- receiver();
- } // loops forever
- }// main
- //--------------------------------------------------------------------------------------
- // User input state switch
- // allows keyboard input
- //--------------------------------------------------------------------------------------
- private static void userInput() {
- char key = 0;
- //-------------------------------------------------------------------------------------------
- // Login case: sets the users login to the character that is input through the keyboard
- //-------------------------------------------------------------------------------------------
- switch (kindex) {
- case LOGIN:
- if (terminal.kbhit()) {
- key = terminal.getChar();
- key = Character.toUpperCase(key); //takes the character and changes it to uppercase
- if (key >= 'A' && key <= 'Z') { //checks that the letter is from A-Z
- terminal.putChar(key);
- terminal.putChar('\n');
- myaddr = key;
- kbdpacket = clearpacket(kbdpacket); //clears the packet ready for building
- kbdpacket[DEST] = myaddr; //sets the destination of the packet to your desired address
- kbdpacket[SRC] = myaddr; //sets the source to your address
- kbdpacket[TYPE] = 'L'; //sets the packet to a 'Login' packet
- setchsum(kbdpacket); //sets the checksum at the end of the packet
- System.out.println(kbdpacket);
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //
- pendtable[i].setPending(0); //
- pendtable[i].setDelay(0); // sends the packet to the comm port
- } //
- pendtable[myaddr - 'A'].setPacket(kbdpacket); //
- pendtable[myaddr - 'A'].setPending(5); //
- pendtable[myaddr - 'A'].setDelay(DELAY); //
- kindex = ADDRPENDING; // program jumps to 'ADDRPENDING'
- }
- }//if(terminal.kbhit()) END
- break;
- //--------------------------------------------------------------------------------------
- // Set login to the character
- // Display menu on terminal
- //--------------------------------------------------------------------------------------
- case ADDRPENDING:
- if (pendtable[myaddr - 'A'].getLoggedin() == -1) {
- // System.out.println("" + myaddr);
- // pendtable[myaddr - 'A'].setLoggedin(-1);
- for (int i = 0; i < 26; i++) {
- int j = pendtable[i].getLoggedin();
- System.out.println("" + j);
- }
- pendtable[myaddr - 'A'].setPending(0);
- // if (pendtable[myaddr - 'A'].getLoggedin() == -1) {
- terminal.println("Your node id is now set to: " + myaddr);
- menu(); //displays the menu
- kindex = MENU; //program jumps to 'MENU'
- }//if END
- // }
- break;
- //--------------------------------------------------------------------------------------
- // Takes instruction form the user input and moves to the appropriate selection
- //
- //--------------------------------------------------------------------------------------
- case MENU:
- if (terminal.kbhit()) {
- key = terminal.getChar();
- key = Character.toUpperCase(key);
- if (key == 'D' || key == 'S' || key == 'C' || key == 'L') { //checks to see if the key pressed is D,S,C or L
- switch (key) {
- case 'D': //
- terminal.print("Choose a destination address: "); //if D is presssed jumps to 'GETADDR'
- kindex = GETADDR; //where user sets destination address
- break;
- case 'S': //if S is selected
- if (kbdpacket[1] > 0) {
- terminal.println("You chose: " + key); //
- pendtable[kbdpacket[1] - 'A'].setPending(1); //
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //
- pendtable[i].setPending(0); // sends the packet with the message in.
- pendtable[i].setDelay(0); //
- } //
- pendtable[myaddr - 'A'].setPacket(kbdpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY); //
- clearpacket(kbdpacket);
- clearpacket(rxpacket);
- terminal.println("Packet Sent");
- terminal.println("");
- System.out.println("this should be empty packet: " + kbdpacket);
- menu(); //displays the menu
- }
- kindex = MENU; // ERROR!! SHOULD THIS GO TO MENU???
- break;
- case 'C': //
- terminal.println("You chose: " + key); //
- clearpacket(txpacket); //
- terminal.println("Packet Cleared"); // when c is selected it clears the packet
- terminal.println(""); //
- System.out.println(txpacket); //
- terminal.println("Please choose another option: "); //
- menu(); // displays the menu
- break;
- case 'L':
- terminal.println("\nYou are now logged out.\n");
- terminal.print("Please enter you next login: ");
- clearpacket(kbdpacket); //cleared the packet
- kbdpacket[1] = myaddr; //sets the first letter to users address
- kbdpacket[2] = myaddr; //sets the second letter to users address
- kbdpacket[3] = 'X'; //set the packet type to 'X' (logout)
- setchsum(kbdpacket); //sets the checksum at the end of the packet
- System.out.println(kbdpacket);
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //
- pendtable[i].setPending(0); //fdssends the logout packet
- pendtable[i].setDelay(0); //sets the logout packet ready for sending
- } //
- pendtable[myaddr - 'A'].setPacket(kbdpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY); //
- if (pendtable[myaddr - 'A'].getPending() == 0) {
- System.out.println("gettiong heerer");
- System.arraycopy(pendtable[myaddr - 'A'].getPacket(), 0, kbdpacket, 0, 16); //changed from txpacket
- pendtable[myaddr - 'A'].setPending(1);
- }
- for (int i = 0; i < 26; i++) {
- pendtable[i].setLoggedin(0);
- }
- for (int i = 0; i < 26; i++) {
- int j = pendtable[i].getLoggedin();
- System.out.println("" + j);
- }
- myaddr = 0;
- kindex = LOGIN;
- break;
- default:
- break;
- }// case END
- }//if D,S,C or L END
- }//if terminal.kbhit END
- //--------------------------------------------------------------------------------------
- // Sets the address for the destination of the packet set by the user
- // Print out the menu before moving back to allow another selection
- //--------------------------------------------------------------------------------------
- case GETADDR:
- // System.out.println("getting here");
- if (terminal.kbhit()) {
- key = terminal.getChar();
- key = Character.toUpperCase(key);
- terminal.putChar(key);
- terminal.print("\n");
- // if (pendtable[key - 'A'].getLoggedin() == 0) {
- menu();
- kindex = MENU;
- // } else {
- clearpacket(kbdpacket); //set up packet
- //clearpacket(rxpacket);
- // clearpacket(txpacket);
- kbdpacket[1] = key; //dest addr set
- kbdpacket[2] = myaddr; //sets source
- kbdpacket[3] = 'D';
- setchsum(kbdpacket);
- System.out.println(kbdpacket);
- keycount = 4;
- terminal.println("You chose: " + key);
- terminal.println("Please enter your message (up to 10 characters): ");
- System.out.println(kbdpacket);
- kindex = INPUTMESS;
- // }
- }//if(terminal.kbhit()) END
- break;
- //--------------------------------------------------------------------------------------
- // Allows the user to input a 10 character long message to be entered
- //--------------------------------------------------------------------------------------
- case INPUTMESS:
- if (terminal.kbhit()) {
- key = terminal.getChar();
- terminal.putChar(key);
- if (keycount <= 13 && key != '\n' && key > 0) { //if the keycount is less than 14, the letter is not a new line and is a valid character
- kbdpacket[keycount++] = key; //add 1 to the keycount
- System.out.println(kbdpacket);
- System.out.println("keycount: " + keycount);
- } else { //when 10 chars or enter has occoured
- //keycount=15;
- terminal.println("");
- terminal.println("End of message!");
- setchsum(kbdpacket); // set the checksum
- if (pendtable[kbdpacket[1] - 'A'].getPending() == 0) {
- System.arraycopy(pendtable[myaddr - 'A'].getPacket(), 0, kbdpacket, 0, 16);
- }
- kindex = MENU; // go to the menu
- menu();
- }//if keycount<14 END
- }// if terminal.kbhit END
- break;
- default:
- terminal.println("Error in keyboard user state machine!!"); // shouldnt get here!!!!
- break;
- }//switch(kindex) END
- }//public static void () END
- //--------------------------------------------------------------------------------------
- // Transmits the packets from the pendtable through the COM port,
- //--------------------------------------------------------------------------------------
- private static void transmitter() {
- if (++pendindex > ('Z' - 'A')) {
- pendindex = 0;
- }
- if (pendtable[pendindex].getPending() > 0) {
- if (pendtable[pendindex].getDelay() == DELAY) {
- sio.sendpacket(pendtable[pendindex].getPacket());
- }
- }
- pendtable[pendindex].decDelay();
- if (pendtable[pendindex].getDelay() == 0) {
- pendtable[pendindex].decPending();
- if (pendtable[pendindex].getPending() > 0) {
- }
- }
- }//transmitter END
- //--------------------------------------------------------------------------------------
- // Starts the receiving process
- //--------------------------------------------------------------------------------------
- private static void receiver() {
- char letter = 0;
- switch (rxindex) {
- //--------------------------------------------------------------------------------------
- // While there is no incoming transmission.
- // detects incoming transmission and moves to the recieving case
- //--------------------------------------------------------------------------------------
- case WAITING:
- //char letter = 0;
- letter = sio.getChar();
- if (letter == '{') { // when '{' is recognised... packet arriving
- System.out.println("getting here!!!");
- inum = 0;
- rxpacket[inum++] = letter; // first letter only
- rxindex = RECEIVING; //goes to 'RECEIVING'
- }
- break;
- //--------------------------------------------------------------------------------------
- // If there is an incoming message, checks packet length
- // moves to arrived case
- //--------------------------------------------------------------------------------------
- case RECEIVING:
- letter = sio.getChar();
- if (letter > 0) {
- rxpacket[inum++] = letter;
- if (inum > 15) { //when the packet length has reached 15
- System.out.println(rxpacket);
- rxindex = ARRIVED; // it will jump to 'ARRIVED'
- }
- }
- break;
- //--------------------------------------------------------------------------------------
- // indicates that a packet has been recieved
- // continues sending the packet on
- // moves on to Decoding case
- //--------------------------------------------------------------------------------------
- case ARRIVED:
- //terminal.println("\nRecieved Packet: "+rxpacket[4]+rxpacket[5]+rxpacket[6]+rxpacket[7]+rxpacket[8]+rxpacket[9]+rxpacket[10]+rxpacket[11]+rxpacket[12]+rxpacket[13]);
- txpacket = clearpacket(txpacket);
- System.out.println("junk????????????? : " + txpacket);
- //check checksum if else
- rxindex = DECODING;
- break;
- //--------------------------------------------------------------------------------------
- // Allows the system to recognise who the packet is from, what type of packet it is
- // and who the desired destination is.
- //--------------------------------------------------------------------------------------
- case DECODING:
- fromMeToMe();
- fromMeToYou();
- fromYouToMe();
- fromYouToYou();
- rxindex = WAITING;
- break;
- default:
- break;
- }//switch(rxindex) END
- // }
- }//public static void receiver () END
- private static void fromMeToMe() {
- if (rxpacket[SRC] == myaddr) {
- if (rxpacket[DEST] == myaddr) {
- switch (rxpacket[TYPE]) {
- case 'L':
- terminal.println("Recived Login Packet for: " + myaddr);
- // if(rxpacket[CHKSM] == txpacket[CHKSM]){
- if (pendtable[myaddr - 'A'].getLoggedin() == 0) {
- pendtable[myaddr - 'A'].setLoggedin(-1);
- pendtable[myaddr - 'A'].setPending(0);
- }//}
- // else{
- // terminal.println("checksum error");
- // }
- break;
- case 'R': //illegal! -- clear the packet/leave to overwrite, remove entry in pendtable
- pendtable[myaddr - 'A'].setPending(-1); // could be 0
- break;
- case 'D': // test msg to myself, ACK it -- print the message
- //if(rxpacket[CHKSM] == txpacket[CHKSM]){
- terminal.println("\nRecieved Message: " + rxpacket[4] + rxpacket[5] + rxpacket[6] + rxpacket[7] + rxpacket[8] + rxpacket[9] + rxpacket[10] + rxpacket[11] + rxpacket[12] + rxpacket[13]);
- menu();
- //}
- // else{
- // terminal.println("checksum error");
- // }
- rxindex = MENU;
- break;
- case 'A': // got ACK to my msg
- pendtable[myaddr - 'A'].setPending(-1);//could be 0
- break;
- case 'N':// got NAK to my msg, ket try again!-- left blank
- break;
- case 'X':// my logout
- pendtable[myaddr - 'A'].setLoggedin(0); //
- for (int i = 0; i < 26; i++) { // sets this terminal to logged out
- pendtable[i].setLoggedin(0); // sets value in the pendtable to 0
- }
- break;
- default:
- break;
- } //switch END
- }//if rxpacket[DEST] END
- }//if rxpacket[SRC] END
- }//from me to me END
- private static void fromMeToYou() {
- if (rxpacket[SRC] == myaddr) {
- if (rxpacket[DEST] != myaddr) { //change to destination address
- switch (rxpacket[TYPE]) {
- case 'L'://illegal
- pendtable[myaddr - 'A'].setPending(-1);
- break;
- case 'R': //error where has he gone now?
- break;
- case 'D': // rx failed, del packet, re tx from pendtable-- left blank
- break;
- case 'A': // rx failed delete packet
- pendtable[myaddr - 'A'].setPending(-1); // could be 0
- break;
- case 'N':// rx failed delete packet
- pendtable[myaddr - 'A'].setPending(-1); // could be 0
- break;
- case 'X':// illegal
- pendtable[myaddr - 'A'].setPending(-1);
- break;
- default:
- break;
- }//switch END
- }//if rxpacket[DEST] END
- }//if rxpacket[SRC] END
- }// from me to you END
- private static void fromYouToMe() {
- if (rxpacket[SRC] != myaddr) { // change to source destination
- if (rxpacket[DEST] == myaddr) {
- switch (rxpacket[TYPE]) {
- case 'L'://illegal
- pendtable[myaddr - 'A'].setPending(-1);
- break;
- case 'R': //error where has he gone now?
- break;
- case 'D': // rx failed, del packet, re tx from pendtable
- terminal.println("\nRecieved Message: " + rxpacket[4] + rxpacket[5] + rxpacket[6] + rxpacket[7] + rxpacket[8] + rxpacket[9] + rxpacket[10] + rxpacket[11] + rxpacket[12] + rxpacket[13]);
- break;
- case 'A': // rx failed delete packet
- pendtable[myaddr - 'A'].setPending(-1); // could be 0
- break;
- case 'N':// rx failed delete packet
- pendtable[myaddr - 'A'].setPending(-1); // could be 0
- break;
- case 'X':// illegal
- pendtable[myaddr - 'A'].setPending(-1);
- break;
- default:
- break;
- }//switch END
- }//if rxpacket[DEST] END
- }//if rxpacket[SRC] END
- }//from you to me END
- private static void fromYouToYou() {
- if (rxpacket[SRC] != myaddr) { //src address
- if (rxpacket[DEST] != myaddr) { //destination address
- switch (rxpacket[TYPE]) {
- case 'L'://re txpacket, update pending, send R
- System.out.println("getting here!!!!!"); //
- pendtable[myaddr - 'A'].setPacket(rxpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY); //
- break;
- case 'R': //illegal
- pendtable[myaddr - 'A'].setPending(-1);
- break;
- case 'D': // re-tx packet
- //rxpacket to where it needs to go
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //possibly set to -1???
- pendtable[i].setPending(0); // sends the packet with the message in.
- pendtable[i].setDelay(0); //
- } //
- pendtable[myaddr - 'A'].setPacket(rxpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY);
- break;
- case 'A': // re-tx packet
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //possibly set to -1???
- pendtable[i].setPending(0); // sends the packet with the message in.
- pendtable[i].setDelay(0); //
- } //
- pendtable[myaddr - 'A'].setPacket(rxpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY);
- break;
- case 'N':// re-tx packet
- for (int i = 0; i < PENDTABLE_SIZE; i++) { //
- pendtable[i].setLoggedin(0); //possibly set to -1???
- pendtable[i].setPending(0); // sends the packet with the message in.
- pendtable[i].setDelay(0); //
- } //
- pendtable[myaddr - 'A'].setPacket(rxpacket); //
- pendtable[myaddr - 'A'].setPending(1); //
- pendtable[myaddr - 'A'].setDelay(DELAY);
- break;
- case 'X':// re-tx packet, ammend pendtable
- break;
- default:
- break;
- }//switch END
- }//if rxpacket[DEST] END
- }//if rxpacket[SRC] END
- }//from you to you END
- private static void welcome(String name) { //WELCOME MESSAGE
- terminal.println("\tWelcome to " + name + "s Serial JavaLan\n");
- terminal.println("Once logged in you can send TEN characters");
- terminal.println("message to anyone else who is logged in.\n\n");
- terminal.print("To login hit a character (A - Z): ");
- terminal.println(" ");
- }//welcome END
- private static void menu() { //MENU MESSAGE
- terminal.println(" ");
- terminal.println("Choose an option: ");
- terminal.println(" D: Set destination");
- terminal.println(" S: send packet");
- terminal.println(" C: Clear packet");
- terminal.println(" L: Logout");
- terminal.println(" ");
- }//menu END
- private static void ack() {
- clearpacket(kbdpacket); //set up packet
- kbdpacket[1] = rxpacket[2]; //dest addr set
- kbdpacket[2] = myaddr; //sets source
- kbdpacket[3] = 'A';
- setchsum(kbdpacket);
- }
- }//class END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement