-
Notifications
You must be signed in to change notification settings - Fork 0
Implement the socket communication #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
print 'Socket created on:', port | ||
|
||
data = client_socket.recv(512) | ||
print "RECIEVED:" , data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "received"
moreover, this print won't work with python 3, use print("RECEIVED: %s" % data) instead (if data is a string).
import socket | ||
import sys | ||
|
||
port = 5001 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- there is a trailing whitespace at the line of this line. Use git diff before commit and/or git show before push. Trailing whitespaces are marked with a "red rectangle".
- Use capitalized constants. For example, PORT = 5001
try: | ||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
client_socket.connect(("localhost", port)) | ||
except socket.error, msg: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing whitespace
port = 5001 | ||
|
||
def main(): | ||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not indent with tab. Use the same indentation as used in other python scripts (eg. tools/build.py uses 4 spaces)
@@ -0,0 +1,22 @@ | |||
import socket |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add #!/usr/bin/env python to the first line
- License header is mssing
|
||
extern int remote_init(void); | ||
extern void connection_closed(void); | ||
extern void send_to_client(const char* data, uint16_t data_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possible style issue: * goes to the right side: const char *data
def main(): | ||
try: | ||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
client_socket.connect(("localhost", port)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"localhost" should be also stored in a constant. For example: HOST = "localhost"
@@ -23,6 +23,10 @@ | |||
#include "jerry-port.h" | |||
#include "jerry-port-default.h" | |||
|
|||
#ifdef JERRY_DEBUGGER | |||
#include "jerry-debugger.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possible style issue: do we indent includes inside a guard?
@@ -343,6 +354,11 @@ main (int argc, | |||
int i; | |||
int files_counter = 0; | |||
|
|||
#ifdef JERRY_DEBUGGER | |||
/* Initialize the socket connection */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment seems to be misindented
@@ -673,6 +689,11 @@ main (int argc, | |||
jerry_release_value (ret_value); | |||
jerry_cleanup (); | |||
|
|||
#ifdef JERRY_DEBUGGER | |||
/* Need to close the socket at the end of the program. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment seems to be misindented
Please add a bit more description about the patch. |
ssize_t byte_send; /**< size of byte_send */ | ||
size_t data_len; /**< data lenght */ | ||
bool optval = true; /**< the arguments optval and optlen are used to access option values for setsockopt() */ | ||
char* data[BUFFER_SIZE]; /**< data buffer */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just char not char*. We are not string pointers here, only bytes. The current definition is 128 pointers which points to chars.
|
||
/** | ||
* Create an endpoint for communication | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single line is enough for such comments.
1ccd9b4
to
02189c0
Compare
#include "jerry-port.h" | ||
|
||
#define PORT 5001 | ||
#define BLACKLOG 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is still not fixed
#define BLACKLOG 1 | ||
#define BUFFER_SIZE 128 | ||
|
||
int sock; /**< return value of socket(), used other methods */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment is still not clear. Focus on the function of this variable: why is it used for? what is this good for?
For example, "socket file descriptor for the remote communication"
connected = accept (sock, (struct sockaddr *)&client_addr, &sin_size); | ||
|
||
jerry_port_log (JERRY_LOG_LEVEL_DEBUG,"Connected from: %s:%d\n", | ||
inet_ntoa (client_addr.sin_addr),ntohs (client_addr.sin_port)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: spaces
#define BUFFER_SIZE 128 | ||
|
||
int sock; /**< return value of socket(), used other methods */ | ||
int connected; /**< return value of the whole socket connection */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the file descriptor of the connected socket, see: http://man7.org/linux/man-pages/man2/accept.2.html
Rename the variable and rephrase the comment to make it more descriptive.
|
||
int sock; /**< return value of socket(), used other methods */ | ||
int connected; /**< return value of the whole socket connection */ | ||
ssize_t byte_send; /**< size of byte_send */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this global?
int sock; /**< return value of socket(), used other methods */ | ||
int connected; /**< return value of the whole socket connection */ | ||
ssize_t byte_send; /**< size of byte_send */ | ||
size_t data_len; /**< data lenght */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this global?
Moreover, you have a function parameter with the same name (but different type) and it is very misleading.
int connected; /**< return value of the whole socket connection */ | ||
ssize_t byte_send; /**< size of byte_send */ | ||
size_t data_len; /**< data lenght */ | ||
bool optval = true; /**< the arguments optval and optlen are used to access option values for setsockopt() */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems nobody use this: it is set by the setsockopt but the result is not used. I think it is not needed to be global for now.
ssize_t byte_send; /**< size of byte_send */ | ||
size_t data_len; /**< data lenght */ | ||
bool optval = true; /**< the arguments optval and optlen are used to access option values for setsockopt() */ | ||
char data[BUFFER_SIZE]; /**< data buffer */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as the data_len
|
||
extern bool remote_init(void); | ||
extern void connection_closed(void); | ||
extern void send_to_client(const char *data, uint16_t data_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space is required before (
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
*/ | ||
if (byte_send != (ssize_t) data_len) | ||
{ | ||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error, there is some missing package...\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a byte_send
which has the return value of send ( on success, these calls return the number of bytes sent ). I check the data_len
with this, and if the two value is not equal there is some missing bytes. Maybe I need to add to check the -1 ( error ), what do you think?
|
||
|
||
/* Send the parsed file names to the client side */ | ||
void send_to_client (const char *data, uint16_t data_len) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only data_len is needed since we have only one sender buffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it.
|
||
/* Server adress declaration */ | ||
server_addr.sin_family = AF_INET; /**< the address family is host by order*/ | ||
server_addr.sin_port = htons (PORT); /**< host to network long (PORT) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment misaligned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
b4fddb4
to
f6df650
Compare
server_addr.sin_family = AF_INET; | ||
server_addr.sin_port = htons (PORT); | ||
server_addr.sin_addr.s_addr = INADDR_ANY; | ||
bzero (&(server_addr.sin_zero), BACKLOG); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this exactly doing?
int connected; /**< hold the file descriptor for the accepted socket */ | ||
|
||
struct sockaddr_in server_addr, client_addr; /**< declarations of the socket address */ | ||
socklen_t sin_size = sizeof (struct sockaddr_in); /**< size of the structure pointed by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to create a non-static non-const variable for this? I think we can simply use sizeof (struct sockaddr_in) when needed.
#define BACKLOG 1 | ||
|
||
int sock; /**< socket file descriptor for the remote communication */ | ||
int connected; /**< hold the file descriptor for the accepted socket */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be static. Perhaps they could even be part of the global context but I am not sure.
int sock; /**< socket file descriptor for the remote communication */ | ||
int connected; /**< hold the file descriptor for the accepted socket */ | ||
|
||
struct sockaddr_in server_addr, client_addr; /**< declarations of the socket address */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these could be local variables of main().
/* Set the options on socket */ | ||
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int)) == -1) | ||
{ | ||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Erorr: %s\n", strerror (errno)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error
{ | ||
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "TCPServer connection closed on port: %d\n", PORT); | ||
close (connected); | ||
close (sock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we support only one connection I think there is no point to keep the listening socket.
Also connected should be renamed something else, such es jerry_debugger_socket
* information is sent to the client. */ | ||
else if (byte_send != (ssize_t) data_len) | ||
{ | ||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Missing bytes from the pacakge.\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package.
Ot just say: Error: transmission error.
Btw, I think a loop would be better here to send the remaining bytes. Only -1 is a true error, partial transmission could be resumed.
|
||
extern bool remote_init (void); | ||
extern void connection_closed (void); | ||
extern void send_to_client (uint16_t data_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be size_t instead of uint16_t. Both are one register long.
c911b08
to
1e5938f
Compare
1e5938f
to
7b15eb2
Compare
@@ -21,6 +21,12 @@ | |||
|
|||
#define MAX_BUFFER_SIZE 128 | |||
|
|||
static uint8_t jerry_debugger_buffer[MAX_BUFFER_SIZE]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid duplicate definitions this should only be an extern definition:
extern uint8_t jerry_debugger_buffer[MAX_BUFFER_SIZE];
In the C file you should create the variable:
uint8_t jerry_debugger_buffer[MAX_BUFFER_SIZE];
Also comment about the variable is mission.
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
c27588b
to
a21433f
Compare
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
Implement the socket communication server side in ANSI C. Add a new debugger folder in jerry-core for the debugger files. We need the socket communication between the client and the server to exchange debugger's data.
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu