| | 550 | //------ |
| | 551 | // Note: Probably not efficient or anything... |
| | 552 | class StreamConnection { |
| | 553 | |
| | 554 | public: |
| | 555 | StreamConnection(NetworkConnection& connection); |
| | 556 | int peekByte(); |
| | 557 | int readByte(); |
| | 558 | int skipSegment(const char * separators); // TODO: Return if separators found or not? |
| | 559 | int readSegmentByte(const char * separators); |
| | 560 | int debug; |
| | 561 | int gobbleMatch(const char * separators, const char * target); |
| | 562 | |
| | 563 | private: |
| | 564 | NetworkConnection& _connection; |
| | 565 | int _byteSeen; |
| | 566 | }; |
| | 567 | |
| | 568 | StreamConnection::StreamConnection(NetworkConnection& connection) : _connection (connection) { |
| | 569 | /* |
| | 570 | |
| | 571 | */ |
| | 572 | _byteSeen = -1; |
| | 573 | debug = 0; |
| | 574 | } |
| | 575 | |
| | 576 | int StreamConnection::peekByte() { |
| | 577 | /* |
| | 578 | */ |
| | 579 | if (_byteSeen < 0) { |
| | 580 | if (_connection.available()) { |
| | 581 | _byteSeen = _connection.read(); |
| | 582 | } |
| | 583 | } |
| | 584 | |
| | 585 | return _byteSeen; |
| | 586 | } |
| | 587 | |
| | 588 | int StreamConnection::readByte() { |
| | 589 | /* |
| | 590 | */ |
| | 591 | int newByte = -1; |
| | 592 | |
| | 593 | if (_byteSeen < 0) { |
| | 594 | if (_connection.available()) { |
| | 595 | newByte = _connection.read(); |
| | 596 | } |
| | 597 | } else { |
| | 598 | newByte = _byteSeen; |
| | 599 | _byteSeen = -1; |
| | 600 | } |
| | 601 | |
| | 602 | if (debug == 1) { |
| | 603 | Serial.print(newByte, BYTE); |
| | 604 | } |
| | 605 | |
| | 606 | return newByte; |
| | 607 | } |
| | 608 | |
| | 609 | int StreamConnection::readSegmentByte(const char * separators) { |
| | 610 | /* |
| | 611 | */ |
| | 612 | // Blocks |
| | 613 | while (peekByte() < 0) { // TODO: Time out? |
| | 614 | delay(500); |
| | 615 | } |
| | 616 | if (!strchr(separators, peekByte())) { |
| | 617 | return readByte(); |
| | 618 | } |
| | 619 | return -1; |
| | 620 | } |
| | 621 | |
| | 622 | int StreamConnection::skipSegment(const char * separators) { |
| | 623 | /* |
| | 624 | */ |
| | 625 | |
| | 626 | if (debug == 1) { |
| | 627 | Serial.println("Start skipping non-separator"); |
| | 628 | } |
| | 629 | |
| | 630 | |
| | 631 | do { |
| | 632 | if (peekByte() < 0) { |
| | 633 | return -1; |
| | 634 | } |
| | 635 | // Skip everything not a separator |
| | 636 | } while (!strchr(separators, peekByte()) && readByte()); |
| | 637 | |
| | 638 | if (debug == 1) { |
| | 639 | Serial.println("Finished skipping non-separator"); |
| | 640 | } |
| | 641 | |
| | 642 | do { |
| | 643 | /* // We can't return here or we get stuck above. |
| | 644 | if (peekByte() < 0) { |
| | 645 | return -1; |
| | 646 | }*/ |
| | 647 | while (peekByte() < 0) { |
| | 648 | delay(500); // TODO: Time out? |
| | 649 | } |
| | 650 | // Skip everything that *is* a separator |
| | 651 | } while (strchr(separators, peekByte()) && readByte()); |
| | 652 | |
| | 653 | if (debug == 1) { |
| | 654 | Serial.println("Finished skipping separator"); |
| | 655 | } |
| | 656 | |
| | 657 | return 1; |
| | 658 | } |
| | 659 | |
| | 660 | int StreamConnection::gobbleMatch(const char * separators, const char * target) { |
| | 661 | /* |
| | 662 | */ |
| | 663 | |
| | 664 | int idx = 0; |
| | 665 | int byteRead = -1; |
| | 666 | |
| | 667 | while ((byteRead = readSegmentByte(separators)) >=0) { |
| | 668 | if (idx < strlen(target)) { |
| | 669 | if (byteRead != target[idx]) { |
| | 670 | break; |
| | 671 | } |
| | 672 | idx++; |
| | 673 | } else { |
| | 674 | break; |
| | 675 | } |
| | 676 | } |
| | 677 | |
| | 678 | if ((byteRead == -1) && (idx == strlen(target))) { // Matched |
| | 679 | while (skipSegment(separators) < 0) { |
| | 680 | // Ummm, we ran outta data--this screws things up... |
| | 681 | // TODO: Have a time out? |
| | 682 | delay(500); |
| | 683 | } |
| | 684 | return 1; |
| | 685 | } |
| | 686 | return -1; |
| | 687 | } |
| | 688 | //------ |
| | 689 | |
| | 690 | |
| | 691 | |
| 516 | | #if |
| | 721 | #if 1 |
| | 722 | #if 1 //b |
| | 723 | |
| | 724 | |
| | 725 | |
| | 726 | NetworkConnection conn = Network.connect(209,177,146,34, 6667); // TODO: Better? |
| | 727 | |
| | 728 | Serial.println("Waiting to connect..."); |
| | 729 | |
| | 730 | while (!conn.isConnected()) { |
| | 731 | //Serial.print(!conn.isConnected()); |
| | 732 | delay(500); |
| | 733 | } |
| | 734 | |
| | 735 | Serial.println("Connected..."); |
| | 736 | |
| | 737 | int byteRead = -1; |
| | 738 | int dataWait = 0; |
| | 739 | |
| | 740 | StreamConnection stream = StreamConnection(conn); |
| | 741 | |
| | 742 | conn.print("NICK Arduino\n"); |
| | 743 | conn.print("USER Arduino 0 0 Arduino\n"); |
| | 744 | |
| | 745 | byte buf[4] = {0, 0, 0, 0}; |
| | 746 | |
| | 747 | int state = 0; |
| | 748 | |
| | 749 | while (conn.isConnected()) { |
| | 750 | #if 1 //c |
| | 751 | |
| | 752 | if (stream.peekByte() >= 0) { |
| | 753 | |
| | 754 | // New line |
| | 755 | //Serial.println("Process new line..."); |
| | 756 | if (stream.peekByte() != ':') { |
| | 757 | // Skip local responses/messages entirely. TODO: Handle PINGs? |
| | 758 | while (stream.skipSegment("\x0A\x0D") < 0) { |
| | 759 | // Ummm, we ran outta data--this screws things up... |
| | 760 | // TODO: Have a time out? |
| | 761 | delay(500); |
| | 762 | } |
| | 763 | } else { |
| | 764 | //Serial.println(state, HEX); |
| | 765 | if (state == 0) { |
| | 766 | // Skip sending servername // TODO: Check it's the main one? |
| | 767 | while (stream.skipSegment(" ") < 0) { |
| | 768 | // Ummm, we ran outta data--this screws things up... |
| | 769 | // TODO: Have a time out? |
| | 770 | delay(500); |
| | 771 | } |
| | 772 | |
| | 773 | for (int idx=0; idx < 3; idx++) { |
| | 774 | while (stream.peekByte() < 0) { // TODO: Time out? |
| | 775 | delay(500); |
| | 776 | } |
| | 777 | buf[idx] = stream.readByte(); |
| | 778 | //Serial.print(buf[idx], BYTE); |
| | 779 | } |
| | 780 | //Serial.print("//"); |
| | 781 | Serial.println((const char *) buf); |
| | 782 | |
| | 783 | if (strcmp((const char *) buf, "376") == 0) { // End MOTD |
| | 784 | state=1; |
| | 785 | conn.print("JOIN #arduino\n"); |
| | 786 | //stream.debug = 1; |
| | 787 | } |
| | 788 | } else if (state == 1) { |
| | 789 | //stream.debug = 1; |
| | 790 | //Serial.println("Skip servername"); |
| | 791 | // Skip sending servername // TODO: Check it's the main one? |
| | 792 | while (stream.skipSegment(" ") < 0) { |
| | 793 | // Ummm, we ran outta data--this screws things up... |
| | 794 | // TODO: Have a time out? |
| | 795 | delay(500); |
| | 796 | } |
| | 797 | //stream.debug = 0; |
| | 798 | |
| | 799 | /* |
| | 800 | int idx = 0; |
| | 801 | const char * PRIVMSG = "PRIVMSG"; |
| | 802 | //Serial.println("Checking for PRIVMSG."); |
| | 803 | |
| | 804 | //stream.debug = 1; |
| | 805 | |
| | 806 | while ((byteRead = stream.readSegmentByte(" ")) >=0) { |
| | 807 | if (idx < strlen(PRIVMSG)) { |
| | 808 | if (byteRead != PRIVMSG[idx]) { |
| | 809 | break; |
| | 810 | } |
| | 811 | idx++; |
| | 812 | } else { |
| | 813 | break; |
| | 814 | } |
| | 815 | } |
| | 816 | |
| | 817 | // stream.debug = 0; |
| | 818 | |
| | 819 | |
| | 820 | //Serial.println("Finished check PRIVMSG, determining result."); |
| | 821 | |
| | 822 | if ((byteRead == -1) && (idx == strlen(PRIVMSG))) { // Matched |
| | 823 | //Serial.println("Matched."); |
| | 824 | while (stream.skipSegment(" ") < 0) { |
| | 825 | // Ummm, we ran outta data--this screws things up... |
| | 826 | // TODO: Have a time out? |
| | 827 | delay(500); |
| | 828 | } |
| | 829 | */ |
| | 830 | |
| | 831 | if (stream.gobbleMatch(" ", "PRIVMSG") > 0) { |
| | 832 | /* ** |
| | 833 | while ((byteRead = stream.readSegmentByte("\x0A\x0D")) >=0) { |
| | 834 | Serial.print(byteRead, BYTE); |
| | 835 | } |
| | 836 | Serial.println(""); |
| | 837 | */ |
| | 838 | Serial.println("Matched PRIV MSG"); |
| | 839 | if (stream.gobbleMatch(" :", "#arduino") > 0) { // We treat the ":" as a separator too--does this break? |
| | 840 | Serial.println("Matched #arduino"); |
| | 841 | //Serial.println(stream.peekByte(), HEX); |
| | 842 | //if (stream.gobbleMatch("\x0A\x0D", "arduino:") > 0) { |
| | 843 | //if (stream.gobbleMatch(":", "Arduino") > 0) { |
| | 844 | //Serial.println("Matched Arduino"); |
| | 845 | if ((stream.peekByte() == 'A') && (stream.gobbleMatch(":", "Arduino") > 0)) { |
| | 846 | Serial.println("Matched something"); |
| | 847 | //conn.print("PRIVMSG #arduino :Huh, what? Was someone talking to me?\n"); |
| | 848 | conn.print("PRIVMSG #arduino :My current pot status is "); |
| | 849 | conn.print(((analogRead(2)*70)/630)/10+'0'); |
| | 850 | conn.print("3%, man!\n"); |
| | 851 | } |
| | 852 | } |
| | 853 | |
| | 854 | } else { |
| | 855 | //Serial.println("No match."); |
| | 856 | } |
| | 857 | |
| | 858 | |
| | 859 | |
| | 860 | } else { |
| | 861 | while ((byteRead = stream.readSegmentByte("\x0A\x0D")) >=0) { |
| | 862 | Serial.print(byteRead, BYTE); |
| | 863 | } |
| | 864 | Serial.println(""); |
| | 865 | } |
| | 866 | //Serial.println("Skipping this:"); |
| | 867 | //stream.debug = 1; |
| | 868 | while (stream.skipSegment("\x0A\x0D") < 0) { |
| | 869 | // Ummm, we ran outta data--this screws things up... |
| | 870 | // TODO: Have a time out? |
| | 871 | delay(500); |
| | 872 | } |
| | 873 | //stream.debug = 0; |
| | 874 | //Serial.println("\nFinish skipping."); |
| | 875 | |
| | 876 | } |
| | 877 | |
| | 878 | } |
| | 879 | |
| | 880 | #else //c |
| | 881 | if (conn.available()) { |
| | 882 | byteRead = conn.read(); |
| | 883 | Serial.print(byteRead, BYTE); |
| | 884 | if (byteRead == '\n') { |
| | 885 | dataWait++; |
| | 886 | Serial.println(dataWait, DEC); |
| | 887 | } |
| | 888 | if (dataWait==4) { |
| | 889 | //conn.print("NICK Arduino\n"); |
| | 890 | //conn.print("USER Arduino Arduino Arduino Arduino\n"); |
| | 891 | dataWait++; |
| | 892 | } |
| | 893 | if (dataWait==67) { |
| | 894 | conn.print("JOIN #arduino\n"); |
| | 895 | //conn.print("PRIVMSG #arduino :Hello, world!\n"); |
| | 896 | //delay(10000); |
| | 897 | conn.print("QUIT\n"); |
| | 898 | dataWait++; |
| | 899 | } |
| | 900 | //conn.print(conn.read()); |
| | 901 | } else { |
| | 902 | //dataWait++; |
| | 903 | //Serial.print(dataWait, DEC); |
| | 904 | } |
| | 905 | #endif //c |
| | 906 | } |
| | 907 | |
| | 908 | Serial.println(""); |
| | 909 | |
| | 910 | conn.close(); |
| | 911 | |
| | 912 | |
| | 913 | #else //b |