001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), hosted at https://github.com/gunterze/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2011
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.net;
040
041import java.io.IOException;
042
043import org.dcm4che3.net.pdu.AAbort;
044import org.dcm4che3.net.pdu.AAssociateAC;
045import org.dcm4che3.net.pdu.AAssociateRJ;
046import org.dcm4che3.net.pdu.AAssociateRQ;
047
048/**
049 * @author Gunter Zeilinger <gunterze@gmail.com>
050 *
051 */
052public enum State {
053    Sta1("Sta1 - Idle") {
054
055        @Override
056        void write(Association as, AAbort aa) {
057            // NO OP
058        }
059
060        @Override
061        void closeSocket(Association as) {
062            // NO OP
063        }
064
065        @Override
066        void closeSocketDelayed(Association as) {
067            // NO OP
068        }
069    },
070    Sta2("Sta2 - Transport connection open") {
071
072        @Override
073        void onAAssociateRQ(Association as, AAssociateRQ rq)
074                throws IOException {
075            as.handle(rq);
076        }
077    },
078    Sta3("Sta3 - Awaiting local A-ASSOCIATE response primitive"),
079    Sta4("Sta4 - Awaiting transport connection opening to complete"),
080    Sta5("Sta5 - Awaiting A-ASSOCIATE-AC or A-ASSOCIATE-RJ PDU") {
081
082        @Override
083        void onAAssociateAC(Association as, AAssociateAC ac)
084                throws IOException {
085            as.handle(ac);
086        }
087
088        @Override
089        void onAAssociateRJ(Association as, AAssociateRJ rj)
090                throws IOException {
091            as.handle(rj);
092        }
093    },
094    Sta6("Sta6 - Association established and ready for data transfer") {
095
096        @Override
097        void onAReleaseRQ(Association as) throws IOException {
098            as.handleAReleaseRQ();
099        }
100
101        @Override
102        void onPDataTF(Association as) throws IOException {
103            as.handlePDataTF();
104        }
105
106        @Override
107        void writeAReleaseRQ(Association as) throws IOException {
108            as.writeAReleaseRQ();
109        }
110
111        @Override
112        public void writePDataTF(Association as) throws IOException {
113            as.doWritePDataTF();
114        }
115    },
116    Sta7("Sta7 - Awaiting A-RELEASE-RP PDU") {
117
118        @Override
119        public void onAReleaseRP(Association as) throws IOException {
120            as.handleAReleaseRP();
121        }
122
123        @Override
124        void onAReleaseRQ(Association as) throws IOException {
125            as.handleAReleaseRQCollision();
126        }
127
128        @Override
129        void onPDataTF(Association as) throws IOException {
130            as.handlePDataTF();
131        }
132    },
133    Sta8("Sta8 - Awaiting local A-RELEASE response primitive") {
134
135        @Override
136        public void writePDataTF(Association as) throws IOException {
137            as.doWritePDataTF();
138        }
139    },
140    Sta9("Sta9 - Release collision requestor side; awaiting A-RELEASE response"),
141    Sta10("Sta10 - Release collision acceptor side; awaiting A-RELEASE-RP PDU"){
142
143        @Override
144        void onAReleaseRP(Association as) throws IOException {
145            as.handleAReleaseRPCollision();
146        }
147    },
148    Sta11("Sta11 - Release collision requestor side; awaiting A-RELEASE-RP PDU"){
149
150        @Override
151        void onAReleaseRP(Association as) throws IOException {
152            as.handleAReleaseRP();
153        }
154    },
155    Sta12("Sta12 - Release collision acceptor side; awaiting A-RELEASE response primitive"),
156    Sta13("Sta13 - Awaiting Transport Connection Close Indication") {
157
158        @Override
159        public void onAReleaseRP(Association as) throws IOException {
160            // NO OP
161        }
162
163        @Override
164        void onAReleaseRQ(Association as) throws IOException {
165            // NO OP
166        }
167
168        @Override
169        void onPDataTF(Association as) throws IOException {
170            // NO OP
171        }
172
173        @Override
174        void write(Association as, AAbort aa) {
175            // NO OP
176        }
177
178        @Override
179        void closeSocketDelayed(Association as) {
180            // NO OP
181        }
182    };
183
184    private String name;
185
186    State(String name) {
187        this.name = name;
188    }
189
190    @Override
191    public String toString() {
192        return name;
193    }
194
195    void onAAssociateRQ(Association as, AAssociateRQ rq)
196            throws IOException {
197        as.unexpectedPDU("A-ASSOCIATE-RQ");
198    }
199
200    void onAAssociateAC(Association as, AAssociateAC ac)
201            throws IOException {
202        as.unexpectedPDU("A-ASSOCIATE-AC");
203    }
204
205    void onAAssociateRJ(Association as, AAssociateRJ rj)
206            throws IOException {
207        as.unexpectedPDU("A-ASSOCIATE-RJ");
208    }
209
210    void onPDataTF(Association as) throws IOException {
211        as.unexpectedPDU("P-DATA-TF");
212    }
213
214    void onAReleaseRQ(Association as) throws IOException {
215        as.unexpectedPDU("A-RELEASE-RQ");
216    }
217
218    void onAReleaseRP(Association as) throws IOException {
219        as.unexpectedPDU("A-RELEASE-RP");
220    }
221
222    void writeAReleaseRQ(Association as) throws IOException {
223        throw new AssociationStateException(this);
224    }
225
226    void write(Association as, AAbort aa) throws IOException {
227        as.write(aa);
228    }
229
230    public void writePDataTF(Association as) throws IOException {
231        throw new AssociationStateException(this);
232    }
233
234    void closeSocket(Association as) {
235        as.doCloseSocket();
236    }
237
238    void closeSocketDelayed(Association as) {
239        as.doCloseSocketDelayed();
240    }
241}