etisserant@203: #!/usr/bin/env python etisserant@203: # -*- coding: utf-8 -*- etisserant@203: etisserant@203: #This file is part of Beremiz, a Integrated Development Environment for etisserant@203: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. etisserant@203: # etisserant@203: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@203: # etisserant@203: #See COPYING file for copyrights details. etisserant@203: # etisserant@203: #This library is free software; you can redistribute it and/or etisserant@203: #modify it under the terms of the GNU General Public etisserant@203: #License as published by the Free Software Foundation; either etisserant@203: #version 2.1 of the License, or (at your option) any later version. etisserant@203: # etisserant@203: #This library is distributed in the hope that it will be useful, etisserant@203: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@203: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@203: #General Public License for more details. etisserant@203: # etisserant@203: #You should have received a copy of the GNU General Public etisserant@203: #License along with this library; if not, write to the Free Software etisserant@203: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@203: etisserant@203: import os, sys, getopt, socket etisserant@203: etisserant@203: def usage(): greg@206: print """ greg@206: Usage of Beremiz PLC execution service :\n greg@206: %s {[-a ip] [-d path] [-p port]|-h|--help} greg@206: -a, --address - authorized ip to connect (x.x.x.x) greg@206: -d, --directory path - set the working directory greg@206: -p, --port port number - set the port number greg@206: -h, --help - print this help text and quit greg@206: """%sys.argv[0] etisserant@203: etisserant@203: try: greg@206: opts, args = getopt.getopt(sys.argv[1:], "a:d:p:h", ["directory=", "port=", "help"]) greg@206: except getopt.GetoptError, err: etisserant@203: # print help information and exit: greg@206: print str(err) # will print something like "option -a not recognized" etisserant@203: usage() etisserant@203: sys.exit(2) etisserant@203: greg@206: # default values greg@206: WorkingDir = os.getcwd() greg@206: ip = "" greg@206: port = 3000 greg@206: etisserant@203: for o, a in opts: etisserant@203: if o in ("-h", "--help"): etisserant@203: usage() etisserant@203: sys.exit() greg@206: elif o in ("-a", "--address"): greg@206: if len(a.split(".")) == 4 or a == "localhost": greg@206: ip = a greg@206: elif o in ("-d", "--directory"): greg@206: # overwrite default working directory greg@206: WorkingDir = a greg@206: elif o in ("-p", "--port"): greg@206: # port: port that the service runs on greg@206: port = int(a) greg@206: else: greg@206: usage() greg@206: sys.exit() etisserant@203: etisserant@203: from runtime import PLCObject, ServicePublisher etisserant@203: import Pyro.core as pyro etisserant@203: etisserant@203: if not os.path.isdir(WorkingDir): etisserant@203: os.mkdir(WorkingDir) etisserant@203: etisserant@203: # type: fully qualified service type name etisserant@203: type = '_PYRO._tcp.local.' etisserant@203: # name: fully qualified service name etisserant@203: name = 'First test.%s'%(type) etisserant@203: # address: IP address as unsigned short, network byte order etisserant@203: etisserant@203: def gethostaddr(dst = '224.0.1.41'): etisserant@203: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) etisserant@203: try: etisserant@203: s.connect((dst, 7)) etisserant@203: (host, port) = s.getsockname() etisserant@203: s.close() etisserant@203: if host != '0.0.0.0': etisserant@203: return host etisserant@203: except error: etisserant@203: pass etisserant@203: return socket.gethostbyname(socket.gethostname()) etisserant@203: etisserant@203: # properties: dictionary of properties (or a string holding the bytes for the text field) etisserant@203: serviceproperties = {'description':'Remote control for PLC'} etisserant@203: etisserant@203: pyro.initServer() etisserant@203: daemon=pyro.Daemon(host=ip, port=port) etisserant@203: uri = daemon.connect(PLCObject(WorkingDir, daemon),"PLCObject") greg@207: etisserant@203: print "The daemon runs on port :",daemon.port etisserant@203: print "The object's uri is :",uri etisserant@203: print "The working directory :",WorkingDir etisserant@203: etisserant@203: # Configure and publish service greg@207: # Not publish service if localhost in address params greg@207: print ip greg@207: if ip != "localhost" and ip != "127.0.0.1": greg@207: # No ip params -> get host ip greg@207: if ip == "": greg@207: ip_32b = socket.inet_aton(gethostaddr(ip)) greg@207: else: greg@207: ip_32b = ip greg@207: print "Publish service on local network" greg@207: service = ServicePublisher.PublishService() greg@207: service.ConfigureService(type, name, ip_32b, port, serviceproperties) greg@207: service.PublishService() etisserant@203: etisserant@203: daemon.requestLoop()