~ubuntu-branches/ubuntu/natty/electric/natty

« back to all changes in this revision

Viewing changes to com/sun/electric/tool/ServerJobManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2010-01-09 16:26:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100109162604-1ypvmy8ijmlc6oq7
Tags: 8.10-1
* New upstream version.
* debian/control
  - Add libjava3d-java and quilt build dependencies.
  - Update standards version to 3.8.3.
  - Add libjava3d-java as recommends to binary package.
* debian/rules
  - Use quilt patch system instead of simple patchsys.
  - Add java3d related jar files to DEB_JARS.
* debian/patches/*
  - Update as per current upstream source. Convert to quilt.
* debian/ant.properties
  - Do not disable 3D plugin anymore.
  - Use new property to disable compilation of OS X related classes.
* debian/wrappers/electric
  - Add java3d related jar files to runtime classpath.
* debian/README.source
  - Change text to the appropriate one for quilt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import com.sun.electric.database.hierarchy.Cell;
27
27
import com.sun.electric.database.hierarchy.EDatabase;
28
28
import com.sun.electric.database.hierarchy.Library;
 
29
import com.sun.electric.Main;
29
30
import com.sun.electric.database.id.CellId;
30
31
import com.sun.electric.database.id.LibId;
31
32
import com.sun.electric.database.id.TechId;
49
50
import java.util.Iterator;
50
51
import java.util.List;
51
52
import java.util.concurrent.locks.Condition;
 
53
import java.util.concurrent.locks.ReentrantLock;
52
54
import java.util.logging.Level;
53
55
 
54
 
 
55
56
/**
56
57
 *
57
58
 */
58
 
public class ServerJobManager extends JobManager {
 
59
public class ServerJobManager {
59
60
    private static final String CLASS_NAME = Job.class.getName();
60
61
    private static final int defaultNumThreads = Math.max(2, Runtime.getRuntime().availableProcessors());
61
 
    /** mutex for database synchronization. */  private final Condition databaseChangesMutex = newCondition();
 
62
 
 
63
    private final ReentrantLock lock = new ReentrantLock();
 
64
 
 
65
    void lock() { lock.lock(); }
 
66
    void unlock() { lock.unlock(); }
 
67
    /** mutex for database synchronization. */  private final Condition databaseChangesMutex = lock.newCondition();
62
68
 
63
69
    /** started jobs */ private final ArrayList<EJob> startedJobs = new ArrayList<EJob>();
64
70
    /** waiting jobs */ private final ArrayList<EJob> waitingJobs = new ArrayList<EJob>();
65
71
 
66
72
    private final ServerSocket serverSocket;
67
73
//    private final ArrayList<EJob> finishedJobs = new ArrayList<EJob>();
68
 
    private final ArrayList<Client> serverConnections = new ArrayList<Client>();
 
74
    final ArrayList<Client> serverConnections = new ArrayList<Client>();
69
75
    private int passiveConnections;
70
76
//    private final UserInterface redirectInterface = new UserInterfaceRedirect();
71
77
    private int numThreads;
73
79
    private boolean runningChangeJob;
74
80
//    private boolean guiChanged;
75
81
    private boolean signalledEThread;
76
 
    
 
82
 
77
83
    private static int maxNumberOfThreads;
78
84
 
79
85
    /** Creates a new instance of JobPool */
80
86
    ServerJobManager(int recommendedNumThreads, String loggingFilePath, boolean pipe, int socketPort) {
81
87
        maxNumThreads = initThreads(recommendedNumThreads);
82
88
        maxNumberOfThreads = maxNumThreads;
 
89
        if (Job.currentUI != null)
 
90
            initCurrentUI(Job.currentUI);
83
91
        if (loggingFilePath != null)
84
 
            initSnapshotLogging(loggingFilePath);
85
 
        passiveConnections = serverConnections.size();
86
 
        if (Job.currentUI != null)
87
 
            passiveConnections--;
 
92
            initSnapshotLogging(new File(loggingFilePath));
88
93
        if (pipe)
89
94
            initPipe();
90
95
        ServerSocket serverSocket = null;
99
104
        this.serverSocket = serverSocket;
100
105
    }
101
106
 
 
107
    ServerJobManager() {
 
108
        this(0, null, false, 0);
 
109
        serverConnections.add(new Client(0) {});
 
110
    }
 
111
 
102
112
    private int initThreads(int recommendedNumThreads) {
103
113
        int maxNumThreads = defaultNumThreads;
104
114
        if (recommendedNumThreads > 0)
107
117
        return maxNumThreads;
108
118
    }
109
119
 
110
 
