![]() |
Welcome to SIMPL Programming for Python
|
| Course content | Enter chat room | Send email
to mailing list |
Check calendar |
Lesson Overview |
In this lesson we are going to examine the detailed workings of a python gui-based sending program and a C coded receiving program.
The sender program is able to send three different types of messages and the receiver program to receive these messages and react appropriately.
The sender program in this lesson has a gui instead of a textual interface. The sending of the various messages are initiated by clicking on the various message buttons.
The messages are exactly the same as in Lessons #2 and #3.
The receiver program prints to screen the various components of the messages received.
The receiver program is exactly the same one as in Lesson #3.
With this algorithm, we hope to demonstrate the following:
Note:Do NOT cut and paste the following code pieces. The scripts are available in the .../src directory for you to run directly. |
Let's examine the sending script. |
The following code can be found in the ../src/sender_gui.py file. We
will examine it line by line.
| 1. """
2. sender_gui.py: This is a python simpl gui-based sender. 3. Refer to lesson 4. 4. Usage: python sender_gui.py 5. """ 6. 7. #! /usr/bin/python 8. 9. # import necessary modules 10. import sys 11. import wcsimpl 12. from Tkinter import * 13. 14. nee = None 15. 16. # define the callback for button 1 17. def hndlButton1(event): 18. token = 10 19. nee.packInt(token, nee.BIN) 20. a = 99 21. nee.packInt(a, nee.BIN) 22. b = 999 23. nee.packInt(b, nee.BIN) 24. c = 9999 25. nee.packInt(c, nee.BIN) 26. makeSend() 27. 28. # define the callback for button 2 29. def hndlButton2(event): 30. token = 20 31. nee.packInt(token, nee.BIN) 32. d = 3.1415 33. nee.packFloat(d, nee.BIN) 34. makeSend() 35. 36. # define the callback for button 3 37. def hndlButton3(event): 38. token = 30 39. nee.packInt(token, nee.BIN) 40. s = "We are the knights who say Neee." 41. l = len(s) 42. nee.packInt(l, nee.BIN) 43. nee.packString(s, nee.BIN) 44. makeSend() 45. 46. # define the call for the simpl send 47. def makeSend(): 48. retVal = nee.send(receiverId) 49. if retVal == -1: 50. print "%s: send error-%s" %(sName, nee.whatsMyError()) 51. sys.exit(-1) 52. 53. # define the exit call 54. def die(event): 55. sys.exit(0) 56. 57. # initialize some required variables 58. sName = "SENDER" 59. rName = "RECEIVER" 60. 61. # create an instance of Simpl 62. nee = wcsimpl.Simpl() 63. 64. # attach a simpl name 65. retVal = nee.nameAttach(sName, 1024) 66. if retVal == -1: 67. print "%s: name attach error-%s" %(sName, nee.whatsMyError()) 68. sys.exit(-1) 69. 70. # name locate the receiver 71. receiverId = nee.nameLocate(rName) 72. if receiverId == -1: 73. print "%s: name locate error-%s" %(sName, nee.whatsMyError()) 74. sys.exit(-1) 75. 76. # initialize Tk for graphics 77. root = Tk() 78. 79. # set up a button to initiate the sending of message token type 10 80. button = Button(root) 81. button["text"] = "send message token type 10" 82. button.bind(" 83. button.pack() 84. 85. # set up a button to initiate the sending of message token type 20 86. button = Button(root) 87. button["text"] = "send message token type 20" 88. button.bind(" 89. button.pack() 90. 91. # set up a button to initiate the sending of message token type 30 92. button = Button(root) 93. button["text"] = "send message token type 30" 94. button.bind(" 95. button.pack() 96. 97. # set up a button for program exit 98. button = Button(root) 99. button["text"] = "Exit" 100. button.bind(" 101. button.pack() 102. 103. # handle user input 104. root.mainloop() |
| Line 11 |
imports the wcsimpl module for SIMPL capability.
Line 14 sets a global variable which will become a an instance of Simpl further on. |
| Lines 17-26 | define a callback function that builds an outgoing message composed of an integer token and three other integers. The integer packing routines are called as instance methods and the packing type is binary because the message is being sent to a C program. Finally, the makeSend call performs the actual SIMPL Send. |
| Lines 28-51 | define callback functions for other messages in a similar way as the one above. |
| Lines 58-59 | define the unique SIMPL names which are treated as constants. |
| Line 62 | creates the Simpl instance used to refer to all things Simpl in this program. |
| Lines 65-68 | perform the simpl name attach. |
| Lines 71-74 | perform the simpl name locate. |
| Line 77 | initializes Tkinter and loads the root variable with the required information for the root window. |
| Lines 80-83 | create a button widget with text and define the function callback to be carried out in the case this button is selected. When this button is clicked on by the user a message with token type 10 will be sent to the receiver program. |
| Lines 86-89 | create a button for sending messages of token type 20 to the receiver program. |
| Lines 92-95 | create a button for sending messages of token type 30 to the receiver program. |
| Lines 98-101 | create a button that allows the user to exit gracefully from the script. |
| Line 104 | creates an infinite event loop on the root window that awaits incoming events (in this case mouse clicks on the various buttons). |
Let's examine the receiving program. Note: this is exactly the same receiving program used in Lesson #3. |
| 1. /*
2. FILE: receiver.c 3. DESCRIPTION: This is a C simpl receiver. 4. Refer to lessons 3, 4, 6, 7. 5. USAGE: receiver 6. */ 7. 8. // include required headers 9. #include <stdio.h> 10. #include <stdlib.h> 11. #include <simpl.h> 12. 13. // define possible message structures 14. typedef struct 15. { 16. int token; 17. int var1; 18. int var2; 19. int var3; 20. } MSG1; 21. 22. typedef struct 23. { 24. int token; 25. float var1; 26. } MSG2; 27. 28. typedef struct 29. { 30. int token; 31. int var1; 32. char var2[34]; 33. } MSG3; 34. 35. int main() 36. { 37. char *me = "RECIVER"; 38. char *sender; 39. char mem[1024]; 40. int n; 41. MSG1 *in1; 42. MSG2 *in2; 43. MSG3 *in3; 44. int *token; 45. 46. // perform simpl name attach 47. if (name_attach(me, NULL) == -1) 48. { 49. printf("%s: cannot attach name-%s\n", me whatsMyError()); 50. exit(-1); 51. } 52. 53. while (1) 54. { 55. // receive incoming messages 56. n = Receive(&sender, mem, 1024); 57. if (n == -1) 58. { 59. printf("%s: Receive error-%s\n", me, whatsMyError()); 60. continue; 61. } 62. 63. // set a pointer to the value of the message token 64. token = (int *)mem; 65. 66. // decide course of action based on the value of the token 67. switch (*token) 68. { 69. case 10: 70. in1 = (MSG1 *)mem; 71. Printf("token=%d var1=%d var2=%d var3=%d\n", 72. & nbsp; in1->token, 73. & nbsp; in1->var1, 74. & nbsp; in1->var2, 75. & nbsp; in1->var3); 76. break; 77. 78. case 20: 79. in2 = (MSG2 *)mem; 80. printf("token=%d var1=%d\n", 81. & nbsp; in2->token, 82. & nbsp; in2->var1); 83. break; 84. 85. case 30: 86. in3 = (MSG3 *)mem; 87. printf("token=%d var1=%d var2=%.*s\n", 88. in3->token, 89. & nbsp; in3->var1, 90. & nbsp; in3->var1, 91. & nbsp; in3->var2); 92. break; 93. 94. default: 95. printf("%s: unknown message token=%d\n", me. token); 96. } 97. 98. // reply to sender 99. Reply(sender, NULL, 0); 100. } 101. 102. return(0); 103. } |
| Lines 9-11 | include the required C header files. Note the inclusion of the simpl.h header file. |
|
Lines 14-20 | define the binary message that corresponds to token value 10. |
|
Lines 22-26 | define the binary message that corresponds to token value 20. |
|
Lines 28-33 | define the binary message that corresponds to token value 30. |
|
Line 35 | is the start of the program. |
|
Lines 37-44 | declare necessary local variables. |
|
Lines 47-51 | handle the simpl name attach. |
|
Line 53 | starts an infinite loop which awaits incoming messages. |
|
Lines 56-61 | receive the incoming messages. |
|
Line 64 | sets a pointer to the incoming message token. |
|
Lines 67-96 | deal with each message type based on the token by printing the various message components to the screen. |
| Line 99 | replies a null message to the blocked sender. This reply message could be anything at all but in the interests of simplicity, the reply message has been made null. |
The Makefile for the C receiver program. |
The receiver program must be compiled and linked before it can be of any use. Go to ../src and at the command prompt type:
make cleanThen type:
make installThis makes a new executable of receiver.c called receiver and puts it in ../bin .
Note:In order to run this Makefile, the shell variable SIMPL_HOME must be set so that the make can find the simpl header and library. |
Let's run the two programs. |
In order to run the sender_gui.py script and the C receiver program, do the following:
The command indicates:
You should see the receiver program print the contents of each message
as you choose the various message buttons displayed by the sender gui.
Summary |
Let's summarize what you should have observed in this lesson: