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@217: import os, sys, getopt etisserant@203: etisserant@203: def usage(): greg@206: print """ greg@206: Usage of Beremiz PLC execution service :\n etisserant@217: %s {[-n name] [-i ip] [-p port]|-h|--help} working_dir etisserant@217: -n - zeroconf service name etisserant@217: -i - ip of interface to bind to (x.x.x.x) etisserant@217: -p - port number etisserant@217: -h - print this help text and quit etisserant@217: etisserant@217: working_dir - directory where are stored PLC files greg@206: """%sys.argv[0] etisserant@203: etisserant@203: try: etisserant@217: opts, args = getopt.getopt(sys.argv[1:], "i:p:n:h") 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: ip = "" greg@206: port = 3000 etisserant@217: name = os.environ[{ etisserant@217: "linux2":"USER", etisserant@217: "win32":"USERNAME", etisserant@217: }.get(sys.platform, "USER")] etisserant@217: etisserant@203: for o, a in opts: etisserant@217: if o == "-h": etisserant@203: usage() etisserant@203: sys.exit() etisserant@217: elif o == "-i": greg@206: if len(a.split(".")) == 4 or a == "localhost": greg@206: ip = a etisserant@217: elif o == "-p": greg@206: # port: port that the service runs on greg@206: port = int(a) etisserant@217: elif o == "-n": etisserant@217: name = a greg@206: else: greg@206: usage() greg@206: sys.exit() etisserant@203: etisserant@210: if len(args) > 1: etisserant@210: usage() etisserant@210: sys.exit() etisserant@210: elif len(args) == 1: etisserant@210: WorkingDir = args[0] etisserant@210: elif len(args) == 0: etisserant@210: WorkingDir = os.getcwd() etisserant@217: args=[WorkingDir] etisserant@210: 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: etisserant@203: pyro.initServer() etisserant@203: daemon=pyro.Daemon(host=ip, port=port) etisserant@210: uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"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: if ip != "localhost" and ip != "127.0.0.1": greg@207: print "Publish service on local network" etisserant@217: service = ServicePublisher.ServicePublisher(name, ip, port) etisserant@217: etisserant@217: sys.stdout.flush() etisserant@203: etisserant@203: daemon.requestLoop()