PDA

View Full Version : vizard and java -> jython?


Johannes
06-03-2005, 12:24 PM
Hi,

I'm thinking about using a Java program and Vizard together in one application.

The result should bea split screen: On the top the java application that simulates a physics simulation conceptually (e.g. mass, force, acceleration, slope of airtrack etc.) and in the bottom window vizard (a python application) that takes certain values out of the java application and shows the realistic looking result.

E.g. I change the slope in the Java application and see that the slope of the airtrack changes in my simulation created with Vizard.

Questions:

0. Does anyone have experience with that? Examples?
1. Is that possible?
2. What first steps would you recommend?

What about jython? Which version of python does Vizard need? As the speed of Jython is tied very tightly to the speed of the underlying JVM it is about 2 times slower than python which might lead to a problem....

An advantage if vizard works on jython would be that it would work on any operating system.....

Johannes

farshizzo
06-03-2005, 01:49 PM
Hi,

Vizard is written entirely in C/C++. So it would not be compatible with Jython or Java. The best thing you can do is have your Java application manually start up Vizard and communicate with it through some form of IPC (Interprocess Communication). Why do you need to use Java? Are you using it for a GUI or calculations?

Johannes
06-03-2005, 03:06 PM
Vizard is written entirely in C/C++. So it would not be compatible with Jython or Java. The best thing you can do is have your Java application manually start up Vizard and communicate with it through some form of IPC (Interprocess Communication). Why do you need to use Java? Are you using it for a GUI or calculations?

Mmmh, IPC... What about the Java Native Interface...


The Java Native Interface (JNI) is the native programming interface for Java.
The JNI allows Java code that runs within a Java Virtual Machine (VM) to operate with applications and libraries written in other languages, such as C, C++, and assembly. In addition, the Invocation API allows you to embed the Java Virtual Machine into your native applications.


Why do you need to use Java? Are you using it for a GUI or calculations?

@Why? I have a Java application that I want to use to model and show the conceptual model.

@ GUI: so it is using a GUI and calculations.

Johannes

farshizzo
06-10-2005, 03:06 PM
Hi,

Have you considered using sockets to communicate between your Java program and Vizard? Sockets are very simple to use in Python. I'm not sure how easy it is in Java. Let me know if you want some sample Python code for socket communication.

Johannes
06-10-2005, 03:52 PM
If you have some sample Python code for socket communication close by, I would like to see / play with it on the weekend. If it is a little work for you to get it I might just do some "research" myself. Thank's a lot for the suggestion, this gives me a starting point...

Johannes

farshizzo
06-10-2005, 04:15 PM
Hi,

Here is a sample script that sends and receives data over a socket. Press spacebar to send a message. When a message is received it will be printed out.import viz
viz.go()

import socket

#The maximum amount of data to receive at a time
MAX_DATA_SIZE = 1024

#The port to send/receive data on
PORT = 4999

#Get the name of this computer
COMPUTER_NAME = socket.gethostname()

#Get the IP address of this computer
COMPUTER_IP_ADDRESS = socket.gethostbyname(COMPUTER_NAME)

#Create a socket to send data over
OutSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
OutSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

#Create a socket to receive data from
InSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
InSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
InSocket.bind(('', PORT))
InSocket.setblocking(0)


def SendData(data):
OutSocket.sendto(data,(COMPUTER_IP_ADDRESS,PORT))

def ReceiveData():
try:
return InSocket.recv(MAX_DATA_SIZE)
except:
pass

def onkeydown(key):
if key == ' ':
#Send data over the socket
SendData('hello there')

viz.callback(viz.KEYDOWN_EVENT,onkeydown)


def ontimer(num):
#Try to receive data from socket
data = ReceiveData()
if data:
print 'Received Message:',data

viz.callback(viz.TIMER_EVENT,ontimer)
viz.starttimer(0,0,viz.FOREVER)

Johannes
06-21-2005, 02:19 PM
Hi,
funny thing happened. Tried the above posted code and now Vizard does not start any program (vizard starts but when I press run it does only give me the below error) any more. What to do?

Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\Program Files\Vizard25\viz.py", line 13, in ?
import socket
File ".\socket.py", line 4, in ?
viz.go()
AttributeError: 'module' object has no attribute 'go'
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\Program Files\Vizard25\vizact.py", line 188, in ?
class VizGotoAction(viz.ActionClass):
AttributeError: 'module' object has no attribute 'ActionClass'
Traceback (most recent call last):
File "<string>", line 3, in ?
NameError: name 'viz' is not defined
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name 'viz' is not defined
in ?
import vizinfo
File "C:\Program Files\Vizard25\python\vizinfo.py", line 22, in ?
_TEXTALIGN = [viz.TEXT_RIGHT_TOP,viz.TEXT_LEFT_TOP,viz.TEXT_RIGH T_TOP,viz.TEXT_LEFT_TOP]
AttributeError: 'module' object has no attribute 'TEXT_RIGHT_TOP'

farshizzo
06-21-2005, 02:55 PM
Hi,

It looks like you created a file called socket.py. This is overriding the standard socket module that comes with python. This causes an error within viz.py, which will prevent it from being imported. Rename this file to something else and it should work.

Johannes
06-21-2005, 04:01 PM
yes, renaming and deleting socket.py and another one helped, thank you! It is working again.
At the first try communicating with it from java it did not work, but I will try again sometimes in the next few days, and report you.
Johannes

Johannes
06-27-2005, 10:36 PM
One question and a rather unstructured protocol of this night:

1) if I do
host = "localhost"
addr = (host,PORT)
InSocket.bind(addr)

should it not bind the socket to localhost?

Should I not be able to access it by
telnet localhost 49213

Where Port is 49213

I also tried the current IP-Adress e.g.
telnet 169.254.148.117 49213

--------------


The following is more like a protocol, maybe not very useful...

Tried a lot around to get a connection between Java and your Socket.


Port is 49213


Why do I not get a reply when I do


telnet 169.254.148.117 49213


Other Printouts:

COMPUTER_NAME OGL1
COMPUTER_NAME OGL1COMPUTER_IP_ADDRESS 169.254.148.117

I also tried (offline and online)
telnet localhost 49213

Only get connection failed.


From another Python Client I receive date: e.g.


from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
data = raw_input('>> ')
if not data:
break
else:
if(UDPSock.sendto(data,addr)):
print "Sending message '",data,"'.....<done>"

# Close socket
UDPSock.close()


From Java it works if I use
try
{
DatagramSocket sock = new DatagramSocket();
InetAddress server = InetAddress.getByName(host);
if (timeout > 0)
sock.setSoTimeout(timeout * 1000); // millisec
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out, true);
String line;
while ((line = "hallo12") != null)
{
byte[] req = line.getBytes(), ans = new byte[1024];
DatagramPacket r = new DatagramPacket(req, req.length, server, port), a = new DatagramPacket(ans, ans.length);
....

but normal Client-Sockets do not work e.g.
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
InetAddress server_name = null;

try
{
//InetAddress server_name=localhost.byN;
//String COMPUTER_NAME = ;
String host = "localhost";
InetAddress server = InetAddress.getByName(host);




kkSocket = new Socket(server, 21567);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));

farshizzo
06-28-2005, 10:23 AM
Hi,

I believe telnet uses TCP sockets for communication, therefore you can't use it to communicate with your program. The sample code I gave you uses UDP (Datagram) sockets. This is easier to use than TCP and should work fine when communicating over the same computer. I've never done socket programming in Java so I can't really help you that much.

Also, make sure you enable the reuse of an address by using the following command:InSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)You should also do this within Java.