    void initSnapshotLogging(String loggingFilePath) {
111
 
        int connectionId = serverConnections.size();
 
120
    void initCurrentUI(AbstractUserInterface currentUI) {
 
121
        lock();
 
122
        try {
 
123
            assert currentUI.connectionId == 0;
 
124
            assert serverConnections.isEmpty();
 
125
            serverConnections.add(currentUI);
 
126
        } finally {
 
127
            unlock();
 
128
        }
 
129
        currentUI.startDispatcher();
 
130
    }
 
131
 
 
132
 
 
133
 
 
134
    void initSnapshotLogging(File loggingFile) {
112
135
        StreamClient conn;
113
136
        lock();
114
137
        try {
115
 
            File loggingFile = new File(loggingFilePath);
116
 
//            File tempFile = File.createTempFile("elec", ".slog");
 
138
            int connectionId = serverConnections.size();
117
139
            FileOutputStream out = new FileOutputStream(loggingFile);
118
140
            System.err.println("Writing snapshot log to " + loggingFile);
119
141
            ActivityLogger.logMessage("Writing snapshot log to " + loggingFile);
120
142
            conn = new StreamClient(connectionId, null, new BufferedOutputStream(out));
121
143
            serverConnections.add(conn);
 
144
            passiveConnections++;
122
145
        } catch (IOException e) {
123
146
            System.err.println("Failed to create snapshot log file:" + e.getMessage());
124
147
            return;
148
171
        try {
149
172
            passiveConnections++;
150
173
            if (passiveConnections == serverConnections.size()) {
151
 
                ActivityLogger.finished();
 
174
                try {
 
175
                    ActivityLogger.finished();
 
176
                } catch (Exception e) {
 
177
                }
152
178
                System.exit(0);
153
179
            }
154
180
        } finally {
156
182
        }
157
183
    }
158
184
 
159
 
 
160
185
    /** Add job to list of jobs */
161
186
    void addJob(EJob ejob, boolean onMySnapshot) {
162
187
        lock();
331
356
//                if (Job.threadMode != Job.Mode.BATCH && ejob.client == null)
332
357
//                    finishedJobs.add(ejob);
333
358
                ejob.state = newState;
334
 
                Client.fireServerEvent(new Client.EJobEvent(ejob, ejob.state));
335
 
 
 
359
                Client.fireServerEvent(new Client.EJobEvent(ejob.jobKey, ejob.jobName,
 
360
                        ejob.getJob().getTool(), ejob.jobType, ejob.serializedJob,
 
361
                        ejob.doItOk, ejob.serializedResult, ejob.newSnapshot, ejob.state));
336
362
                break;
337
363
            case CLIENT_DONE:
338
364
                assert oldState == EJob.State.SERVER_DONE;
340
366
//                    finishedJobs.remove(ejob);
341
367
        }
342
368
        ejob.state = newState;
343
 
        List<Job.Inform> jobs = Job.jobManager.getAllJobInforms();
 
369
        List<Job.Inform> jobs = getAllJobInforms();
344
370
        Client.fireServerEvent(new Client.JobQueueEvent(jobs.toArray(new Job.Inform[jobs.size()])));
345
371
 
346
372
//        if (!Job.BATCHMODE && !guiChanged)
448
474
                    selectedEJob = ejob;
449
475
                    break;
450
476
                }
451
 
//                if (Job.threadMode == Job.Mode.BATCH && startedJobs.isEmpty()) {
452
 
//                    ActivityLogger.finished();
453
 
//                    System.exit(1);
454
 
//                }
 
477
                if (Main.isBatch() && startedJobs.isEmpty()) {
 
478
                    ActivityLogger.finished();
 
479
                    System.exit(0);
 
480
                }
455
481
                Job.logger.logp(Level.FINE, CLASS_NAME, "selectConnection", "pause");
456
482
                databaseChangesMutex.awaitUninterruptibly();
457
483
                Job.logger.logp(Level.FINE, CLASS_NAME, "selectConnection", "resume");
464
490
 
465
491
    /*private*/ static class UserInterfaceRedirect implements UserInterface
466
492
        {
 
493
        private final Job.Key jobKey;
 
494
        private final Client client;
 
495
        private final EDatabase database;
467
496
        TechId curTechId;
468
497
        LibId curLibId;
469
498
        CellId curCellId;
470
499
        private String progressNote;
471
500
        private int progressValue = -1;
472
501
 
 
502
        UserInterfaceRedirect(Job.Key jobKey) {
 
503
            this.jobKey = jobKey;
 
504
            client = Job.serverJobManager.serverConnections.get(jobKey.clientId);
 
505
            database = jobKey.doItOnServer ? EDatabase.serverDatabase() : EDatabase.clientDatabase();
 
506
        }
 
507
 
 
508
        UserInterfaceRedirect(Job.Key jobKey, AbstractUserInterface client) {
 
509
            this.jobKey = jobKey;
 
510
            this.client = client;
 
511
            assert !jobKey.doItOnServer;
 
512
            database = EDatabase.clientDatabase();
 
513
        }
 
514
 
 
515
        UserInterfaceRedirect(EDatabase database) {
 
516
            jobKey = null;
 
517
            client = null;
 
518
            this.database = database;
 
519
        }
 
520
 
 
521
        void setCurrents(Job job) {
 
522
            assert jobKey == job.getKey();
 
523
            curTechId = job.curTechId;
 
524
            curLibId = job.curLibId;
 
525
            curCellId = job.curCellId;
 
526
        }
 
527
 
473
528
        private static void printStackTrace(String methodName) {
474
529
            if (!Job.getDebug()) return;
475
530
            System.out.println("UserInterface." + methodName + " was called from DatabaseChangesThread");
528
583
        }
529
584
 
530
585
        public Job.Key getJobKey() {
531
 
            return Job.getRunningEJob().jobKey;
 
586
            return jobKey;
532
587
        }
533
588
 
534
589
        public EDatabase getDatabase() {
535
 
            return EDatabase.serverDatabase();
 
590
            return database;
536
591
        }
537
592
        public Technology getCurrentTechnology() {
538
 
            EDatabase database = getDatabase();
539
593
            Technology tech = null;
540
594
            if (curTechId != null)
541
595
                tech = database.getTech(curTechId);
546
600
            return tech;
547
601
        }
548
602
        public Library getCurrentLibrary() {
549
 
            return curLibId != null ? getDatabase().getLib(curLibId) : null;
 
603
            return curLibId != null ? database.getLib(curLibId) : null;
550
604
        }
551
605
 
552
606
                public EditWindow_ getCurrentEditWindow_() {
561
615
        /** Get current cell from current library */
562
616
                public Cell getCurrentCell()
563
617
        {
564
 
            return curCellId != null ? getDatabase().getCell(curCellId) : null;
 
618
            return curCellId != null ? database.getCell(curCellId) : null;
565
619
        }
566
620
 
567
621
                public Cell needCurrentCell()
592
646
 
593
647
        /**
594
648
         * Method to return the error message associated with the current error.
595
 
         * Highlights associated graphics if "showhigh" is nonzero.  Fills "g1" and "g2"
596
 
         * with associated geometry modules (if nonzero).
 
649
         * Highlights associated graphics if "showhigh" is nonzero.
597
650
         */
598
 
        public String reportLog(ErrorLogger.MessageLog log, boolean showhigh, Geometric[] gPair, int position)
 
651
        public String reportLog(ErrorLogger.MessageLog log, boolean showhigh, boolean separateWindow, int position)
599
652
        {
600
653
            printStackTrace("reportLog");
601
654
            // return the error message
609
662
         */
610
663
        public void showErrorMessage(String message, String title)
611
664
        {
612
 
            EJob ejob = Job.getRunningEJob();
613
 
            Client.fireServerEvent(new Client.ShowMessageEvent(ejob.client, message, title, true));
 
665
            Client.fireServerEvent(new Client.ShowMessageEvent(client, message, title, true));
614
666
        }
615
667
 
616
668
        /**
620
672
         */
621
673
        public void showInformationMessage(String message, String title)
622
674
        {
623
 
            EJob ejob = Job.getRunningEJob();
624
 
            Client.fireServerEvent(new Client.ShowMessageEvent(ejob.client, message, title, false));
 
675
            Client.fireServerEvent(new Client.ShowMessageEvent(client, message, title, false));
625
676
        }
626
677
 
627
678
        /**
632
683
        public void printMessage(String message, boolean newLine) {
633
684
            if (newLine)
634
685
                message += "\n";
635
 
            EJob ejob = Job.getRunningEJob();
636
 
            Client.fireServerEvent(new Client.PrintEvent(ejob.client, message));
 
686
            Client.fireServerEvent(new Client.PrintEvent(client, message));
637
687
        }
638
688
 
639
689
        /**
641
691
         * @param filePath file to save
642
692
         */
643
693
        public void saveMessages(String filePath) {
644
 
            EJob ejob = Job.getRunningEJob();
645
 
            Client.fireServerEvent(new Client.SavePrintEvent(ejob.client, filePath));
 
694
            Client.fireServerEvent(new Client.SavePrintEvent(client, filePath));
646
695
        }
647
696
 
648
697
        /**
705
754
    public static int getDefaultNumberOfThreads(){
706
755
        return defaultNumThreads;
707
756
    }
708
 
    
 
757
 
709
758
    public static int getMaxNumberOfThreads(){
710
759
        return maxNumberOfThreads;
711
760
    }
712
 
    
713
761
}