print("  "+ClusterExecutionCode.rawMain+"__"+selfID+"();\n");
{ Iterator i = sends_to.iterator(); while (i.hasNext()) {  SIRStream stream = (SIRStream)i.next();  int fromID = selfID; int toID = NodeEnumerator.getSIROperatorId(stream);  print("\n  streamit.scheduler2.constrained.Scheduler cscheduler = new streamit.scheduler2.constrained.Scheduler(ClusterBackend.topStreamIter);  streamit.scheduler2.iriter.Iterator firstIter = IterFactory.createIter((SIRStream)NodeEnumerator.getNode(selfID)); streamit.scheduler2.iriter.Iterator lastIter = IterFactory.createIter(stream);  streamit.scheduler2.SDEPData sdep;  try { sdep = cscheduler.computeSDEP(firstIter, lastIter);  int srcInit = sdep.getNumSrcInitPhases(); int srcSteady = sdep.getNumSrcSteadyPhases();  int dstInit = sdep.getNumDstInitPhases(); int dstSteady = sdep.getNumDstSteadyPhases();  String sdepname = "sdep_"+fromID+"_"+toID;  print("  "+sdepname+" = new sdep("+ srcInit+","+dstInit+","+ srcSteady+","+dstSteady+");\n");  for (int y = 0; y < dstInit + dstSteady + 1; y++) { print("  "+sdepname+"->setDst2SrcDependency("+y+","+sdep.getSrcPhase4DstPhase(y)+");\n"); }  } catch (streamit.scheduler2.constrained.NoPathException ex) {  }    } }  print("\n  "+ClusterExecutionCode.rawMain+"__"+selfID+"();\n");
    public void visitFilter(SIRFilter self,			    SIRFilterIter iter) {	selfID = NodeEnumerator.getSIROperatorId(self);	SIRPortal outgoing[] = SIRPortal.getPortalsWithSender(self);	SIRPortal incoming[] = SIRPortal.getPortalsWithReceiver(self);	HashSet sends_to = new HashSet();	HashSet receives_from = new HashSet();	for (int t = 0; t < outgoing.length; t++) {	    SIRStream[] receivers = outgoing[t].getReceivers();	    for (int i = 0; i < receivers.length; i++) {		sends_to.add(receivers[i]);	    }	}	for (int t = 0; t < incoming.length; t++) {	    SIRPortalSender[] senders = incoming[t].getSenders();	    for (int i = 0; i < senders.length; i++) {		receives_from.add(senders[i].getStream());	    }	}	/*	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		print("// sends to "+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		print("// receives from "+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"\n");	    }	}        	print("\n");	*/		//System.out.print("filter.equals(self): "+(filter.equals(self))+"\n");	//Entry point of the visitor	print("#include <stdlib.h>\n");	print("#include <unistd.h>\n");	print("#include <math.h>\n\n");		//if there are structures in the code, include	//the structure definition header files		//if (RawBackend.structures.length > 0) 	//    print("#include \"structs.h\"\n");	p.print("#include <init_instance.h>\n");	p.print("#include <mysocket.h>\n");	p.print("#include <peek_stream.h>\n");	p.print("#include <timer.h>\n");	p.print("\n");	p.print("extern int __number_of_iterations;\n");	//////////////////////////////////////////////	// Declare Filter Fields	//Visit fields declared in the filter class	JFieldDeclaration[] fields = self.getFields();	for (int i = 0; i < fields.length; i++)	   fields[i].accept(this);	print("\n");	//////////////////////////////////////////////	// Declare Socket Variables	//declare input/output socket variables	NetStream in = RegisterStreams.getFilterInStream(self);	NetStream out = RegisterStreams.getFilterOutStream(self);	if (in != null) {	    	    print("peek_stream<"+self.getInputType().toString()+"> *"+in.name()+"in;\n");	}	if (out != null) {	    	    print("mysocket *"+out.name()+"out;\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		print("mysocket *__msg_sock_"+selfID+"_"+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"out;\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		print("mysocket *__msg_sock_"+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"_"+selfID+"in;\n");	    }	}	//////////////////////////////////////////////	// Declare_Sockets Method	print("\nvoid __declare_sockets_"+selfID+"() {\n");	if (in != null) {	    print("  init_instance::add_incoming("+in.getSource()+","+in.getDest()+");\n");	}	if (out != null) {	    print("  init_instance::add_outgoing("+out.getSource()+","+out.getDest()+",lookup_ip(init_instance::get_node_name("+out.getDest()+")));\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		int dst = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  init_instance::add_outgoing("+(-selfID-1)+","+(-dst-1)+",lookup_ip(init_instance::get_node_name("+dst+")));\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  init_instance::add_incoming("+(-src-1)+","+(-selfID-1)+");\n");	    }	}	print("}\n");	//////////////////////////////////////////////	// Method Declarations	//visit methods of filter, print the declaration first	declOnly = true;	JMethodDeclaration[] methods = self.getMethods();	for (int i =0; i < methods.length; i++) {	    methods[i].accept(this);	}	print("\n");	//////////////////////////////////////////////	// Method Bodies	//now print the functions with body	declOnly = false;	for (int i =0; i < methods.length; i++) {	    methods[i].accept(this);		}	//////////////////////////////////////////////	// Handle Message Method		print("\nvoid handle_message_"+selfID+"(mysocket *sock) {\n");	print("  int index = sock->read_int();\n");	print("  printf(\"Message receieved! thread: "+selfID+", index: %d\\n\", index);\n");	SIRPortal[] portals = SIRPortal.getPortalsWithReceiver(self);	/* there should be only one portal or none */	if (portals.length == 1) {	    	    CClass pclass = portals[0].getPortalType().getCClass();	    CMethod pmethods[] = pclass.getMethods();	    for (int i = 0 ; i < pmethods.length; i++) {		CMethod portal_method = pmethods[i];		CType portal_method_params[] = portal_method.getParameters();		String method_name = portal_method.getIdent();		int length = method_name.length();		if (!method_name.startsWith("<") && 		    !method_name.endsWith(">")) {		    print("  if (index == "+i+") {\n");		    for (int t = 0; t < methods.length; t++) {		    			String thread_method_name = methods[t].getName();						if (thread_method_name.startsWith(method_name) &&			    thread_method_name.charAt(length) == '_' &&			    thread_method_name.charAt(length + 1) == '_') {			    			    int param_count = methods[t].getParameters().length;			    for (int a = 0; a < param_count; a++) {				if (portal_method_params[a].toString().equals("int")) {				    print("    int p"+a+" = sock->read_int();\n");				}				if (portal_method_params[a].toString().equals("float")) {				    print("    float p"+a+" = sock->read_float();\n");				}			    }			    print("    "+thread_method_name+"__"+selfID+"(");			    for (int a = 0; a < param_count; a++) {				if (a > 0) print(", ");				print("p"+a);			    }			    print(");\n");			}		    }		    print("  }\n");		}	    }	}	print("}\n");	//////////////////////////////////////////////	// The Run Method	//now the run function	print("\nvoid run_"+selfID+"() {\n");	print("  int i;\n");	if (in != null) {	    print("  "+in.name()+"in = new peek_stream<"+self.getInputType().toString()+">(new mysocket(init_instance::get_incoming_socket("+in.getSource()+","+in.getDest()+")));\n");	}	if (out != null) {	    print("  "+out.name()+"out = new mysocket(init_instance::get_outgoing_socket("+out.getSource()+","+out.getDest()+"));\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		int dst = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  __msg_sock_"+selfID+"_"+dst+"out = new mysocket(init_instance::get_outgoing_socket("+(-selfID-1)+","+(-dst-1)+"));\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  __msg_sock_"+src+"_"+selfID+"in = new mysocket(init_instance::get_incoming_socket("+(-src-1)+","+(-selfID-1)+"));\n");	    }	}		print("  "+ClusterExecutionCode.rawMain+"__"+selfID+"();\n");		/*	print("  "+self.getInit().getName()+"__"+selfID+"();\n");	if (out == null) print("  timer t;\n");	if (out == null) print("  t.start();\n");		print("  for (i = 0; i < __number_of_iterations; i++) { \n");	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("    if (__msg_sock_"+src+"_"+selfID+"in->data_available()) {\n      handle_message_"+selfID+"(__msg_sock_"+src+"_"+selfID+"in);\n    }\n");	    }	}		print("    "+self.getWork().getName()+"__"+selfID+"();\n");	print("  } \n");	if (out == null) print("  t.stop();\n");	if (out == null) print("  t.output(stderr);\n");	*/	print("  sleep(3); // so that sockets dont get closed\n");	print("}\n");       	createFile(selfID);    }