Hi James and all,

Pyorbit mail list seems inactive for long time, so I post my question here. Hope it is right.

I want to use python to write a gnome panel applet, and comminicate with a corba server in java. So pyorbit is used.

PYORBIT/ORBIT/IIOP

add_listener(objref)
[gnome panel applet (in python)] --------------------------> [corba server (java)]
<-------------------------
listener.notify()



python client first get IOR of server, then call the add_listener(objref) method of corba server. The
parameter objref is reference to a servent in python. (Now the python applet is actually corba server)
Then corba server can call listener's notify().


For test, In java server side, I start a thread and listener.notify() python applet in about 5 seconds. But the python
applet segment fault! But if I just call listener.notify() in add_listener's implementation (java), it will always succeed!


I have struggle for 2 days on this problem. Attached full source for help,

Thanks!
York


#!/usr/bin/env python
import pygtk
pygtk.require('2.0')

import gtk
import gtk.glade
import gnome.applet
import ORBit
import bonobo
import gobject
import threading
import time


ORBit.load_file('/tmp/Calendar.idl')
import Calendar

import Calendar__POA

class myListener (Calendar__POA.Listener):

    def notify_me(self, event):
        print "Notify called\n"
        print event.summary
        pass

class CalClient:
    orb = bonobo.orb()
    filename = "/tmp/java-server-ior"
    ior = file(filename, 'r').read()
    objref = orb.string_to_object(ior)._narrow(Calendar.GetEvents)

    def __init__(self):
        pass

    def get_events(self, time_from, time_to):
        print "in get_events" 

        year,month,day,hour,minute,second = time_from

        f = Calendar.Time(month, day, year, hour, minute)

        year,month,day,hour,minute,second = time_to
        t = Calendar.Time(month, day, year, hour, minute)

        # Fetch from corba server
        events = self.objref.get_events(f, t)

        return events

    def test_seq (self):
        seq = self.objref.test_seq()
        return seq

    def add_listener (self, l):
        self.objref.add_listener(l)


cclient = CalClient()

def append_model(events):
    for s in events:
        iter = model.append()
        model.set_value(iter, 0, s.summary)

        mydate = monthmap[s.start.month] +  (" %d" % (s.start.day)).zfill(2) + " 
%d:%d" % (s.start.hh, s.start.mm)

        model.set_value(iter, 1, mydate)
        model.set_value(iter, 2, s.summary)

def on_date_changed():
    pass

monthmap = {0:"Sorry", 1:"Jan", 2:"Feb",3:"Mar", 4:"Apr", 5:"May", 6:"Jun", 7:"Jul", 
8:"Aug", 9:"Sep", 10:"Oct",11:"Nov", 12:"Dec"}

def button_press_cb(*args):

    xml_fname = "/tmp/demo.glade"
    xml = gtk.glade.XML(xml_fname)

    tree = xml.get_widget("treeview1")

    tree.set_model(model)

    # fill model with today's event.
    cal = xml.get_widget("calendar1")

    # Fetch Today's Event
    mydate = cal.get_date ()

    current = time.time() 
    year,month,day,hour,minute,second,wday,yday,isdst = time.gmtime(current)

    time_from = year,month,day,0,0,0
    time_to = year,month,day,23,59,59

    events = cclient.get_events(time_from,time_to)

    append_model(events)

    cell = gtk.CellRendererText()
    column = gtk.TreeViewColumn("Summary", cell, text=0)
    tree.append_column(column)

    cell = gtk.CellRendererText()
    column = gtk.TreeViewColumn("Date Start", cell, text=1)
    tree.append_column(column)

    cell = gtk.CellRendererText()
    column = gtk.TreeViewColumn("Date End", cell, text=1)
    tree.append_column(column)

    xml.signal_autoconnect(locals())


model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)

# Init last time
last_time = time.time()

def update_events(args):
    print "in run loop inited"
    global last_time

    current_time = time.time()
    from_time = time.gmtime(last_time)
    year,month,day,hour,minute,second,wday,yday,isdst =  time.gmtime(last_time)
    from_time = year, month, day, hour, minute,second
    #to_time = time.gmtime(current_time)
    year,month,day,hour,minute,second,wday,yday,isdst =  time.gmtime(current_time)
    to_time = year, month, day, hour, minute,second
    events = cclient.get_events(from_time, to_time)

    last_time = current_time 

    # Update the label
    label = time.ctime(current_time)
    button.set_label(label)
    return gtk.TRUE

