Skip to content

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

Merged
merged 1 commit into from
Nov 23, 2016
Merged

Conversation

polaroi8d
Copy link
Owner

@polaroi8d polaroi8d commented Nov 2, 2016

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.

  • Implement a simple send function
  • The backlog argument defines the maximum length to which the queue of pending connections for sock may grow.
  • jerry_debugger_socket_init() for initialize the communication
  • jerry_debugger_connection_end() to close the socket

JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu

print 'Socket created on:', port

data = client_socket.recv(512)
print "RECIEVED:" , data
Copy link

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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".
  2. 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:
Copy link

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:
Copy link

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Add #!/usr/bin/env python to the first line
  2. 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);
Copy link

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))
Copy link

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"
Copy link

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 */
Copy link

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. */
Copy link

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

@zherczeg
Copy link

zherczeg commented Nov 7, 2016

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 */
Copy link

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
*/
Copy link

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.

@polaroi8d polaroi8d force-pushed the socket-client branch 2 times, most recently from 1ccd9b4 to 02189c0 Compare November 9, 2016 07:50
#include "jerry-port.h"

#define PORT 5001
#define BLACKLOG 1
Copy link

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 */
Copy link

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));
Copy link

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 */
Copy link

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 */
Copy link

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 */
Copy link

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() */
Copy link

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 */
Copy link

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space is required before (

Copy link
Owner Author

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");

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.

Copy link
Owner Author

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)

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.

Copy link
Owner Author

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) */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment misaligned.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@polaroi8d polaroi8d force-pushed the socket-client branch 3 times, most recently from b4fddb4 to f6df650 Compare November 17, 2016 12:47
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);

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

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 */

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 */

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));

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);

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");

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);

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.

@polaroi8d polaroi8d force-pushed the socket-client branch 2 times, most recently from c911b08 to 1e5938f Compare November 18, 2016 07:08
@polaroi8d polaroi8d closed this Nov 18, 2016
@polaroi8d polaroi8d reopened this Nov 18, 2016
@@ -21,6 +21,12 @@

#define MAX_BUFFER_SIZE 128

static uint8_t jerry_debugger_buffer[MAX_BUFFER_SIZE];

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
@polaroi8d polaroi8d merged commit 7a36672 into remote-debugger Nov 23, 2016
@polaroi8d polaroi8d deleted the socket-client branch January 6, 2017 08:49
polaroi8d pushed a commit that referenced this pull request Jan 18, 2017
JerryScript-DCO-1.0-Signed-off-by: Levente Orban orbanl@inf.u-szeged.hu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants