How common is it to decipher what type an object is when using socket connections? - sockets

I've a text game I'm making to give me some practice. I'm to the point where I'm building an overhead map that just shows terrain type for where you're located in the world. The reason I ask this is because I'll be having text and BufferedImages sent to multiple clients connected to a server via socket/serversocket. Now, would it be silly to make two completely different socket connections through different ports to split up my text replies and BufferedImage replies to the various clients, or is it pretty common to decipher what an object is before deciding what to do with it when reading from an objectinputstream? Deciphering the object type would allow me to use just one serversocket connection, I just don't know how proper/practical/good practice that is. I also don't know if sending BufferedImage objects will cause a problem in receiving the text replies from the server promptly. The BufferedImage objects would be about 31x31 pixels in size, so pretty small but I'd be sending up to 9 at a time in a standard array[] type.

It's very common. In fact, I'd go as far as to say it's required, for any non-trivial remote interaction. The only question is how you want to go about communicating type information to the program on the other side of the socket connection; there are many ways of doing that, ranging from the simple (send a type-code integer first that tells the receiver what kind of data to expect next) all the way through full-blown object-serialization protocols (things like JSON, XML, Google protocol buffers, and so on).

Related

Correct aproach to model network interaction in Enterprise Architect

I have a class Actor whose instances send/receive network messages. (E.g. each instance of that class is part of a different process running on a different physical machine.) The network messages are serialized instances of classes MessageA and MessageB whose attributes are sent over the wire. An incoming message is handled by a callback method method of my Actor class. An ougoing message is triggered by calling a method of my Actor class.
Hence, I started to model this situation in a class diagram like this:
The network messages are "signals" in EA term, i.e. classes with a special prototype (for succinctness the attributes are left out)
My Actor-class is an usual class in EA with four corresponding methods
Now, I want to model a typical interaction and started to draw the following sequence diagram:
The messages are no methods invocations, but are asynchronous and have kind "signal" which allows me to assign them the correct message type.
However, I wonder how I model
the fact that a message with payload MessageA is handled by onMessageAReceived
that method sendMessageA emits a message with payload MessageA
(Note: In terms of my implementation it is correct, that sendMessageA returns void, because sending a network message is asynchronous, offloaded to the underlying OS and the method returns to its callee after having send the message.)
in the sequence diagram.
Maybe, my whole approach is completely wrong and I am trying to model something which cannot be modeled like that. In that case some pointers to the correct approach are highly welcome.
Of course there's more than one way to model this (and it does not depend on the tool EA). So, you should ask which audience you are talking to, repsectively which their domain is basically.
Technical
A SD is well suited to show a physical transport. In that case you concentrate on the way how messages are sent. In this case you will have the physical operations shown as messages. E.g. using sockets, it would be some (a-)synchronous send(message) which assures that the content message is transported from A to B. This could be at any level of technical implementation from rough to single CRCs being sent (or how the operation is internally built to ensure packages are not lost).
Logical
In order to show a more logical aspect it's a good idea to have components (being deployed on multiple hardware) having ports (realizing some interface) along which you have an information flow (which is a connector you will find in EA) that can transport something (that is your message classes).
Overview
You might want to describe both aspects in your model. But likely you will have the focus on the one or other part depending on your overall domain.
There is no single way to model something. Models are always abstraction which is why we create models. They shall show reality, but more light weight.

Send TCP/IP message from PLC to PC using Ladder Program