def sample_factory(applet, iid):
    global button

    label = time.ctime(time.time())
    button = gtk.Button(label)
    button.connect ('clicked', button_press_cb)
    applet.add(button)
    applet.show_all()

    servant = myListener()

    poa = bonobo.poa()
    poa.activate_object (servant)

    #file('/tmp/iorfile', 'w').write(orb.object_to_string(objref))

    #cclient.add_listener(servant)
    global myobjref
    myobjref = poa.servant_to_reference(servant)

    cclient.add_listener(myobjref)

    # Start the thread that checking server
    gtk.timeout_add(15000, update_events, 0)
    poa._get_the_POAManager().activate()

    return gtk.TRUE

gnome.applet.bonobo_factory("OAFIID:GNOME_PythonAppletSample_Factory", 
                             gnome.applet.Applet.__gtype__, 
                             "glow-applet", "0", sample_factory)
module Calendar
{

  struct Time {
    short month, day, year;
    short hh, mm;
  };
  
  struct Event {
    string summary;
    Time start;
    Time end;
  };

  typedef sequence <Event> EvtSeq;
  typedef sequence <short> ShortSeq;

  interface Hello
  {
    string sayHello();  // This line is the operation statement.
    oneway void shutdown();
  };

  interface Listener
  {
     void notify_me(in Event e);
  };
 
  interface GetEvents
  {
    EvtSeq get_events (in Time from, in Time to);
    ShortSeq test_seq ();
    void add_listener (in Listener l);
  };
};
// Copyright and License 
import Calendar.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import java.util.Properties;
import java.io.*;
import java.util.Vector;

import java.util.Timer;
import java.util.TimerTask;



class GetEventsImpl extends GetEventsPOA{
  private ORB orb;
  private Vector ls = new Vector();
  private Listener lis;

  final Calendar.Event[] storage = {
    new Calendar.Event("Server1", 
               new Calendar.Time((short)1,(short)2,(short)3,(short)4,(short)5),
               new Calendar.Time((short)0,(short)0,(short)0,(short)0,(short)0)),
    new Calendar.Event("Server2", 
               new Calendar.Time((short)0,(short)0,(short)0,(short)0, (short)0),
               new Calendar.Time((short)0,(short)0,(short)0,(short)0, (short)0)),
    new Calendar.Event("Server3", 
               new Calendar.Time((short)0,(short)0,(short)0,(short)0, (short)0),
               new Calendar.Time((short)0,(short)0,(short)0,(short)0, (short)0)),
    new Calendar.Event("Today event", 
               new Calendar.Time((short)1,(short)5,(short)2004,(short)6, (short)0),
               new Calendar.Time((short)1,(short)5,(short)2004,(short)6, (short)2)),
    new Calendar.Event("Today event", 
               new Calendar.Time((short)1,(short)6,(short)2004,(short)6, (short)0),
               new Calendar.Time((short)1,(short)6,(short)2004,(short)6, (short)2)),
    };

  public void setORB(ORB orb_val){
    orb = orb_val;
  }

  public Listener get_listener() {
      return lis;
  }

   // true if a < b
   private boolean smaller (Calendar.Time a, Calendar.Time b) {
 
      System.out.println (a.year + " " + b.year); 

      if (a.year < b.year) return true;

      if (a.year == b.year) {
        System.out.println (a.month + " " + b.month); 
        if (a.month < b.month) return true;
        if (a.month == b.month) {
          System.out.println (a.day + " " + b.day); 
          if (a.day < b.day) return true;
          if (a.day == b.day) {
            System.out.println (a.hh + " " + b.hh); 
            if (a.hh < b.hh) return true;
            if (a.hh == b.hh) {
              System.out.println (a.mm + " " + b.mm); 
              if (a.mm < b.mm) return true;
            }
          }
        }
      }

      System.out.println ("return false");
      return false;
   }

   // true if a = b
   private boolean equal (Calendar.Time a, Calendar.Time b) {
        if ((a.year == b.year) &&
            (a.month == b.month) &&
            (a.day == b.day) &&
            (a.hh == b.hh) &&
            (a.mm == b.mm))
          return true;

        return false;
   }

  public Calendar.Event[] get_events (Calendar.Time from, Calendar.Time to) {
    int i;
    Vector v;
    Calendar.Event[] ret;

    System.out.println("get_events called from " + from.year + " " + from.month + " " 
+ from.day + " " + from.hh + " " +  from.mm);
    System.out.println("get_events called to " + to.year + " " + to.month + " " + 
to.day + " " + to.hh + " " +  to.mm);


    v = new Vector();

    for (i = 0; i < storage.length; i ++) {
       System.out.println (i);
       System.out.println("equal");
       if (equal (storage[i].start, from)) {
          v.add (storage[i]);
          continue;
       }

       System.out.println("equal");
       if (equal (storage[i].end, to)) {
          v.add (storage[i]);
          continue;
       }

       System.out.println("smaller from");
       if (smaller (storage[i].start, to) && smaller(from, storage[i].start)) {
          v.add (storage[i]);
          continue;
       }

       System.out.println("smaller to");
       if (smaller (storage[i].end, to) && smaller(to, storage[i].end)) {
          v.add (storage[i]);
          continue;
       }
    }


    ret = new Calendar.Event[v.size()];

    v.toArray(ret);

    return ret;
  }


  public short[] test_seq () {
    short[] tmp = {(short) 3 , (short)4, (short)5};
    return tmp;
  }


  public void add_listener (Calendar.Listener l) {

    System.out.println ("add listener called.");
    lis = l;

    lis.notify_me(storage[0]);

        /*
    for (int i = 0 ; i < 10000; i++) {
      System.out.println ("loop" + i);
      lis.notify_me(storage[0]);
      lis.notify_me(storage[0]);
      lis.notify_me(storage[0]);
    }
        */
  }
  
  public void shutdown(){
    orb.shutdown(false);
  }
}

class HelloImpl extends HelloPOA{
  private ORB orb;
                                                                                
  public void setORB(ORB orb_val){
    orb = orb_val;
  }
                                                                                
  public String sayHello(){
   System.out.println("sayHello called");
    return "\nHello world !!\n";
  }
                                                                                
  public void shutdown(){
    orb.shutdown(false);
  }
}


public class CalendarServer{
   static GetEventsImpl getEventsImpl = new GetEventsImpl();

   static final Calendar.Event[] storage = {
   new Calendar.Event("Server1", 
               new Calendar.Time((short)1,(short)2,(short)3,(short)4,(short)5),
               new Calendar.Time((short)0,(short)0,(short)0,(short)0,(short)0)),
   };

   static public void notify_test () {
     // Test : Just try to notify
     Listener listener = getEventsImpl.get_listener();
     System.out.println ( "before notify" );
     listener.notify_me (storage[0]);
     System.out.println ( "notify done" );
   }

  static class NotifyTask extends TimerTask {
        public void run() {
            notify_test();
            System.out.println("Time's up!");
        }
  }


  public static void main(String args[]){
        Timer timer = new Timer();
        NotifyTask nt = new NotifyTask();
        timer.schedule(nt,
                       10000,        //initial delay
                       5*1000);  //subsequent rate

    try{
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // Get reference to rootpoa & activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      //GetEventsImpl getEventsImpl = new GetEventsImpl();
      getEventsImpl.setORB(orb); 

      // create a tie, with servant being the delegate.
      GetEventsPOATie tie = new GetEventsPOATie(getEventsImpl, rootpoa);

      // obtain the objectRef for the tie
      // this step also implicitly activates the 
      // the object
      GetEvents href = tie._this(orb);
            
      // get the root naming context
        /*
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
        */
      
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
        /*
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
        */


      // bind the Object Reference in Naming
        /*
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);
        */

      String ior = orb.object_to_string(href);
      System.out.println(ior);

FileOutputStream fos = new FileOutputStream("/tmp/java-server-ior");
      PrintStream ps = new PrintStream(fos);
      ps.print(ior);
      ps.close();

      System.out.println("HelloServer ready and waiting ...");

      // wait for invocations from clients
      orb.run();
      } 
      
    catch (Exception e){
      System.err.println("ERROR: " + e);
      e.printStackTrace(System.out);
    }
    
    System.out.println("HelloServer Exiting ...");
        
  }
}
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>
<requires lib="gnome"/>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>

  <child>
    <widget class="GtkHBox" id="hbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
	<widget class="GtkCalendar" id="calendar1">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="display_options">GTK_CALENDAR_SHOW_HEADING|GTK_CALENDAR_SHOW_DAY_NAMES</property>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkScrolledWindow" id="scrolledwindow1">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>
	  <property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
	  <property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
	  <property name="shadow_type">GTK_SHADOW_NONE</property>
	  <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

	  <child>
	    <widget class="GtkTreeView" id="treeview1">
	      <property name="visible">True</property>
	      <property name="can_focus">True</property>
	      <property name="headers_visible">True</property>
	      <property name="rules_hint">False</property>
	      <property name="reorderable">False</property>
	      <property name="enable_search">True</property>
	    </widget>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to