Consider the following Ladder Program that checks if a connection is enabled (A202.00) then send a message from the PLC to the PC.
The documentation (Omron CX-Programmer) has a severe lack of explanation of the program convention. What I do not understand is:
To send a message from a node to a node. I should need to specify the receiver ID. It seems the function block does not have an option where I can insert an IP address. Am I supposed to MOV an IP address to a DM address (D300) then use it? If that's the case how (IP address has dots in between 4 bytes..)?
Can someone please explain what is S (First source word), D (First destination word) and C (First control word). Aren't they just memory address? E.g. sending content of a memory adress to another memory address?
[EDIT]
What am I trying to do?
I am trying to interface a measuring gauge (controlled through Ethernet by PC/C# application) to a robotic system (no RS232 or serial, no TCP/IP, only has the simplest I/O points) with an Omron PLC. When gauge completes a measurement, the C# app sends a command to the Omron PLC which, according to the command received, switch ON or OFF an output which triggers a voltage flow to the robot's I/O port.
Should I use FINS? What functions/protocol from the PLC I need to know to do this? I do not know so I am testing every function from the documentation. So far, zero progress.
1) All addressing information is encapsulated in the five control words (C -> C+4). C- "First Control Word" is the pointer to the first word in this table of five words you must have stored somewhere in your PLC to set up the communication.
2) First source word points to the first word in your PLC you wish to send. First destination word points to the first address in the PLC/device you wish to send to. In the example , the first control word specifies that 10 words should be sent. You point to the first one and it will send that one plus the next nine addresses as well.
To do this you have to use FINS communication - the PC stores a memory structure similar to the PLCs (CIO, DM, etc) called Event Memory and these are the addresses in the PC you are pointing to. The PC gets a FINS node number and address just like a PLC would - no IP addresses are involved. (see : FINS Manual) FINS is old, however, and has been superceded by things like Sysmac Gateway.
There are much better ways of communicating between PLC/PC, however, depending on what you are trying to do. Are you trying to write an HMI? If so, what language are you using?
Edit :
If you're using C#, I highly recommend you look into Sysmac Gateway and CX-Compolet. This is probably the most flexible, simple, and extensible way to get .NET working with Omron PLCs. If it is at all possible, however, a better way might even be to have the measurement unit communicate directly with the PLC via hardware I/O (relays, DIO, etc).
CX-Compolet, Sysmac Gateway link:
http://www.ia.omron.com/product/family/63/index_l_u.html

Socket protocol specification for transferring keystrokes?

Is there an existing socket protocol for defining how a way to transfer keystroke data across machines? I want to be able to type on one machine and have what I type there show up on another machine.
For instance, the protocol might the field (eg. that the data is for a keystroke), data type (keystroke), and a value (keystroke value).
One thing that you could do is look into messaging. I am not sure if this will help you that much but, there are a lot of different implementations of this but a good place to start is looking at the JMS API.

why is it not possible to use mmap with socket fd as an argument?

i know it's not possible, i'm trying to understand the true reason behind it OS wise
Because the concept of a socket simply doesn't map to the concept of a random-access in-memory array, which is the abstraction which mmap gives you. A file on a block-device (disk) usually allows random read/write access. This maps nicely to an in-memory contiguous array, which also gives you random read/write access.
A socket, however, is usually stream (or packet/datagram) oriented. Meaning, a stream of data gets sent over the socket, and a stream of data is received from the socket. But you can't, for example, write/read to the Nth byte of an open socket stream - that simply doesn't make any sense conceptually.
In fact it is possible with some protocol families in linux, namely:
PF_NETLINK
PF_PACKET
For the rest of protocols mmapping is not implemented/possible. For example PF_INET
The mmap system call for socket gets dispatched here
See also:
Documentation/networking/packet_mmap.txt
There is whole thing called packet mmap. Search on google. There is program example in elixir maintained and doc page with example. Just search
Program link in kernel source maintained to view
https://elixir.bootlin.com/linux/v3.14.32/source/tools/testing/selftests/net
Tutorial
https://www.kernel.org/doc/html/latest/networking/packet_mmap.html
What is it. Use socket fd with mmap. So mapping socket to rx and tx descriptor in device driver.
To understand more about device descriptors u can look at this code https://github.com/torvalds/linux/blob/master/drivers/net/ethernet/realtek/r8169_main.c in case anyone is are curious and stumble upon this page and linux device drivers pdf

GWT : Type of Container

I see that there are two ways of transferring objects from server to client
Use the same domain object (Contact.java) as used in the service layer. (I do not use hibernate)
Use the HashMap to send the domain object field values in the form of
Map with the help of BeanUtilsBean class. For multiple objects, use
the List>. Similary, use the Map to submit form
values from client to server
Is there any performance advantage for option 1 over 2?.
Is there a way to hide the classname/package name that is sent to the browser if we
use option 1?.
thanks!.
You have to understand that whatever option you choose, it will need to get converted to JavaScript (+ some wrappers, etc.) - this stuff takes more time and space/bandwidth (note: I haven't done any benchmarks, this is just a [reasonable] conclusion I came up with ;)) than, say, JSON. But if you used JSON, you have to recreate the object on the server side, os it's not a silver bullet. In the end, it all depends how much performance is of an issue to you - for more insight, see this question.
I'd go with option 1: just leave it to the GWT team to pack your domain objects and transfer them between client and server. In the future (GWT 2.1), we'll have some really nice things, including a more lightweight transfer protocol - see this years presentation from Google I/O on architecting GWT apps - it's something worth keeping in mind.
PS: It's always good to do benchmarks yourself in this kind of situations - your configuration, the type of objects, etc. might yield some different results than expected.